By subsampling data from a Excel spreadsheet I have created a Mathematica dataset that I then seek to modify by applying various functions to the fields to convert these data into a different flat file format in a sorted order for eventual output.
One field of the dataset that I wish to format involves the date at which a particular collection was made. In particular, one key value pair(column name & value) contains dates typically in the format "1950/2/20" that I wish to convert to "20 Feb 1950". Consequently, I am trying to make the conversion in a statement:
bskudatasorted2 = bskudatasorted[All, {"BSKUno" -> bskunoconvert, "LocalityEn" -> bskulocalityconvert, "CollDateFrom" -> bskucolldatefromconvert}]
where bskudatasorted is the dataset in sorted order and where bskucolldateformatconvert is a overloaded function designed to deal with specific entries that do not quite conform to the expected pattern given above (ie such as "","Null", 1952/**/**, "1952/02/**"etc.). Taking one signature of this overloaded function, namely,
bskucolldatefromconvert[x_ /; StringMatchQ[x, __ ~~ "/" ~~ "**" ~~ "/" ~~"**"]] := Module[{y1}, y1 = DateString[DateObject[x ], {"Year"}];Return[y1]];
However, this function fails to provide the correct date conversion (the functions bskunoconvert and bskulocalityconvert work fine but there is an error in my function bskucolldatefromconvert) and the overloaded function returns a rather cryptic error: Failure. Message Template: Message:ssgl; MessageParameters{$MessageList} Tag: Message.
To debug this I have simply imported one test entry (ie skudatasorted[[211,36]]) with the day and month missing to understand why it is not working as expected. I do this as follows:
bskudatasorted[[211, 36]]
and get out the String "1952/**/**" as expected
If I execute
DateObject[bskudatasorted[[211,36]]
I then get out a DateObject with the correct date (ie. "Year: 1952")
If I then execute the following:
DateString[DateObject[bskudatasorted[[211, 36]]], "Year"]
I also get out the correct year
Out[109]="1952"
If further executing
StringMatchQ[bskudatasorted[[211, 36]], __ ~~ "/" ~~ "**" ~~ "/" ~~ "**"]
I get Out[113]= True
Again, indicating that this entry is being corrected identified as having the appropriate pattern for conditional execution.
And again, at this point:
In[104]:= Head[%] gives me, as expected,
Out[104]= String
that indicates that the entry reported is in fact a String.
However, if I attempt to call the function as:
bskucolldatefromconvert[bskudatasorted[[211, 36]]]
I do not get the correct/expected date but rather:
Out[116]= "Tue Jan 1952"
. It would seem that the default behavior of DateString somehow needs to be overridden when part of the date string is missing.
How do I suppress the default behavior when some elements of the date are missing or otherwise alter my function and those in other signatures (e.g. when only Month and Year are present) so that the correct date (ie only "1952" in this case) is returned when parts of the date are missing?
DateString
; "Those not filled in are taken to have default values {Subscript[y, c],1,1,0,0,0}, where Subscript[y, c] is the current year." That is you have only specified"Year"
and so date string is filling in the rest as per the documentation. SeeDateValue
for year only dates. $\endgroup$DateString[DateObject@"1952/**/**", {"Year"}]
returns"1952"
. $\endgroup$