From b5ad62c1d5127c45798cbafa199a4492884e8345 Mon Sep 17 00:00:00 2001 From: Egor Panfilov Date: Tue, 18 Aug 2015 22:38:49 +0300 Subject: [PATCH 1/4] BUG: Error while saving DataFrame with TimedeltaIndex to .csv #10833 --- pandas/core/format.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 8ac148996b14788b8fe499e35394b52c829b053c Mon Sep 17 00:00:00 2001 From: Egor Date: Thu, 20 Aug 2015 13:35:30 +0300 Subject: [PATCH 2/4] BUG: #10833 Added test and info to whatsnew --- doc/source/whatsnew/v0.17.0.txt | 1 + pandas/tests/test_frame.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) 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/tests/test_frame.py b/pandas/tests/test_frame.py index a3a57929ad931..e4085310cfcb6 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,20 @@ 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_orig = pd.DataFrame({'data': list(range(10))}, + index=[i*dt for i in range(10)]) + df_orig.index.rename('timestamp', inplace=True) + df_orig.to_csv(path) + + df_test = pd.read_csv(path, index_col='timestamp') + df_test.index = pd.to_timedelta(df_test.index) + df_test.index.rename('timestamp', inplace=True) + + self.assertTrue(df_test.equal(df_orig)) + def test_to_csv_cols_reordering(self): # GH3454 import pandas as pd From d39c4745361b6e95658bc7e8c68db3392e66b1bc Mon Sep 17 00:00:00 2001 From: Egor Date: Thu, 20 Aug 2015 18:19:31 +0300 Subject: [PATCH 3/4] BUG: #10833 Reworked test in accordance with the code style --- pandas/tests/test_frame.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index e4085310cfcb6..5d3dcdba6b30b 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -6322,16 +6322,16 @@ def test_to_csv_from_csv(self): with ensure_clean() as path: # GH 10833 (TimedeltaIndex formatting) dt = pd.Timedelta(seconds=1) - df_orig = pd.DataFrame({'data': list(range(10))}, - index=[i*dt for i in range(10)]) - df_orig.index.rename('timestamp', inplace=True) - df_orig.to_csv(path) + 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) - df_test = pd.read_csv(path, index_col='timestamp') - df_test.index = pd.to_timedelta(df_test.index) - df_test.index.rename('timestamp', inplace=True) + result = pd.read_csv(path, index_col='dt_index') + result.index = pd.to_timedelta(result.index) + result['dt_data'] = pd.to_timedelta(result['dt_data']) - self.assertTrue(df_test.equal(df_orig)) + assert_frame_equal(df, result, check_index_type=True) def test_to_csv_cols_reordering(self): # GH3454 From 2093cc51786015ee65f5985c5b96361b6b606254 Mon Sep 17 00:00:00 2001 From: Egor Panfilov Date: Fri, 21 Aug 2015 06:38:00 +0300 Subject: [PATCH 4/4] BUG: #10833 Added forced index rename --- pandas/tests/test_frame.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 5d3dcdba6b30b..68d6ecf33e619 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -6329,6 +6329,8 @@ def test_to_csv_from_csv(self): 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)