How to save a pandas panel into a file?

I am running an experiment several times to average the results in order to smooth the curves. I choose a pandas panel for its multiple advantages, however since it takes too much to compute I decided to save the panel:

panel=Experiment(data, labels, test_size, n_label,10) panel.to_json(args["output"])

I have tried the methods available by autocompleting .to_json() or .to_csv()... but I always get an "Not implemented error". I just want to have the data in a file.

Upvotes: 1

Views: 1771

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210912

Consider the following example:

from pandas_datareader import data as web

p = web.DataReader(['AAPL','GOOG'], 'google', '2017-04-01')

# save Panel to HDF5 file
p.to_hdf('c:/temp/panel_test.h5', 'key')

# read from HDF5 file to Panel
pp = pd.read_hdf('c:/temp/panel_test.h5', 'key')

Let's compare them

In [13]: pp
Out[13]:
<class 'pandas.core.panel.Panel'>
Dimensions: 5 (items) x 31 (major_axis) x 2 (minor_axis)
Items axis: Open to Volume
Major_axis axis: 2017-04-03 00:00:00 to 2017-05-16 00:00:00
Minor_axis axis: AAPL to GOOG

In [14]: p
Out[14]:
<class 'pandas.core.panel.Panel'>
Dimensions: 5 (items) x 31 (major_axis) x 2 (minor_axis)
Items axis: Open to Volume
Major_axis axis: 2017-04-03 00:00:00 to 2017-05-16 00:00:00
Minor_axis axis: AAPL to GOOG

NOTE: be aware that Panels will be deprecated in future Pandas releases, so it's better to use xarrays instead

a = p.to_xarray()

# http://xarray.pydata.org/en/stable/io.html
a.to_netcdf('c:/temp/test.nc')

Upvotes: 4

Related Questions