Skip to content

BUG: Error while saving DataFrame with TimedeltaIndex to .csv #10833

Closed
@soupault

Description

@soupault

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)

But if Timedelta is not an Index, but a regular column, everything is OK:

>>> df['timedelta'] = df.index
>>> df.reset_index(drop=True, inplace=True)
>>> df.to_csv('test')

Python 3.4.2 x64 Windows version.
pandas (0.16.2), numpy (1.9.2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions