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

Closed
soupault opened this Issue Aug 17, 2015 · 4 comments

Comments

Projects
None yet
2 participants
Contributor

soupault commented Aug 17, 2015

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)

Contributor

jreback commented Aug 17, 2015

seems a little buggy. I think you can just insert a np.array(.....,) here

want to do a pull-request ?

@jreback jreback added Bug CSV labels Aug 17, 2015

jreback added this to the 0.17.0 milestone Aug 17, 2015

Contributor

soupault commented Aug 17, 2015

@jreback yes, please. Give me a day.

Contributor

jreback commented Aug 17, 2015

awesome!

jreback added the Timedelta label Aug 18, 2015

@jreback jreback modified the milestone: Next Major Release, 0.17.0 Aug 19, 2015

soupault changed the title from Error while saving DataFrame with TimedeltaIndex to .csv to BUG: Error while saving DataFrame with TimedeltaIndex to .csv Aug 21, 2015

@jreback jreback added a commit that referenced this issue Aug 21, 2015

@jreback jreback Merge pull request #10845 from soupault/timedelta-bugfix
BUG: Error while saving DataFrame with TimedeltaIndex to .csv #10833
2c5b458
Contributor

jreback commented Aug 21, 2015

closed by #10845

jreback closed this Aug 21, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment