3
$\begingroup$

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:

[temperatureSeries["DatePath"] //Map@formatSeries output.[2]

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]
$\endgroup$

closed as off-topic by MarcoB, José Antonio Díaz Navas, Henrik Schumacher, eyorble, Edmund Feb 21 at 21:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, José Antonio Díaz Navas, Henrik Schumacher, eyorble, Edmund
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Notice that the Head of temperatureSeries as you defined it is TemporalData, not TimeSeries. Since you restricted the definition of convertToList to only working on TimeSeries, 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$ – MarcoB Feb 15 at 19:38
2
$\begingroup$

This works well for me. You might want to try it.

data =
  Module[{dates, temps},
    dates = DateValue[#, {"Year", "Month", "Day", "Hour"}] & /@ temperatureSeries["Dates"];
    temps = QuantityMagnitude[#] & /@ temperatureSeries["Values"];
    Flatten /@ Thread[{dates, temps}]];

Export[FileNameJoin[{$HomeDirectory, "Desktop", "Temperature.csv"}], data]

The exported CSV file looks like this

2018,1,1,0,-26.3
2018,1,1,1,-27
2018,1,1,2,-27
2018,1,1,3,-26.7
2018,1,1,4,-27.2
2018,1,1,5,-27.2
2018,1,1,6,-27.4
2018,1,1,7,-27.9
2018,1,1,8,-28.5
2018,1,1,9,-28.2
2018,1,1,10,-28.2
2018,1,1,11,-28.6

 ...

2018,1,31,12,-24.4
2018,1,31,13,-25.4
2018,1,31,14,-25.7
2018,1,31,15,-26.2
2018,1,31,16,-26
2018,1,31,17,-25.6
2018,1,31,18,-24.9
2018,1,31,19,-24.2
2018,1,31,20,-23.6
2018,1,31,21,-23.4
2018,1,31,22,-23.4
2018,1,31,23,-23.3

Notes:

  • I only asked for the temperatures for January, 2018, in my query to WeatherData.

  • Since the data in temperatureSeries is hourly, I added a hour column to the exported data. You can simply remove "Hour" from the 2nd DateValue argument if don't want this column.

$\endgroup$

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