Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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`)
2 changes: 1 addition & 1 deletion pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


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 @@ -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))

Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a Timedelta column as well.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine, though result.index.name = 'dt_index' as well

result.index = result.index.rename('dt_index')
result['dt_data'] = pd.to_timedelta(result['dt_data'])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set the index name for now so this issue is independent

assert_frame_equal(df, result, check_index_type=True)

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