Message Boards Message Boards

0
|
5167 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Function to turn Dataset columns into time series by year?

Posted 3 years ago
Attachments:
POSTED BY: Michael Madsen
4 Replies
Posted 3 years ago

As an alternative to Eric's answer, you could generate an Association to store the timeseries for each year. e.g.

years = dataset // First // Keys // Normal // Take[#, {2, -10}] &
timeSeries = 
 AssociationMap[
  TimeSeries[
    Normal@Values@dataset[All, {#}], {"Oct 10 " <> ToString[ToExpression[#] - 1]}] &, years]

Then pick the years you want to plot

KeyTake[timeSeries, {"2010", "2020"}] // Values // DateListPlot

Or as individual plots

KeyTake[timeSeries, {"2010", "2015", "2020"}] // 
 KeyValueMap[DateListPlot[#2, PlotLabel -> #1] &]

Also instead of "Oct 10", shouldn't it be "Oct 1" since the date column starts at 10-01?

POSTED BY: Rohit Namjoshi
Posted 3 years ago

I'm not quite understanding your desire. You said, "I would like to capture each as a separate variable but do it programmatically so I can change the date range without have to set all the variables". So, I'm confused about whether you need individual variables or not.

Using the code you've already built, you can just do:

DateListPlot[
 TimeSeries[
    Normal@
     Values@
      dataset[
       All, {ToString[
         ToExpression[#] - 1]}], {"Oct 10 " <> #}] & /@ {"2019", 
   "2020", "2021", "2022"}]

If you don't want to keep adding years one-by-one, you could build a helper function for that. Something like this:

YearRange[start_, stop_] := ToString /@ Range[start, stop]

If you need the TimeSeries data stored in a variable, just assign that expression to a variable. The variable will contain a List, which can then be accessed by position (if you need the individual year data time series data). Or you just use the variable as a way to simplify the DateListPlot expression. Something like this:

plotData = 
  TimeSeries[
     Normal@
      Values@
       dataset[
        All, {ToString[ToExpression[#] - 1]}], {"Oct 10 " <> #}] & /@ 
   YearRange[2019, 2022];

DateListPlot[plotData]
POSTED BY: Eric Rimbey
Posted 3 years ago

Another alternative that creates an Association, but could easily extend to Dataset

yKeys = Keys[dataset[1]] // Normal // 
   StringCases[StringExpression @@ Table[DigitCharacter, 4]] // Flatten

assn1 = With[{y = #, yTS = ToString[ToExpression@# - 1]},
      y -> dataset[TimeSeries[#, {"Oct 1 " <> yTS}] &, y]
      ] & /@ yKeys // Association;

DateListPlot[Lookup[assn1, {"1981", "2020", "2021", "2022"}], 
 ImageSize -> Large]

Custom Years

DateListPlot[Lookup[assn1, yKeys], ImageSize -> Large]

All Years

POSTED BY: David G
Posted 3 years ago

Thank you both of those responses have been helpful. Yes, the date should start on October 1st.

POSTED BY: Michael Madsen
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract