Skip to content

Commit b5308ad

Browse files
committed
BUG: Regression in astype not casting to bytes (pandas-dev#39484)
(cherry picked from commit 300d1fc)
1 parent 4f271f0 commit b5308ad

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
@@ -1137,7 +1137,7 @@ def soft_convert_objects(
11371137
# bound of nanosecond-resolution 64-bit integers.
11381138
try:
11391139
values = lib.maybe_convert_objects(values, convert_datetime=True)
1140-
except OutOfBoundsDatetime:
1140+
except (OutOfBoundsDatetime, ValueError):
11411141
pass
11421142

11431143
if timedelta and is_object_dtype(values.dtype):

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2503,7 +2503,7 @@ class ObjectBlock(Block):
25032503
_can_hold_na = True
25042504

25052505
def _maybe_coerce_values(self, values):
2506-
if issubclass(values.dtype.type, (str, bytes)):
2506+
if issubclass(values.dtype.type, str):
25072507
values = np.array(values, dtype=object)
25082508
return values
25092509

pandas/tests/frame/methods/test_astype.py

+5
Original file line numberDiff line numberDiff line change
@@ -611,3 +611,8 @@ def test_astype_tz_object_conversion(self, tz):
611611
# do real test: object dtype to a specified tz, different from construction tz.
612612
result = result.astype({"tz": "datetime64[ns, Europe/London]"})
613613
tm.assert_frame_equal(result, expected)
614+
615+
def test_astype_bytes(self):
616+
# GH#39474
617+
result = DataFrame(["foo", "bar", "baz"]).astype(bytes)
618+
assert result.dtypes[0] == np.dtype("S3")

pandas/tests/series/methods/test_astype.py

+5
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ def test_astype_unicode(self):
337337
reload(sys)
338338
sys.setdefaultencoding(former_encoding)
339339

340+
def test_astype_bytes(self):
341+
# GH#39474
342+
result = Series(["foo", "bar", "baz"]).astype(bytes)
343+
assert result.dtypes == np.dtype("S3")
344+
340345

341346
class TestAstypeCategorical:
342347
def test_astype_categorical_invalid_conversions(self):

0 commit comments

Comments
 (0)