@@ -1122,20 +1122,29 @@ def convert_dtypes(
1122
1122
from pandas .core .arrays .arrow .dtype import ArrowDtype
1123
1123
from pandas .core .arrays .string_ import StringDtype
1124
1124
1125
- if isinstance (inferred_dtype , PandasExtensionDtype ):
1126
- base_dtype = inferred_dtype .base
1127
- elif isinstance (inferred_dtype , (BaseMaskedDtype , ArrowDtype )):
1128
- base_dtype = inferred_dtype .numpy_dtype
1129
- elif isinstance (inferred_dtype , StringDtype ):
1130
- base_dtype = np .dtype (str )
1131
- else :
1132
- # error: Incompatible types in assignment (expression has type
1133
- # "Union[str, Any, dtype[Any], ExtensionDtype]",
1134
- # variable has type "Union[dtype[Any], ExtensionDtype, None]")
1135
- base_dtype = inferred_dtype # type: ignore[assignment]
1136
- pa_type = to_pyarrow_type (base_dtype )
1137
- if pa_type is not None :
1138
- inferred_dtype = ArrowDtype (pa_type )
1125
+ assert not isinstance (inferred_dtype , str )
1126
+
1127
+ if (
1128
+ (convert_integer and inferred_dtype .kind in "iu" )
1129
+ or (convert_floating and inferred_dtype .kind in "fc" )
1130
+ or (convert_boolean and inferred_dtype .kind == "b" )
1131
+ or (convert_string and isinstance (inferred_dtype , StringDtype ))
1132
+ or (
1133
+ inferred_dtype .kind not in "iufcb"
1134
+ and not isinstance (inferred_dtype , StringDtype )
1135
+ )
1136
+ ):
1137
+ if isinstance (inferred_dtype , PandasExtensionDtype ):
1138
+ base_dtype = inferred_dtype .base
1139
+ elif isinstance (inferred_dtype , (BaseMaskedDtype , ArrowDtype )):
1140
+ base_dtype = inferred_dtype .numpy_dtype
1141
+ elif isinstance (inferred_dtype , StringDtype ):
1142
+ base_dtype = np .dtype (str )
1143
+ else :
1144
+ base_dtype = inferred_dtype
1145
+ pa_type = to_pyarrow_type (base_dtype )
1146
+ if pa_type is not None :
1147
+ inferred_dtype = ArrowDtype (pa_type )
1139
1148
1140
1149
# error: Incompatible return value type (got "Union[str, Union[dtype[Any],
1141
1150
# ExtensionDtype]]", expected "Union[dtype[Any], ExtensionDtype]")
0 commit comments