In [1]:
import numpy as np
import pandas as pd
In [2]:
# create time-indexed data frame
arr = np.random.randn(100)
cols = {f'column_{i}': arr for i in range(3)}
df = pd.DataFrame(data=cols, index=pd.date_range('2019-01-01', periods=len(arr)))
df.tail()
Out[2]:
column_0 column_1 column_2
2019-04-06 -0.455504 -0.455504 -0.455504
2019-04-07 -2.018253 -2.018253 -2.018253
2019-04-08 1.305361 1.305361 1.305361
2019-04-09 -0.290746 -0.290746 -0.290746
2019-04-10 1.679213 1.679213 1.679213
In [3]:
df.to_feather('time-indexed-df.feather')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-ab48e8ccc89c> in <module>
----> 1 df.to_feather('time-indexed-df.feather')

~\Anaconda3\envs\sortino\lib\site-packages\pandas\core\frame.py in to_feather(self, fname)
   2129         """
   2130         from pandas.io.feather_format import to_feather
-> 2131         to_feather(self, fname)
   2132 
   2133     def to_parquet(self, fname, engine='auto', compression='snappy',

~\Anaconda3\envs\sortino\lib\site-packages\pandas\io\feather_format.py in to_feather(df, path)
     65 
     66     if not df.index.equals(RangeIndex.from_range(range(len(df)))):
---> 67         raise ValueError("feather does not support serializing a "
     68                          "non-default index for the index; you "
     69                          "can .reset_index() to make the index "

ValueError: feather does not support serializing a non-default index for the index; you can .reset_index() to make the index into column(s)
In [4]:
# what does `reset_index()` do?
df.reset_index().tail()
Out[4]:
index column_0 column_1 column_2
95 2019-04-06 -0.455504 -0.455504 -0.455504
96 2019-04-07 -2.018253 -2.018253 -2.018253
97 2019-04-08 1.305361 1.305361 1.305361
98 2019-04-09 -0.290746 -0.290746 -0.290746
99 2019-04-10 1.679213 1.679213 1.679213
In [5]:
# reset index and write to feather in one step
df.reset_index().to_feather('time-indexed-df.feather')

Success!!

In [6]:
# read it back in
df = pd.read_feather('time-indexed-df.feather')
df.set_index('index', inplace=True)
df.tail()
Out[6]:
column_0 column_1 column_2
index
2019-04-06 -0.455504 -0.455504 -0.455504
2019-04-07 -2.018253 -2.018253 -2.018253
2019-04-08 1.305361 1.305361 1.305361
2019-04-09 -0.290746 -0.290746 -0.290746
2019-04-10 1.679213 1.679213 1.679213
In [7]:
# ...or in one step:
df = pd.read_feather('time-indexed-df.feather').set_index('index')
df.tail()
Out[7]:
column_0 column_1 column_2
index
2019-04-06 -0.455504 -0.455504 -0.455504
2019-04-07 -2.018253 -2.018253 -2.018253
2019-04-08 1.305361 1.305361 1.305361
2019-04-09 -0.290746 -0.290746 -0.290746
2019-04-10 1.679213 1.679213 1.679213