Skip to content

Commit

Permalink
Merge pull request #10845 from soupault/timedelta-bugfix
Browse files Browse the repository at this point in the history
BUG: Error while saving DataFrame with TimedeltaIndex to .csv #10833
  • Loading branch information
jreback committed Aug 21, 2015
2 parents a6ee127 + 2093cc5 commit 2c5b458
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,4 @@ Bug Fixes
- Bug in ``iloc`` allowing memory outside bounds of a Series to be accessed with negative integers (:issue:`10779`)
- Bug in ``read_msgpack`` where encoding is not respected (:issue:`10580`)
- Bug preventing access to the first index when using ``iloc`` with a list containing the appropriate negative integer (:issue:`10547`, :issue:`10779`)
- Bug in ``TimedeltaIndex`` formatter causing error while trying to save ``DataFrame`` with ``TimedeltaIndex`` using ``to_csv`` (:issue:`10833`)
2 changes: 1 addition & 1 deletion pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2190,7 +2190,7 @@ def __init__(self, values, nat_rep='NaT', box=False, **kwargs):
def _format_strings(self):
formatter = self.formatter or _get_format_timedelta64(self.values, nat_rep=self.nat_rep,
box=self.box)
fmt_values = [formatter(x) for x in self.values]
fmt_values = np.array([formatter(x) for x in self.values])
return fmt_values


Expand Down
17 changes: 16 additions & 1 deletion pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6312,7 +6312,6 @@ def test_to_csv_from_csv(self):
header=['AA', 'X'])

with ensure_clean(pname) as path:
import pandas as pd
df1 = DataFrame(np.random.randn(3, 1))
df2 = DataFrame(np.random.randn(3, 1))

Expand All @@ -6324,6 +6323,22 @@ def test_to_csv_from_csv(self):
xp.columns = lmap(int,xp.columns)
assert_frame_equal(xp,rs)

with ensure_clean() as path:
# GH 10833 (TimedeltaIndex formatting)
dt = pd.Timedelta(seconds=1)
df = pd.DataFrame({'dt_data': [i*dt for i in range(3)]},
index=pd.Index([i*dt for i in range(3)],
name='dt_index'))
df.to_csv(path)

result = pd.read_csv(path, index_col='dt_index')
result.index = pd.to_timedelta(result.index)
# TODO: remove renaming when GH 10875 is solved
result.index = result.index.rename('dt_index')
result['dt_data'] = pd.to_timedelta(result['dt_data'])

assert_frame_equal(df, result, check_index_type=True)

def test_to_csv_cols_reordering(self):
# GH3454
import pandas as pd
Expand Down

0 comments on commit 2c5b458

Please sign in to comment.