diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 1079ec52338b9..e41fe592a57ae 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -678,3 +678,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`) diff --git a/pandas/core/format.py b/pandas/core/format.py index 4ec4375349764..52f3c17ebdd26 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -2174,7 +2174,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 diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index a3a57929ad931..68d6ecf33e619 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -6308,7 +6308,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)) @@ -6320,6 +6319,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