Message Boards Message Boards

Combine TimeSeriesThread and WeatherData?

Hello,

I'm exploring WeatherData and was interested in using TimeSeriesThread with it, but running into trouble with syntax.

As an example, suppose I wanted to calculate the arithmetic mean maximum temperature each month across a number of years. I'd first generate my data using WeatherData:

WeatherData["Syracuse", 
   "MaxTemperature", {{#, 1, 1}, {#, 12, 31}, "Month"}] & /@ 
 Range[2000, 2017]

If I understand this correctly, that should get me a list of 17 TimeSeries, each of which contains 12 data points: the maximum temperature for each month during the year.

The next step would be to add all 17 entries for each month (e.g. sum all the January max temps), then divide by the number of years in my range, in this case 17. The final result would be a new TimeSeries with 12 data points, each of which is the mean maximum temperature each month across that range of 17 years. Reading the documentation and looking at the examples, it seems TimeSeriesThread would fit the need, but the syntax eludes me.

Thanks for any assistance.

Christopher

POSTED BY: Christopher Fox
4 Replies

Thank you again, Henrik. That makes it perfectly clear, and I've built an example that demonstrates if the time/date stamps match for the two time series, WeatherData works as expected:

DateListPlot[
 TimeSeriesThread[
  First[#] - Last[#] &, {WeatherData["Richmond", 
    "MaxTemperature", {{2017, 1, 1}, {2017, 12, 31}, "Day"}], 
   WeatherData["Syracuse", 
    "MaxTemperature", {{2017, 1, 1}, {2017, 12, 31}, "Day"}]}]]

Thanks again for your help.

POSTED BY: Christopher Fox

Thank you, Henrik.

I realized my "off by one" error on my drive in to work this morning as I was thinking about this problem further.

There's an example of TimeSeriesThread that is based on calendar dates from FinancialData:

goog = FinancialData["GOOGL", "2012"];
appl = FinancialData["AAPL", "2012"];
DateListPlot[TimeSeriesThread[First[#] - Last[#] &, {goog, appl}]]

Any thoughts as to why this works, but not a WeatherData?

Thanks again for your help.

POSTED BY: Christopher Fox

Hi Christopher,

in your example above (taken from the documentation) both TimeSeries describe data belonging to the identical date range! So an operation (subtraction here) can be done "date-wise", i.e. (value1 at some date) - (value2 at the same date). In your original problem you do not have this situation, but just the opposite: Every of your data is associated with a different date. It is basically the same as if you would try to do this:

goog12 = FinancialData["GOOGL", {"Jan. 1, 2012", "Dec. 31, 2012"}];
appl13 = FinancialData["AAPL", {"Jan. 1, 2013", "Dec. 31, 2013"}];
DateListPlot[TimeSeriesThread[First[#] - Last[#] &, {goog12, appl13}]]

(This was the point I tried to make in my first reply.) And therefore - at least according to my understanding - the application of TimeSeriesThread does not make sense here.

Regards -- Henrik

POSTED BY: Henrik Schachner

Hi Christopher,

I guess TimeSeriesThread is not suitable for your problem, because the output you want cannot be assigned to a specific date but just to a month in general. So the simplest solution would probably be to extract just the temperatures (which are sorted in monthly order anyway):

wd = WeatherData["Syracuse", "MaxTemperature", {{#, 1, 1}, {#, 12, 31}, "Month"}] & /@ Range[2000, 2017];
meanTemps = Mean /@ Transpose[Map[Last, Normal@wd, {2}]];
ListLinePlot[meanTemps, AxesLabel -> {"month", Automatic}]

By they way: The number in question is 18 (i.e. Range[2000, 2017] // Length)

I hope this helps, regards -- Henrik

POSTED BY: Henrik Schachner
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