4
$\begingroup$

I recently responded to a question on Wolfram Community. My solution works, but I believe it could be improved. I'm trying to apply a TimeSeries to one or more columns of a Dataset. The column headers are the year specifying the end of the time span of the columns. The start of each year is "Oct 1". For example, the data time span would be "Oct 1 1980" to "Sep 30 1981" for the column header "1981". There is a "date" column in "Month-Day" format, but for this problem it doesn't seem useful to use that because the year and fixed start day is specified already.

One can take advantage of the TimeSeries method of specifing a start time. The tricky part is that the start time is based on a column header ("Year"). See my code below.

My question is:

Is it possible to use only a Dataset query rather than use the method that I used which involves nesting the Dataset query within a With block and mapping that onto column keys? In other words, can I eliminate the With block?

The return value could be a Dataset rather than an Association as in my code.

The data is imported from a CSV file. Here is the solution that I used:

url = "https://www.nrcs.usda.gov/Internet/WCIS/AWS_PLOTS/basinCharts/\
POR/WTEQ/assocHUCut3/state_of_utah.csv";
ds1 = Import[url, "Dataset", HeaderLines -> 1]
yKeys = Keys[ds1[1]] // Normal // 
   StringCases[StringExpression @@ Table[DigitCharacter, 4]] // 
  Flatten

(* {"1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", \
"1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", \
"1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", \
"2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", \
"2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", \
"2021", "2022"} *)
assn1 = With[{y = #, yTS = ToString[ToExpression@# - 1]},
      y -> ds1[TimeSeries[#, {"Oct 1 " <> yTS}] &, y]
      ] & /@ yKeys // Association;

(* Extract one or more years worth of `TimeSeries`. *)

Lookup[assn1, {"1981", "2020", "2021", "2022"}]

(* {TimeSeries[...], TimeSeries[...], TimeSeries[...], TimeSeries[...] *)
$\endgroup$
6
  • 1
    $\begingroup$ does tsds = ds1[ KeyValueMap[Association[# -> TimeSeries[#2, {"Oct 1 " <> #}]] &]@* Transpose, yKeys] give something close to what you need? $\endgroup$
    – kglr
    Commented Jan 2, 2022 at 2:24
  • 1
    $\begingroup$ ... or ds1[Merge[TimeSeries], yKeys]? $\endgroup$
    – kglr
    Commented Jan 2, 2022 at 2:35
  • 1
    $\begingroup$ @kglr the expression for tsds is very close to what I'm looking for. I made one modification to make the start year correct: tsds = ds1[ KeyValueMap[ Association[# -> TimeSeries[#2, {"Oct 1 " <> ToString[ToExpression[#] - 1]}]] &]@*Transpose, yKeys] $\endgroup$
    – david
    Commented Jan 2, 2022 at 16:01
  • $\begingroup$ @david: Does the URL link you provided above work properly? I tried to import the same dataset but it seems that the link is not working. $\endgroup$ Commented Jan 2, 2022 at 18:07
  • $\begingroup$ @TugrulTemel, yes I just copied from this post and tried it. Worked fine. Did you double check that you copied the whole link as posted? $\endgroup$
    – david
    Commented Jan 2, 2022 at 22:33

1 Answer 1

2
$\begingroup$
yearKeys = Select[SyntaxQ[#] && Internal`PossibleDateQ[{ToExpression@#, 1, 1}] &]@
   Normal[Keys[First @ ds1]];

timeSeries = ds1[Map[First] @* Merge[Identity] @*
   KeyValueMap[Association[# -> 
     TimeSeries[#2, {"Oct 1 " <> IntegerString[FromDigits @ # - 1]}]] &] @* Transpose, 
  yearKeys]

enter image description here

{timeSeries[{"1981", "2020", "2021", "2022"}], 
 Query[{"1981", "2020", "2021", "2022"}] @ timeSeries}

enter image description here

timeSeries[DateListPlot @* Map[List]@*{"1981", "1995", "2020", "2021", "2022"}]

enter image description here

timeSeries[{"1981", "1995", "2020", "2021", "2022"} /* Map[DateListPlot]]

enter image description here

$\endgroup$
2
  • $\begingroup$ I like this answer. Can you make the slight change to the start date as I did in my comment above? That will account for the unconventional way the years start. I need to explore your use of Transpose. I'm still learning how to work efficiently with Dataset and Query. Thanks. $\endgroup$
    – david
    Commented Jan 2, 2022 at 16:08
  • $\begingroup$ @david, updated with the change you suggested. $\endgroup$
    – kglr
    Commented Jan 2, 2022 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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