Skip to content

Commit 7fe3bad

Browse files
meeseeksmachinejreback
authored andcommitted
Backport PR #27720: BUG: fix replace_list (#27753)
1 parent 1f6bc37 commit 7fe3bad

File tree

4 files changed

+26
-35
lines changed

4 files changed

+26
-35
lines changed

doc/source/whatsnew/v0.25.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ ExtensionArray
152152

153153
Other
154154
^^^^^
155-
155+
- Bug in :meth:`Series.replace` and :meth:`DataFrame.replace` when replacing timezone-aware timestamps using a dict-like replacer (:issue:`27720`)
156156
-
157157
-
158158
-

pandas/core/generic.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -6684,9 +6684,8 @@ def replace(
66846684
else:
66856685

66866686
# need a non-zero len on all axes
6687-
for a in self._AXIS_ORDERS:
6688-
if not len(self._get_axis(a)):
6689-
return self
6687+
if not self.size:
6688+
return self
66906689

66916690
new_data = self._data
66926691
if is_dict_like(to_replace):

pandas/core/internals/managers.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import numpy as np
99

10-
from pandas._libs import internals as libinternals, lib
10+
from pandas._libs import Timedelta, Timestamp, internals as libinternals, lib
1111
from pandas.util._validators import validate_bool_kwarg
1212

1313
from pandas.core.dtypes.cast import (
@@ -602,9 +602,10 @@ def comp(s, regex=False):
602602
"""
603603
if isna(s):
604604
return isna(values)
605-
if hasattr(s, "asm8"):
605+
if isinstance(s, (Timedelta, Timestamp)) and getattr(s, "tz", None) is None:
606+
606607
return _compare_or_regex_search(
607-
maybe_convert_objects(values), getattr(s, "asm8"), regex
608+
maybe_convert_objects(values), s.asm8, regex
608609
)
609610
return _compare_or_regex_search(values, s, regex)
610611

pandas/tests/indexing/test_coercion.py

+19-28
Original file line numberDiff line numberDiff line change
@@ -1029,22 +1029,20 @@ def test_replace_series(self, how, to_key, from_key):
10291029

10301030
tm.assert_series_equal(result, exp)
10311031

1032-
# TODO(jbrockmendel) commented out to only have a single xfail printed
1033-
@pytest.mark.xfail(
1034-
reason="GH #18376, tzawareness-compat bug in BlockManager.replace_list"
1032+
@pytest.mark.parametrize("how", ["dict", "series"])
1033+
@pytest.mark.parametrize(
1034+
"to_key",
1035+
["timedelta64[ns]", "bool", "object", "complex128", "float64", "int64"],
10351036
)
1036-
# @pytest.mark.parametrize('how', ['dict', 'series'])
1037-
# @pytest.mark.parametrize('to_key', ['timedelta64[ns]', 'bool', 'object',
1038-
# 'complex128', 'float64', 'int64'])
1039-
# @pytest.mark.parametrize('from_key', ['datetime64[ns, UTC]',
1040-
# 'datetime64[ns, US/Eastern]'])
1041-
# def test_replace_series_datetime_tz(self, how, to_key, from_key):
1042-
def test_replace_series_datetime_tz(self):
1037+
@pytest.mark.parametrize(
1038+
"from_key", ["datetime64[ns, UTC]", "datetime64[ns, US/Eastern]"]
1039+
)
1040+
def test_replace_series_datetime_tz(self, how, to_key, from_key):
10431041
how = "series"
10441042
from_key = "datetime64[ns, US/Eastern]"
10451043
to_key = "timedelta64[ns]"
10461044

1047-
index = pd.Index([3, 4], name="xxx")
1045+
index = pd.Index([3, 4], name="xyz")
10481046
obj = pd.Series(self.rep[from_key], index=index, name="yyy")
10491047
assert obj.dtype == from_key
10501048

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

10621060
tm.assert_series_equal(result, exp)
10631061

1064-
# TODO(jreback) commented out to only have a single xfail printed
1065-
@pytest.mark.xfail(
1066-
reason="different tz, currently mask_missing raises SystemError", strict=False
1062+
@pytest.mark.parametrize("how", ["dict", "series"])
1063+
@pytest.mark.parametrize(
1064+
"to_key",
1065+
["datetime64[ns]", "datetime64[ns, UTC]", "datetime64[ns, US/Eastern]"],
10671066
)
1068-
# @pytest.mark.parametrize('how', ['dict', 'series'])
1069-
# @pytest.mark.parametrize('to_key', [
1070-
# 'datetime64[ns]', 'datetime64[ns, UTC]',
1071-
# 'datetime64[ns, US/Eastern]'])
1072-
# @pytest.mark.parametrize('from_key', [
1073-
# 'datetime64[ns]', 'datetime64[ns, UTC]',
1074-
# 'datetime64[ns, US/Eastern]'])
1075-
# def test_replace_series_datetime_datetime(self, how, to_key, from_key):
1076-
def test_replace_series_datetime_datetime(self):
1077-
how = "dict"
1078-
to_key = "datetime64[ns]"
1079-
from_key = "datetime64[ns]"
1080-
1081-
index = pd.Index([3, 4], name="xxx")
1067+
@pytest.mark.parametrize(
1068+
"from_key",
1069+
["datetime64[ns]", "datetime64[ns, UTC]", "datetime64[ns, US/Eastern]"],
1070+
)
1071+
def test_replace_series_datetime_datetime(self, how, to_key, from_key):
1072+
index = pd.Index([3, 4], name="xyz")
10821073
obj = pd.Series(self.rep[from_key], index=index, name="yyy")
10831074
assert obj.dtype == from_key
10841075

0 commit comments

Comments
 (0)