Skip to content

Commit 849bc7d

Browse files
committed
BUG: Bug in DataFrame.replace with a datetime64[ns, tz] and a non-compat to_replace #11326
1 parent 7e5b223 commit 849bc7d

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

doc/source/whatsnew/v0.17.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Bug Fixes
7474

7575
- Bug in tz-conversions with an ambiguous time and ``.dt`` accessors (:issue:`11295`)
7676
- Bug in comparisons of Series vs list-likes (:issue:`11339`)
77+
- Bug in ``DataFrame.replace`` with a ``datetime64[ns, tz]`` and a non-compat to_replace (:issue:`11326`)
78+
7779

7880
- Bug in list-like indexing with a mixed-integer Index (:issue:`11320`)
7981

pandas/core/internals.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,8 @@ def replace(self, to_replace, value, inplace=False, filter=None,
565565
blocks here this is just a call to putmask. regex is not used here.
566566
It is used in ObjectBlocks. It is here for API
567567
compatibility."""
568-
mask = com.mask_missing(self.values, to_replace)
568+
values, to_replace = self._try_coerce_args(self.values, to_replace)
569+
mask = com.mask_missing(values, to_replace)
569570
if filter is not None:
570571
filtered_out = ~self.mgr_locs.isin(filter)
571572
mask[filtered_out.nonzero()[0]] = False
@@ -2110,7 +2111,7 @@ def _try_coerce_args(self, values, other):
21102111
if other.tz != self.values.tz:
21112112
raise ValueError("incompatible or non tz-aware value")
21122113
other = other.tz_localize(None).asi8
2113-
else:
2114+
elif isinstance(other, (np.datetime64, datetime)):
21142115
other = lib.Timestamp(other)
21152116
if not getattr(other, 'tz', None):
21162117
raise ValueError("incompatible or non tz-aware value")

pandas/tests/test_frame.py

+19
Original file line numberDiff line numberDiff line change
@@ -9953,6 +9953,25 @@ def test_replace_datetime(self):
99539953
result = df.replace(d)
99549954
tm.assert_frame_equal(result, expected)
99559955

9956+
def test_replace_datetimetz(self):
9957+
9958+
# GH 11326
9959+
# behaving poorly when presented with a datetime64[ns, tz]
9960+
df = DataFrame({'A' : date_range('20130101',periods=3,tz='US/Eastern'),
9961+
'B' : [0, np.nan, 2]})
9962+
result = df.replace(np.nan,1)
9963+
expected = DataFrame({'A' : date_range('20130101',periods=3,tz='US/Eastern'),
9964+
'B' : Series([0, 1, 2],dtype='float64')})
9965+
assert_frame_equal(result, expected)
9966+
9967+
result = df.fillna(1)
9968+
assert_frame_equal(result, expected)
9969+
9970+
result = df.replace(0,np.nan)
9971+
expected = DataFrame({'A' : date_range('20130101',periods=3,tz='US/Eastern'),
9972+
'B' : [np.nan, np.nan, 2]})
9973+
assert_frame_equal(result, expected)
9974+
99569975
def test_combine_multiple_frames_dtypes(self):
99579976

99589977
# GH 2759

0 commit comments

Comments
 (0)