diff --git a/doc/source/whatsnew/v1.0.2.rst b/doc/source/whatsnew/v1.0.2.rst index c9031ac1ae9fe..e0d2de634bf3d 100644 --- a/doc/source/whatsnew/v1.0.2.rst +++ b/doc/source/whatsnew/v1.0.2.rst @@ -45,6 +45,7 @@ Bug fixes - Fix bug in :meth:`DataFrame.convert_dtypes` for columns that were already using the ``"string"`` dtype (:issue:`31731`). - Fixed bug in setting values using a slice indexer with string dtype (:issue:`31772`) +- Fix bug in :meth:`Series.convert_dtypes` for series with mix of integers and strings (:issue:`32117`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 1c969d40c2c7f..c2b600b5d8c5b 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -1062,11 +1062,6 @@ def convert_dtypes( if convert_integer: target_int_dtype = "Int64" - if isinstance(inferred_dtype, str) and ( - inferred_dtype == "mixed-integer" - or inferred_dtype == "mixed-integer-float" - ): - inferred_dtype = target_int_dtype if is_integer_dtype(input_array.dtype) and not is_extension_array_dtype( input_array.dtype ): diff --git a/pandas/tests/series/methods/test_convert_dtypes.py b/pandas/tests/series/methods/test_convert_dtypes.py index a6b5fed40a9d7..17527a09f07a1 100644 --- a/pandas/tests/series/methods/test_convert_dtypes.py +++ b/pandas/tests/series/methods/test_convert_dtypes.py @@ -81,6 +81,18 @@ class TestSeriesConvertDtypes: ), }, ), + ( # GH32117 + ["h", "i", 1], + np.dtype("O"), + { + ( + (True, False), + (True, False), + (True, False), + (True, False), + ): np.dtype("O"), + }, + ), ( [10, np.nan, 20], np.dtype("float"), @@ -144,11 +156,23 @@ class TestSeriesConvertDtypes: [1, 2.0], object, { - ((True, False), (True, False), (True,), (True, False)): "Int64", + ((True,), (True, False), (True,), (True, False)): "Int64", ((True,), (True, False), (False,), (True, False)): np.dtype( "float" ), - ((False,), (True, False), (False,), (True, False)): np.dtype( + ((False,), (True, False), (True, False), (True, False)): np.dtype( + "object" + ), + }, + ), + ( + [1, 2.5], + object, + { + ((True,), (True, False), (True, False), (True, False)): np.dtype( + "float" + ), + ((False,), (True, False), (True, False), (True, False)): np.dtype( "object" ), },