Skip to content

Commit 3eeef2a

Browse files
BUG: convert_dtypes incorrectly converts byte strings to strings in 1.3+ (#43199)
1 parent 776329f commit 3eeef2a

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v1.3.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17+
- Fixed regression in :meth:`DataFrame.convert_dtypes` incorrectly converts byte strings to strings (:issue:`43183`)
1718
- Fixed regression in :meth:`.GroupBy.agg` where it was failing silently with mixed data types along ``axis=1`` and :class:`MultiIndex` (:issue:`43209`)
1819
- Fixed regression in :func:`merge` with integer and ``NaN`` keys failing with ``outer`` merge (:issue:`43550`)
1920
- Fixed regression in :meth:`DataFrame.corr` raising ``ValueError`` with ``method="spearman"`` on 32-bit platforms (:issue:`43588`)

pandas/core/dtypes/cast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,7 @@ def convert_dtypes(
14241424
inferred_dtype = input_array.dtype
14251425

14261426
if is_string_dtype(inferred_dtype):
1427-
if not convert_string:
1427+
if not convert_string or inferred_dtype == "bytes":
14281428
return input_array.dtype
14291429
else:
14301430
return pandas_dtype("string")

pandas/tests/series/methods/test_convert_dtypes.py

+9
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,12 @@ def test_convert_bool_dtype(self):
226226
# GH32287
227227
df = pd.DataFrame({"A": pd.array([True])})
228228
tm.assert_frame_equal(df, df.convert_dtypes())
229+
230+
def test_convert_byte_string_dtype(self):
231+
# GH-43183
232+
byte_str = b"binary-string"
233+
234+
df = pd.DataFrame(data={"A": byte_str}, index=[0])
235+
result = df.convert_dtypes()
236+
expected = df
237+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)