Skip to content

REF: convert_dtypes return dtype objects #41622

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 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
61 changes: 33 additions & 28 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ def convert_dtypes(
convert_integer: bool = True,
convert_boolean: bool = True,
convert_floating: bool = True,
) -> Dtype:
) -> DtypeObj:
"""
Convert objects to best possible type, and optionally,
to types supporting ``pd.NA``.
Expand All @@ -1407,71 +1407,76 @@ def convert_dtypes(

Returns
-------
str, np.dtype, or ExtensionDtype
dtype
new dtype
np.dtype, or ExtensionDtype
"""
inferred_dtype: str | np.dtype | ExtensionDtype
# TODO: rule out str
inferred_dtype: str | DtypeObj

if (
convert_string or convert_integer or convert_boolean or convert_floating
) and isinstance(input_array, np.ndarray):
inferred_dtype = lib.infer_dtype(input_array)

if not convert_string and is_string_dtype(inferred_dtype):
if is_object_dtype(input_array.dtype):
inferred_dtype = lib.infer_dtype(input_array)
else:
inferred_dtype = input_array.dtype

if is_string_dtype(inferred_dtype):
if not convert_string:
inferred_dtype = input_array.dtype
else:
inferred_dtype = pandas_dtype("string")
return inferred_dtype

if convert_integer:
target_int_dtype = "Int64"
target_int_dtype = pandas_dtype("Int64")

if is_integer_dtype(input_array.dtype):
from pandas.core.arrays.integer import INT_STR_TO_DTYPE

inferred_dtype = INT_STR_TO_DTYPE.get(
input_array.dtype.name, target_int_dtype
)
if not is_integer_dtype(input_array.dtype) and is_numeric_dtype(
input_array.dtype
):
inferred_dtype = target_int_dtype

else:
if is_integer_dtype(inferred_dtype):
inferred_dtype = input_array.dtype
elif is_numeric_dtype(input_array.dtype):
# TODO: de-dup with maybe_cast_to_integer_array?
arr = input_array[notna(input_array)]
if (arr.astype(int) == arr).all():
inferred_dtype = target_int_dtype
else:
inferred_dtype = input_array.dtype

if convert_floating:
if not is_integer_dtype(input_array.dtype) and is_numeric_dtype(
input_array.dtype
):
from pandas.core.arrays.floating import FLOAT_STR_TO_DTYPE

inferred_float_dtype = FLOAT_STR_TO_DTYPE.get(
input_array.dtype.name, "Float64"
inferred_float_dtype: DtypeObj = FLOAT_STR_TO_DTYPE.get(
input_array.dtype.name, pandas_dtype("Float64")
)
# if we could also convert to integer, check if all floats
# are actually integers
if convert_integer:
# TODO: de-dup with maybe_cast_to_integer_array?
arr = input_array[notna(input_array)]
if (arr.astype(int) == arr).all():
inferred_dtype = "Int64"
inferred_dtype = pandas_dtype("Int64")
else:
inferred_dtype = inferred_float_dtype
else:
inferred_dtype = inferred_float_dtype
else:
if is_float_dtype(inferred_dtype):
inferred_dtype = input_array.dtype

if convert_boolean:
if is_bool_dtype(input_array.dtype):
inferred_dtype = "boolean"
else:
if isinstance(inferred_dtype, str) and inferred_dtype == "boolean":
inferred_dtype = input_array.dtype
inferred_dtype = pandas_dtype("boolean")
elif isinstance(inferred_dtype, str) and inferred_dtype == "boolean":
inferred_dtype = pandas_dtype("boolean")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not adverse to combining conditions, black tends to make complex conditionals easy to grok imo. but nbd.


if isinstance(inferred_dtype, str):
# If we couldn't do anything else, then we retain the dtype
inferred_dtype = input_array.dtype

else:
inferred_dtype = input_array.dtype
return input_array.dtype

return inferred_dtype

Expand Down
5 changes: 1 addition & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5065,10 +5065,7 @@ def _convert_dtypes(
convert_boolean,
convert_floating,
)
try:
result = input_series.astype(inferred_dtype)
except TypeError:
result = input_series.copy()
result = input_series.astype(inferred_dtype)
else:
result = input_series.copy()
return result
Expand Down