Skip to content

Backport PR #27720 on branch 0.25.x (BUG: fix replace_list) #27753

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
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ ExtensionArray

Other
^^^^^

- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` when replacing timezone-aware timestamps using a dict-like replacer (:issue:`27720`)
-
-
-
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6684,9 +6684,8 @@ def replace(
else:

# need a non-zero len on all axes
for a in self._AXIS_ORDERS:
if not len(self._get_axis(a)):
return self
if not self.size:
return self

new_data = self._data
if is_dict_like(to_replace):
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import numpy as np

from pandas._libs import internals as libinternals, lib
from pandas._libs import Timedelta, Timestamp, internals as libinternals, lib
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.cast import (
Expand Down Expand Up @@ -602,9 +602,10 @@ def comp(s, regex=False):
"""
if isna(s):
return isna(values)
if hasattr(s, "asm8"):
if isinstance(s, (Timedelta, Timestamp)) and getattr(s, "tz", None) is None:

return _compare_or_regex_search(
maybe_convert_objects(values), getattr(s, "asm8"), regex
maybe_convert_objects(values), s.asm8, regex
)
return _compare_or_regex_search(values, s, regex)

Expand Down
47 changes: 19 additions & 28 deletions pandas/tests/indexing/test_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,22 +1029,20 @@ def test_replace_series(self, how, to_key, from_key):

tm.assert_series_equal(result, exp)

# TODO(jbrockmendel) commented out to only have a single xfail printed
@pytest.mark.xfail(
reason="GH #18376, tzawareness-compat bug in BlockManager.replace_list"
@pytest.mark.parametrize("how", ["dict", "series"])
@pytest.mark.parametrize(
"to_key",
["timedelta64[ns]", "bool", "object", "complex128", "float64", "int64"],
)
# @pytest.mark.parametrize('how', ['dict', 'series'])
# @pytest.mark.parametrize('to_key', ['timedelta64[ns]', 'bool', 'object',
# 'complex128', 'float64', 'int64'])
# @pytest.mark.parametrize('from_key', ['datetime64[ns, UTC]',
# 'datetime64[ns, US/Eastern]'])
# def test_replace_series_datetime_tz(self, how, to_key, from_key):
def test_replace_series_datetime_tz(self):
@pytest.mark.parametrize(
"from_key", ["datetime64[ns, UTC]", "datetime64[ns, US/Eastern]"]
)
def test_replace_series_datetime_tz(self, how, to_key, from_key):
how = "series"
from_key = "datetime64[ns, US/Eastern]"
to_key = "timedelta64[ns]"

index = pd.Index([3, 4], name="xxx")
index = pd.Index([3, 4], name="xyz")
obj = pd.Series(self.rep[from_key], index=index, name="yyy")
assert obj.dtype == from_key

Expand All @@ -1061,24 +1059,17 @@ def test_replace_series_datetime_tz(self):

tm.assert_series_equal(result, exp)

# TODO(jreback) commented out to only have a single xfail printed
@pytest.mark.xfail(
reason="different tz, currently mask_missing raises SystemError", strict=False
@pytest.mark.parametrize("how", ["dict", "series"])
@pytest.mark.parametrize(
"to_key",
["datetime64[ns]", "datetime64[ns, UTC]", "datetime64[ns, US/Eastern]"],
)
# @pytest.mark.parametrize('how', ['dict', 'series'])
# @pytest.mark.parametrize('to_key', [
# 'datetime64[ns]', 'datetime64[ns, UTC]',
# 'datetime64[ns, US/Eastern]'])
# @pytest.mark.parametrize('from_key', [
# 'datetime64[ns]', 'datetime64[ns, UTC]',
# 'datetime64[ns, US/Eastern]'])
# def test_replace_series_datetime_datetime(self, how, to_key, from_key):
def test_replace_series_datetime_datetime(self):
how = "dict"
to_key = "datetime64[ns]"
from_key = "datetime64[ns]"

index = pd.Index([3, 4], name="xxx")
@pytest.mark.parametrize(
"from_key",
["datetime64[ns]", "datetime64[ns, UTC]", "datetime64[ns, US/Eastern]"],
)
def test_replace_series_datetime_datetime(self, how, to_key, from_key):
index = pd.Index([3, 4], name="xyz")
obj = pd.Series(self.rep[from_key], index=index, name="yyy")
assert obj.dtype == from_key

Expand Down