Skip to content

Commit

Permalink
Backport PR #43199: BUG: convert_dtypes incorrectly converts byte str…
Browse files Browse the repository at this point in the history
…ings to strings in 1.3+ (#44066)
  • Loading branch information
meeseeksmachine committed Oct 17, 2021
1 parent a326408 commit 3dd4974
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression in :meth:`DataFrame.convert_dtypes` incorrectly converts byte strings to strings (:issue:`43183`)
- Fixed regression in :meth:`.GroupBy.agg` where it was failing silently with mixed data types along ``axis=1`` and :class:`MultiIndex` (:issue:`43209`)
- Fixed regression in :func:`merge` with integer and ``NaN`` keys failing with ``outer`` merge (:issue:`43550`)
- Fixed regression in :meth:`DataFrame.corr` raising ``ValueError`` with ``method="spearman"`` on 32-bit platforms (:issue:`43588`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ def convert_dtypes(
inferred_dtype = input_array.dtype

if is_string_dtype(inferred_dtype):
if not convert_string:
if not convert_string or inferred_dtype == "bytes":
return input_array.dtype
else:
return pandas_dtype("string")
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/series/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,12 @@ def test_convert_bool_dtype(self):
# GH32287
df = pd.DataFrame({"A": pd.array([True])})
tm.assert_frame_equal(df, df.convert_dtypes())

def test_convert_byte_string_dtype(self):
# GH-43183
byte_str = b"binary-string"

df = pd.DataFrame(data={"A": byte_str}, index=[0])
result = df.convert_dtypes()
expected = df
tm.assert_frame_equal(result, expected)

0 comments on commit 3dd4974

Please sign in to comment.