Skip to content

Commit 300d1fc

Browse files
authored
BUG: Regression in astype not casting to bytes (pandas-dev#39484)
1 parent b554bb3 commit 300d1fc

File tree

5 files changed

+13
-2
lines changed

5 files changed

+13
-2
lines changed

doc/source/whatsnew/v1.2.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed regressions
1717

1818
- Fixed regression in :func:`read_excel` that caused it to raise ``AttributeError`` when checking version of older xlrd versions (:issue:`38955`)
1919
- Fixed regression in :class:`DataFrame` constructor reordering element when construction from datetime ndarray with dtype not ``"datetime64[ns]"`` (:issue:`39422`)
20+
- Fixed regression in :class:`DataFrame.astype` and :class:`Series.astype` not casting to bytes dtype (:issue:`39474`)
2021
- Fixed regression in :meth:`~DataFrame.to_pickle` failing to create bz2/xz compressed pickle files with ``protocol=5`` (:issue:`39002`)
2122
- Fixed regression in :func:`pandas.testing.assert_series_equal` and :func:`pandas.testing.assert_frame_equal` always raising ``AssertionError`` when comparing extension dtypes (:issue:`39410`)
2223
- Fixed regression in :meth:`~DataFrame.to_csv` opening ``codecs.StreamWriter`` in binary mode instead of in text mode and ignoring user-provided ``mode`` (:issue:`39247`)

pandas/core/dtypes/cast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ def soft_convert_objects(
12211221
values = lib.maybe_convert_objects(
12221222
values, convert_datetime=datetime, convert_timedelta=timedelta
12231223
)
1224-
except OutOfBoundsDatetime:
1224+
except (OutOfBoundsDatetime, ValueError):
12251225
return values
12261226

12271227
if numeric and is_object_dtype(values.dtype):

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,7 @@ class ObjectBlock(Block):
22542254
_can_hold_na = True
22552255

22562256
def _maybe_coerce_values(self, values):
2257-
if issubclass(values.dtype.type, (str, bytes)):
2257+
if issubclass(values.dtype.type, str):
22582258
values = np.array(values, dtype=object)
22592259
return values
22602260

pandas/tests/frame/methods/test_astype.py

+5
Original file line numberDiff line numberDiff line change
@@ -651,3 +651,8 @@ def test_astype_dt64_to_string(self, frame_or_series, tz_naive_fixture, request)
651651
# For non-NA values, we should match what we get for non-EA str
652652
alt = obj.astype(str)
653653
assert np.all(alt.iloc[1:] == result.iloc[1:])
654+
655+
def test_astype_bytes(self):
656+
# GH#39474
657+
result = DataFrame(["foo", "bar", "baz"]).astype(bytes)
658+
assert result.dtypes[0] == np.dtype("S3")

pandas/tests/series/methods/test_astype.py

+5
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ def test_astype_unicode(self):
341341
reload(sys)
342342
sys.setdefaultencoding(former_encoding)
343343

344+
def test_astype_bytes(self):
345+
# GH#39474
346+
result = Series(["foo", "bar", "baz"]).astype(bytes)
347+
assert result.dtypes == np.dtype("S3")
348+
344349

345350
class TestAstypeCategorical:
346351
def test_astype_categorical_invalid_conversions(self):

0 commit comments

Comments
 (0)