Skip to content

BUG: convert_dtypes not converting with pd.NA and object dtype #48857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ Conversion
^^^^^^^^^^
- Bug in constructing :class:`Series` with ``int64`` dtype from a string list raising instead of casting (:issue:`44923`)
- Bug in :meth:`DataFrame.eval` incorrectly raising an ``AttributeError`` when there are negative values in function call (:issue:`46471`)
- Bug in :meth:`Series.convert_dtypes` not converting dtype to nullable dtype when :class:`Series` contains ``NA`` and has dtype ``object`` (:issue:`48791`)
- Bug where any :class:`ExtensionDtype` subclass with ``kind="M"`` would be interpreted as a timezone type (:issue:`34986`)
-

Expand Down
13 changes: 13 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,7 @@ def convert_dtypes(
convert_integer: bool = True,
convert_boolean: bool = True,
convert_floating: bool = True,
infer_objects: bool = False,
) -> DtypeObj:
"""
Convert objects to best possible type, and optionally,
Expand Down Expand Up @@ -1139,6 +1140,12 @@ def convert_dtypes(
inferred_dtype = target_int_dtype
else:
inferred_dtype = input_array.dtype
elif (
infer_objects
and is_object_dtype(input_array.dtype)
and inferred_dtype == "integer"
):
inferred_dtype = target_int_dtype

if convert_floating:
if not is_integer_dtype(input_array.dtype) and is_numeric_dtype(
Expand All @@ -1160,6 +1167,12 @@ def convert_dtypes(
inferred_dtype = inferred_float_dtype
else:
inferred_dtype = inferred_float_dtype
elif (
infer_objects
and is_object_dtype(input_array.dtype)
and inferred_dtype == "mixed-integer-float"
):
inferred_dtype = pandas_dtype("Float64")

if convert_boolean:
if is_bool_dtype(input_array.dtype):
Expand Down
1 change: 1 addition & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5704,6 +5704,7 @@ def _convert_dtypes(
convert_integer,
convert_boolean,
convert_floating,
infer_objects,
)
result = input_series.astype(inferred_dtype)
else:
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/series/methods/test_convert_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,23 @@ def test_convert_byte_string_dtype(self):
result = df.convert_dtypes()
expected = df
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"infer_objects, dtype", [(True, "Int64"), (False, "object")]
)
def test_convert_dtype_object_with_na(self, infer_objects, dtype):
# GH#48791
ser = pd.Series([1, pd.NA])
result = ser.convert_dtypes(infer_objects=infer_objects)
expected = pd.Series([1, pd.NA], dtype=dtype)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"infer_objects, dtype", [(True, "Float64"), (False, "object")]
)
def test_convert_dtype_object_with_na_float(self, infer_objects, dtype):
# GH#48791
ser = pd.Series([1.5, pd.NA])
result = ser.convert_dtypes(infer_objects=infer_objects)
expected = pd.Series([1.5, pd.NA], dtype=dtype)
tm.assert_series_equal(result, expected)