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[...] *)
tsds = ds1[ KeyValueMap[Association[# -> TimeSeries[#2, {"Oct 1 " <> #}]] &]@* Transpose, yKeys]
give something close to what you need? $\endgroup$ds1[Merge[TimeSeries], yKeys]
? $\endgroup$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$