Skip to content

Commit

Permalink
BUG: fix replacer's dtypes not respected for frame replace (pandas-de…
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpanmj authored and proost committed Dec 19, 2019
1 parent 58808b0 commit b39f085
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ Reshaping
- Better error message in :func:`get_dummies` when `columns` isn't a list-like value (:issue:`28383`)
- Bug :meth:`Series.pct_change` where supplying an anchored frequency would throw a ValueError (:issue:`28664`)
- Bug where :meth:`DataFrame.equals` returned True incorrectly in some cases when two DataFrames had the same columns in different orders (:issue:`28839`)
- Bug in :meth:`DataFrame.replace` that caused non-numeric replacer's dtype not respected (:issue:`26632`)

Sparse
^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2825,6 +2825,8 @@ def _replace_coerce(
if convert:
block = [b.convert(numeric=False, copy=True) for b in block]
return block
if convert:
return [self.convert(numeric=False, copy=True)]
return self


Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def comp(s, regex=False):
convert=convert,
regex=regex,
)
if m.any():
if m.any() or convert:
new_rb = _extend_blocks(result, new_rb)
else:
new_rb.append(b)
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/frame/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,5 +1338,21 @@ def test_replace_commutative(self, df, to_replace, exp):

expected = pd.DataFrame(exp)
result = df.replace(to_replace)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"replacer",
[
pd.Timestamp("20170827"),
np.int8(1),
np.int16(1),
np.float32(1),
np.float64(1),
],
)
def test_replace_replacer_dtype(self, replacer):
# GH26632
df = pd.DataFrame(["a"])
result = df.replace({"a": replacer, "b": replacer})
expected = pd.DataFrame([replacer])
tm.assert_frame_equal(result, expected)

0 comments on commit b39f085

Please sign in to comment.