Skip to content

Commit be406f3

Browse files
authored
BUG: DataFrame/Series.tz_convert does not modifies original data with copy=False (#24657)
* BUG: DataFrame/Series.tz_localize/convert with copy=False does not mutate argument inplace * Put back space
1 parent 1ae466c commit be406f3

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,7 @@ Timezones
15961596
- Bug in :func:`to_datetime` where ``utc=True`` was not respected when passing a :class:`Timestamp` (:issue:`24415`)
15971597
- Bug in :meth:`DataFrame.any` returns wrong value when ``axis=1`` and the data is of datetimelike type (:issue:`23070`)
15981598
- Bug in :meth:`DatetimeIndex.to_period` where a timezone aware index was converted to UTC first before creating :class:`PeriodIndex` (:issue:`22905`)
1599+
- 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`)
15991600

16001601
Offsets
16011602
^^^^^^^

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9226,7 +9226,7 @@ def _tz_convert(ax, tz):
92269226
ax = _tz_convert(ax, tz)
92279227

92289228
result = self._constructor(self._data, copy=copy)
9229-
result.set_axis(ax, axis=axis, inplace=True)
9229+
result = result.set_axis(ax, axis=axis, inplace=False)
92309230
return result.__finalize__(self)
92319231

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

93929392
result = self._constructor(self._data, copy=copy)
9393-
result.set_axis(ax, axis=axis, inplace=True)
9393+
result = result.set_axis(ax, axis=axis, inplace=False)
93949394
return result.__finalize__(self)
93959395

93969396
# ----------------------------------------------------------------------

pandas/tests/frame/test_timezones.py

+16
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,19 @@ def test_boolean_compare_transpose_tzindex_with_dst(self, tz):
180180
result = df.T == df.T
181181
expected = DataFrame(True, index=list('ab'), columns=idx)
182182
tm.assert_frame_equal(result, expected)
183+
184+
@pytest.mark.parametrize('copy', [True, False])
185+
@pytest.mark.parametrize('method, tz', [
186+
['tz_localize', None],
187+
['tz_convert', 'Europe/Berlin']
188+
])
189+
def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz):
190+
# GH 6326
191+
result = DataFrame(np.arange(0, 5),
192+
index=date_range('20131027', periods=5,
193+
freq='1H', tz=tz))
194+
getattr(result, method)('UTC', copy=copy)
195+
expected = DataFrame(np.arange(0, 5),
196+
index=date_range('20131027', periods=5,
197+
freq='1H', tz=tz))
198+
tm.assert_frame_equal(result, expected)

pandas/tests/series/test_timezones.py

+16
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,19 @@ def test_series_truncate_datetimeindex_tz(self):
348348
result = s.truncate(datetime(2005, 4, 2), datetime(2005, 4, 4))
349349
expected = Series([1, 2, 3], index=idx[1:4])
350350
tm.assert_series_equal(result, expected)
351+
352+
@pytest.mark.parametrize('copy', [True, False])
353+
@pytest.mark.parametrize('method, tz', [
354+
['tz_localize', None],
355+
['tz_convert', 'Europe/Berlin']
356+
])
357+
def test_tz_localize_convert_copy_inplace_mutate(self, copy, method, tz):
358+
# GH 6326
359+
result = Series(np.arange(0, 5),
360+
index=date_range('20131027', periods=5, freq='1H',
361+
tz=tz))
362+
getattr(result, method)('UTC', copy=copy)
363+
expected = Series(np.arange(0, 5),
364+
index=date_range('20131027', periods=5, freq='1H',
365+
tz=tz))
366+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)