'How to avail "Forecasting: Methods and Application" dataset in Python?

I am using the book Forecasting: Methods and Applications by Makridakis, Wheelwright and Hyndman. I want to do the exercises along the way, but in Python, not R (as suggested in the book).

I do not know how to use R. I know that the datasets can be availed from an R package - fma. This is the link to the package.

Is there a possible script, in R or Python, which will allow me to download the datasets as .csv files? That way, I will be able to access them using Python.



Solution 1:[1]

one possibility:

## install and load package:
install.packages('fma')
library('fma')

## list example data of package fma:
data(package = 'fma')

## export single data as csv:
write.csv(cement, file = 'cement.csv')

## bulk export:
## data names are in `[,3]`rd column of list member "results"
## of `data(...)` output
for (data_name in data(package = 'fma')[['results']][,3]){
    write.csv(get(data_name), file = paste0(data_name, '.csv'))
}

Edit:

As Anirban noted, attaching the package {fma} exposes only a few datasets to the search path. The data can be obtained by cloning or downloading from Rob J. Hyndman's source (click green Code button and choose). Subfolder 'data' contains each dataset as an .rda file which can be load()ed and converted. (Observe the licence conditions - GPL3.0 - and acknowledge the authors' efforts anyway.)

That said, you could load and convert the data like this:

setwd('path/to/fma-master/data')

for(data_name in dir()){
    cat(paste0('converting ', data_name, '... '))
    load(data_name)
    object_name <- (gsub('\\.rda','', data_name))
    write.csv(get(object_name),
              file = paste0(object_name,'.csv'),
              row.names = FALSE,
              append = FALSE ## overwrite file if exists
              )
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1