I have tried the following code to export a TimeSeries
to a CSV.
This is using code from a similar post "Export TimeSeries[] to CSV or Excel".
ClearAll["Global`*"]
(* Helper Functions to format the export *)
formatDate[date_DateObject]:=DateValue[date, {"Year", "Month", "Day"}]
formatTemperature[value_Quantity]:=QuantityMagnitude[value]
formatSeries[{date_DateOject,value_Quantity}]:={formatDate@date, formatTemperature@value} //Flatten
convertToList[timeSeries_TimeSeries]:=timeSeries["DatePath"] //Map@formatSeries
(* Get Weather Data *)
windSpeedSeries=WeatherData[{53.506574, -112.096163},"WindSpeed", {{2018,1,1},{2018,12,31}}]
temperatureSeries=WeatherData[{53.506574, -112.096163},"Temperature", {{2018,1,1},{2018,12,31}}]
When I call convertToList[temperatureSeries]
, it appears that convertToList
is not defined.
If I issue the following command:
temperatureSeries["DatePath"] // Map@formatSeries
I get the following output:
[
I think this is a basic question.
This looks like a series of commands that I would like to execute on each element in the list of {date, temperature} pairs.
How would I actually execute this so that I can successfully export the following data columns to a CSV file?
Year, Month, Day, Temperature
If I simply call Export["Temperatures.csv", temperatureSeries["DatePath"] //Map@formatSeries]
, I simply get a text version of the image above.
This leads me to believe that formatSeries
in the code above is undefined. I am not clear on why formatSeries
is undefined.
Alternate Approach
While this is not at all performant, the code below does work. I would suggest that it be translated into a functional code equivalent.
temperatureSeries=WeatherData[{53.506574, -112.096163},"Temperature", {{2018,1,1},{2018,12,31}}]
temperatureList = Table[{temperatureSeries["DatePath"][[i]][[1]][[1]][[1]],
temperatureSeries["DatePath"][[i]][[1]][[1]][[2]],
temperatureSeries["DatePath"][[i]][[1]][[1]][[3]],
QuantityMagnitude[temperatureSeries["DatePath"][[i]][[2]]]},{i,1,Length[temperatureSeries["DatePath"]]}]
Export["Temperature.csv", temperatureList]
Head
oftemperatureSeries
as you defined it isTemporalData
, notTimeSeries
. Since you restricted the definition ofconvertToList
to only working onTimeSeries
, then yes it is technically undefined for the input you provided. I suspect that it will work if you fix or remove the pattern restrictions on your function definitions. $\endgroup$