Skip to content

BUG: DataFrame/Series.tz_convert does not modifies original data with copy=False #24657

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 2 commits into from
Jan 8, 2019
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.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,7 @@ Timezones
- Bug in :func:`to_datetime` where ``utc=True`` was not respected when passing a :class:`Timestamp` (:issue:`24415`)
- Bug in :meth:`DataFrame.any` returns wrong value when ``axis=1`` and the data is of datetimelike type (:issue:`23070`)
- Bug in :meth:`DatetimeIndex.to_period` where a timezone aware index was converted to UTC first before creating :class:`PeriodIndex` (:issue:`22905`)
- Bug in :meth:`DataFrame.tz_localize`, :meth:`DataFrame.tz_convert`, :meth:`Series.tz_localize`, and :meth:`Series.tz_convert` where ``copy=False`` would mutate the original argument inplace (:issue:`6326`)

Offsets
^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9226,7 +9226,7 @@ def _tz_convert(ax, tz):
ax = _tz_convert(ax, tz)

result = self._constructor(self._data, copy=copy)
result.set_axis(ax, axis=axis, inplace=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

this should really have the inplace arg rather than copy as that is the meaning here.

Copy link
Contributor

Choose a reason for hiding this comment

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

not sure if we should change this api though as don’t really love inplace generally

result = result.set_axis(ax, axis=axis, inplace=False)
return result.__finalize__(self)

def tz_localize(self, tz, axis=0, level=None, copy=True,
Expand Down Expand Up @@ -9390,7 +9390,7 @@ def _tz_localize(ax, tz, ambiguous, nonexistent):
ax = _tz_localize(ax, tz, ambiguous, nonexistent)

result = self._constructor(self._data, copy=copy)
result.set_axis(ax, axis=axis, inplace=True)
result = result.set_axis(ax, axis=axis, inplace=False)
return result.__finalize__(self)

# ----------------------------------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/frame/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,19 @@ def test_boolean_compare_transpose_tzindex_with_dst(self, tz):
result = df.T == df.T
expected = DataFrame(True, index=list('ab'), columns=idx)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize('copy', [True, False])
@pytest.mark.parametrize('method, tz', [
['tz_localize', None],
['tz_convert', 'Europe/Berlin']
])
def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz):
# GH 6326
result = DataFrame(np.arange(0, 5),
index=date_range('20131027', periods=5,
freq='1H', tz=tz))
getattr(result, method)('UTC', copy=copy)
expected = DataFrame(np.arange(0, 5),
index=date_range('20131027', periods=5,
freq='1H', tz=tz))
tm.assert_frame_equal(result, expected)
16 changes: 16 additions & 0 deletions pandas/tests/series/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,19 @@ def test_series_truncate_datetimeindex_tz(self):
result = s.truncate(datetime(2005, 4, 2), datetime(2005, 4, 4))
expected = Series([1, 2, 3], index=idx[1:4])
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize('copy', [True, False])
@pytest.mark.parametrize('method, tz', [
['tz_localize', None],
['tz_convert', 'Europe/Berlin']
])
def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz):
# GH 6326
result = Series(np.arange(0, 5),
index=date_range('20131027', periods=5, freq='1H',
tz=tz))
getattr(result, method)('UTC', copy=copy)
Copy link
Member

Choose a reason for hiding this comment

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

@mroeschke i'm confused by this; wouldn't we expect the tz in expected.index to be UTC, not tz

Copy link
Member Author

Choose a reason for hiding this comment

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

The naming of the test is a bit confusing, but this call here should not be an inplace operation.

I'm checking that the original data result was not modified after calling tz_localize/tz_convert with copy

#6326 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

ahh i see. ill update #37498 appropriately

expected = Series(np.arange(0, 5),
index=date_range('20131027', periods=5, freq='1H',
tz=tz))
tm.assert_series_equal(result, expected)