Skip to content

Commit 610568c

Browse files
authored
REF: remove replace_list kludge (pandas-dev#33445)
1 parent df68cce commit 610568c

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

pandas/core/internals/managers.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77

88
import numpy as np
99

10-
from pandas._libs import Timedelta, Timestamp, internals as libinternals, lib
10+
from pandas._libs import internals as libinternals, lib
1111
from pandas._typing import ArrayLike, DtypeObj, Label, Scalar
1212
from pandas.util._validators import validate_bool_kwarg
1313

1414
from pandas.core.dtypes.cast import (
1515
find_common_type,
1616
infer_dtype_from_scalar,
17-
maybe_convert_objects,
1817
maybe_promote,
1918
)
2019
from pandas.core.dtypes.common import (
@@ -33,6 +32,7 @@
3332
import pandas.core.algorithms as algos
3433
from pandas.core.arrays.sparse import SparseDtype
3534
from pandas.core.base import PandasObject
35+
import pandas.core.common as com
3636
from pandas.core.construction import extract_array
3737
from pandas.core.indexers import maybe_convert_indices
3838
from pandas.core.indexes.api import Index, ensure_index
@@ -626,11 +626,8 @@ def comp(s, regex=False):
626626
"""
627627
if isna(s):
628628
return isna(values)
629-
if isinstance(s, (Timedelta, Timestamp)) and getattr(s, "tz", None) is None:
630629

631-
return _compare_or_regex_search(
632-
maybe_convert_objects(values), s.asm8, regex
633-
)
630+
s = com.maybe_box_datetimelike(s)
634631
return _compare_or_regex_search(values, s, regex)
635632

636633
masks = [comp(s, regex) for s in src_list]
@@ -643,11 +640,10 @@ def comp(s, regex=False):
643640
# replace ALWAYS will return a list
644641
rb = [blk if inplace else blk.copy()]
645642
for i, (s, d) in enumerate(zip(src_list, dest_list)):
646-
# TODO: assert/validate that `d` is always a scalar?
647643
new_rb: List[Block] = []
648644
for b in rb:
649645
m = masks[i][b.mgr_locs.indexer]
650-
convert = i == src_len
646+
convert = i == src_len # only convert once at the end
651647
result = b._replace_coerce(
652648
mask=m,
653649
to_replace=s,

pandas/tests/series/methods/test_replace.py

+10
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ def test_replace_gh5319(self):
108108
expected = pd.Series([pd.Timestamp.min, ts], dtype=object)
109109
tm.assert_series_equal(expected, result)
110110

111+
def test_replace_timedelta_td64(self):
112+
tdi = pd.timedelta_range(0, periods=5)
113+
ser = pd.Series(tdi)
114+
115+
# Using a single dict argument means we go through replace_list
116+
result = ser.replace({ser[1]: ser[3]})
117+
118+
expected = pd.Series([ser[0], ser[3], ser[2], ser[3], ser[4]])
119+
tm.assert_series_equal(result, expected)
120+
111121
def test_replace_with_single_list(self):
112122
ser = pd.Series([0, 1, 2, 3, 4])
113123
result = ser.replace([1, 2, 3])

0 commit comments

Comments
 (0)