Skip to content

ERR: Raise error in to_excel when saving datetimes with timezones #27129

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 5 commits into from
Jun 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ Other API changes
- The ``.str``-accessor has been disabled for 1-level :class:`MultiIndex`, use :meth:`MultiIndex.to_flat_index` if necessary (:issue:`23679`)
- Removed support of gtk package for clipboards (:issue:`26563`)
- Using an unsupported version of Beautiful Soup 4 will now raise an ``ImportError`` instead of a ``ValueError`` (:issue:`27063`)
- :meth:`Series.to_excel` and :meth:`DataFrame.to_excel` will now raise a ``ValueError`` when saving timezone aware data. (:issue:`27008`, :issue:`7056`)

.. _whatsnew_0250.deprecations:

Expand Down
4 changes: 4 additions & 0 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ def _format_value(self, val):
val = '-{inf}'.format(inf=self.inf_rep)
elif self.float_format is not None:
val = float(self.float_format % val)
if getattr(val, 'tzinfo', None) is not None:
raise ValueError('Excel does not support datetimes with timzones. '
Copy link
Member

Choose a reason for hiding this comment

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

small typo here for timezones if you want to fix locally (otherwise can do before merge)

'Please ensure that datetimes are timezone '
'unaware before writing to Excel.')
return val

def _format_header_mi(self):
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,21 @@ def test_merged_cell_custom_objects(self, engine, merge_cells, ext):
expected.index = expected.index.astype(np.float64)
tm.assert_frame_equal(expected, result)

@pytest.mark.parametrize('dtype', [None, object])
def test_raise_when_saving_timezones(self, engine, ext, dtype,
tz_aware_fixture):
# GH 27008, GH 7056
tz = tz_aware_fixture
data = pd.Timestamp('2019', tz=tz)
df = DataFrame([data], dtype=dtype)
with pytest.raises(ValueError, match="Excel does not support"):
df.to_excel(self.path, engine=engine)
Copy link
Member

Choose a reason for hiding this comment

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

Just call this without the engine argument - it's already set during test setup so this would be duplicative

That entire mechanism needs a refactor but separate exercise for #27096


data = data.to_pydatetime()
df = DataFrame([data], dtype=dtype)
with pytest.raises(ValueError, match="Excel does not support"):
df.to_excel(self.path, engine=engine)


class TestExcelWriterEngineTests:

Expand Down