Skip to content

Commit bfcda0d

Browse files
committed
Simplify
1 parent 4d77b31 commit bfcda0d

File tree

2 files changed

+6
-20
lines changed

2 files changed

+6
-20
lines changed

pandas/core/dtypes/cast.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@
7070
from pandas.core.dtypes.inference import is_list_like
7171
from pandas.core.dtypes.missing import isna, notna
7272

73-
import pandas as pd
74-
7573
_int8_max = np.iinfo(np.int8).max
7674
_int16_max = np.iinfo(np.int16).max
7775
_int32_max = np.iinfo(np.int32).max
@@ -1051,7 +1049,8 @@ def convert_dtypes(
10511049
dtype
10521050
new dtype
10531051
"""
1054-
if convert_string or convert_integer or convert_boolean:
1052+
is_extension = is_extension_array_dtype(input_array.dtype)
1053+
if (convert_string or convert_integer or convert_boolean) and not is_extension:
10551054
try:
10561055
inferred_dtype = lib.infer_dtype(input_array)
10571056
except ValueError:
@@ -1064,9 +1063,7 @@ def convert_dtypes(
10641063
if convert_integer:
10651064
target_int_dtype = "Int64"
10661065

1067-
if is_integer_dtype(input_array.dtype) and not is_extension_array_dtype(
1068-
input_array.dtype
1069-
):
1066+
if is_integer_dtype(input_array.dtype):
10701067
from pandas.core.arrays.integer import _dtypes
10711068

10721069
inferred_dtype = _dtypes.get(input_array.dtype.name, target_int_dtype)
@@ -1080,10 +1077,7 @@ def convert_dtypes(
10801077
inferred_dtype = input_array.dtype
10811078

10821079
if convert_boolean:
1083-
if is_bool_dtype(input_array.dtype) and (
1084-
(not is_extension_array_dtype(input_array.dtype))
1085-
or (input_array.dtype == pd.BooleanDtype())
1086-
):
1080+
if is_bool_dtype(input_array.dtype):
10871081
inferred_dtype = "boolean"
10881082
else:
10891083
if isinstance(inferred_dtype, str) and inferred_dtype == "boolean":

pandas/tests/series/methods/test_convert_dtypes.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,5 @@ def test_convert_string_dtype(self):
282282

283283
def test_convert_bool_dtype(self):
284284
# GH32287
285-
df = pd.DataFrame([["abc", 123, True]])
286-
exp_dtypes = pd.Series([np.object, np.int64, np.bool])
287-
tm.assert_series_equal(df.dtypes, exp_dtypes)
288-
289-
df_1 = df.convert_dtypes()
290-
exp_dtypes_1 = pd.Series([pd.StringDtype(), pd.Int64Dtype(), pd.BooleanDtype()])
291-
tm.assert_series_equal(df_1.dtypes, exp_dtypes_1)
292-
293-
df_2 = df_1.convert_dtypes()
294-
tm.assert_series_equal(df_2.dtypes, exp_dtypes_1)
285+
df = pd.DataFrame({"A": pd.array([True])})
286+
tm.assert_frame_equal(df, df.convert_dtypes())

0 commit comments

Comments
 (0)