Skip to content

Commit 3dd4974

Browse files
Backport PR #43199: BUG: convert_dtypes incorrectly converts byte strings to strings in 1.3+ (#44066)
1 parent a326408 commit 3dd4974

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
@@ -1419,7 +1419,7 @@ def convert_dtypes(
14191419
inferred_dtype = input_array.dtype
14201420

14211421
if is_string_dtype(inferred_dtype):
1422-
if not convert_string:
1422+
if not convert_string or inferred_dtype == "bytes":
14231423
return input_array.dtype
14241424
else:
14251425
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)