5
$\begingroup$

Bug in 10.4.1 still in 11.0

DateObjects with DateFormat option appear to have the first date part truncated when displayed in a Dataset.

Outside of a Dataset the DateFormat displays as expected.

DateObject[{2007, 1, 20}, DateFormat -> {"Year", "-", "Month", "-", "Day"}]

enter image description here

However, in a Dataset it shows as with the "Year" truncated.

Dataset[
 AssociationThread[{"Date", "One", "Two"}, 
   {DateObject[{2007, 1, 20}, DateFormat -> {"Year", "-", "Month", "-", "Day"}], 1, 2}]]

enter image description here

In fact, it truncates the first 4 characters of the formatted string.

Bug? Or am I doing something wrong?

10.4.1 Win 7 Ent SP1.

WRI confirmed bug. CASE:3625692

$\endgroup$
6
$\begingroup$

The result we see is due to a somewhat baffling formatting decision made within the machinery that generates the box form of datasets. When formatting a date object, it uses DateString form. However, it also expressly checks to see whether the date has three components. If so, it drops the first four characters from the string form. This is strange and I am tempted to call it a bug. It wouldn't hurt to report it to Wolfram.

Here is a direct exhibition of the problem:

Dataset`MakeAtomBox[
  TypeSystem`Atom[DateObject]
, DateObject[{2007, 1, 20}, DateFormat -> {"Year", "-", "Month", "-", "Day"}]
]

(*
    InterpretationBox[TagBox[TooltipBox[StyleBox["-01-20", ...], ...]
*)

Analysis (valid for version 10.4.1)

The problematic definition can be seen like this:

Block[{Dataset`RichBox = HoldForm}
, Dataset`MakeAtomBox[
    TypeSystem`Atom[DateObject]
  , DateObject[{2007, 1, 20}, DateFormat -> {"Year", "-", "Month", "-", "Day"}]
  ]
]

(*
HoldForm[
  DateObject[{2007, 1, 20}, DateFormat -> ...]
, If[Length[First[DateObject[{2007, 1, 20}, DateFormat -> ...]]] == 3
  , StringDrop[#1, 4] &
  , Identity][DateString[DateObject[{2007, 1, 20}, ...]]
  ]
]
*)

or by looking for the DateObject definition in the output of:

Dataset;
Needs["GeneralUtilities`"]
PrintDefinitions[Dataset`FormattingAtomic`PackagePrivate`abox]

Work-around

A quick fix, which will void your warranty, is to replace the problematic definition:

Dataset`FormattingAtomic`PackagePrivate`abox[DateObject, x_DateObject] :=
    Dataset`RichBox[x, DateString[x]]

Giving:

Dataset[
 AssociationThread[{"Date", "One", "Two"}, 
   {DateObject[{2007, 1, 20}, DateFormat -> {"Year", "-", "Month", "-", "Day"}], 1, 2}]]

dataset screenshot

$\endgroup$
  • $\begingroup$ No interest in voiding my warranty. However, I think the developer may have decided to drop the first 4 characters since the default date format starts with the short form of the day name ("Mon", "Tue" , etc.). They may have decided that they were helping everyone out by removing it. $\endgroup$ – Edmund Jun 7 '16 at 15:50
  • $\begingroup$ Adding four spaces to the front ( DateFormat ->{" ", "Year", "-", "Month", "-", "Day"} ) is a less scary proposition. $\endgroup$ – Edmund Jun 7 '16 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.