BUG: Error while saving DataFrame with TimedeltaIndex to .csv · Issue #10833 · pandas-dev/pandas (original) (raw)

I'm trying to save pd.DataFrame using .to_csv method.
If DataFrame has TimedeltaIndex the error is risen:

>>> dt = pd.Timedelta(seconds=10)
>>> timestamps = [dt, 2*dt, 3*dt]
>>> df = pd.DataFrame({'obs': [11,22,33]}, index=timestamps)
>>> df
          obs
00:00:10   11
00:00:20   22
00:00:30   33

>>> df.to_csv('test')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-9b2e5ea53beb> in <module>()
----> 1 df.to_csv('test')

C:\Python34\lib\site-packages\pandas\core\frame.py in to_csv(self, path_or_buf,
sep, na_rep, float_format, columns, header, index, index_label, mode, encoding,
quoting, quotechar, line_terminator, chunksize, tupleize_cols, date_format, doub
lequote, escapechar, decimal, **kwds)
   1187                                      escapechar=escapechar,
   1188                                      decimal=decimal)
-> 1189         formatter.save()
   1190
   1191         if path_or_buf is None:

C:\Python34\lib\site-packages\pandas\core\format.py in save(self)
   1465
   1466             else:
-> 1467                 self._save()
   1468
   1469         finally:

C:\Python34\lib\site-packages\pandas\core\format.py in _save(self)
   1565                 break
   1566
-> 1567             self._save_chunk(start_i, end_i)
   1568
   1569     def _save_chunk(self, start_i, end_i):

C:\Python34\lib\site-packages\pandas\core\format.py in _save_chunk(self, start_i
, end_i)
   1592                                         quoting=self.quoting)
   1593
-> 1594         lib.write_csv_rows(self.data, ix, self.nlevels, self.cols, self.
writer)
   1595
   1596 # from collections import namedtuple

TypeError: Argument 'data_index' has incorrect type (expected numpy.ndarray, got
 list)
>>> df['timedelta'] = df.index
>>> df.reset_index(drop=True, inplace=True)
>>> df.to_csv('test')