Skip to content

ENH: Add lazy copy for tz_convert and tz_localize #50490

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 1 commit into from
Jan 3, 2023
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
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10099,7 +10099,7 @@ def truncate(
@final
@doc(klass=_shared_doc_kwargs["klass"])
def tz_convert(
self: NDFrameT, tz, axis: Axis = 0, level=None, copy: bool_t = True
self: NDFrameT, tz, axis: Axis = 0, level=None, copy: bool_t | None = None
) -> NDFrameT:
"""
Convert tz-aware axis to target time zone.
Expand Down Expand Up @@ -10181,7 +10181,7 @@ def tz_localize(
tz,
axis: Axis = 0,
level=None,
copy: bool_t = True,
copy: bool_t | None = None,
ambiguous: TimeAmbiguous = "raise",
nonexistent: TimeNonexistent = "raise",
) -> NDFrameT:
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
DataFrame,
MultiIndex,
Series,
date_range,
)
import pandas._testing as tm
from pandas.tests.copy_view.util import get_array
Expand Down Expand Up @@ -441,6 +442,28 @@ def test_frame_set_axis(using_copy_on_write):
tm.assert_frame_equal(df, df_orig)


@pytest.mark.parametrize(
"func, tz", [("tz_convert", "Europe/Berlin"), ("tz_localize", None)]
)
def test_tz_convert_localize(using_copy_on_write, func, tz):
# GH 49473
ser = Series(
[1, 2], index=date_range(start="2014-08-01 09:00", freq="H", periods=2, tz=tz)
)
ser_orig = ser.copy()
ser2 = getattr(ser, func)("US/Central")

if using_copy_on_write:
assert np.shares_memory(ser.values, ser2.values)
else:
assert not np.shares_memory(ser.values, ser2.values)

# mutating ser triggers a copy-on-write for the column / block
ser2.iloc[0] = 0
assert not np.shares_memory(ser2.values, ser.values)
tm.assert_series_equal(ser, ser_orig)


def test_series_set_axis(using_copy_on_write):
# GH 49473
ser = Series([1, 2, 3])
Expand Down