Skip to content

Commit 21a651d

Browse files
phoflmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#39065: Regression in replace raising ValueError for bytes object
1 parent d53ff7a commit 21a651d

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

doc/source/whatsnew/v1.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Fixed regressions
2626
- Bug in :meth:`read_csv` with ``float_precision="high"`` caused segfault or wrong parsing of long exponent strings. This resulted in a regression in some cases as the default for ``float_precision`` was changed in pandas 1.2.0 (:issue:`38753`)
2727
- Fixed regression in :meth:`Rolling.skew` and :meth:`Rolling.kurt` modifying the object inplace (:issue:`38908`)
2828
- Fixed regression in :meth:`read_csv` and other read functions were the encoding error policy (``errors``) did not default to ``"replace"`` when no encoding was specified (:issue:`38989`)
29+
- Fixed regression in :meth:`DataFrame.replace` raising ValueError when :class:`DataFrame` has dtype ``bytes`` (:issue:`38900`)
2930

3031
.. ---------------------------------------------------------------------------
3132

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2482,7 +2482,7 @@ class ObjectBlock(Block):
24822482
_can_hold_na = True
24832483

24842484
def _maybe_coerce_values(self, values):
2485-
if issubclass(values.dtype.type, str):
2485+
if issubclass(values.dtype.type, (str, bytes)):
24862486
values = np.array(values, dtype=object)
24872487
return values
24882488

pandas/tests/frame/methods/test_replace.py

+7
Original file line numberDiff line numberDiff line change
@@ -1636,3 +1636,10 @@ def test_replace_unicode(self):
16361636
result = df1.replace(columns_values_map)
16371637
expected = DataFrame({"positive": np.ones(3)})
16381638
tm.assert_frame_equal(result, expected)
1639+
1640+
def test_replace_bytes(self, frame_or_series):
1641+
# GH#38900
1642+
obj = frame_or_series(["o"]).astype("|S")
1643+
expected = obj.copy()
1644+
obj = obj.replace({None: np.nan})
1645+
tm.assert_equal(obj, expected)

0 commit comments

Comments
 (0)