Skip to content

Commit f00a833

Browse files
jbrockmendelYi Wei
authored and
Yi Wei
committed
CLN: avoid infer_dtype (pandas-dev#53063)
1 parent 152fd86 commit f00a833

File tree

4 files changed

+14
-34
lines changed

4 files changed

+14
-34
lines changed

pandas/core/arrays/masked.py

+7-15
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@
7575
from pandas.core.array_algos.quantile import quantile_with_mask
7676
from pandas.core.arraylike import OpsMixin
7777
from pandas.core.arrays.base import ExtensionArray
78-
from pandas.core.construction import ensure_wrapped_if_datetimelike
78+
from pandas.core.construction import (
79+
array as pd_array,
80+
ensure_wrapped_if_datetimelike,
81+
extract_array,
82+
)
7983
from pandas.core.indexers import check_array_indexer
8084
from pandas.core.ops import invalid_comparison
8185

@@ -645,20 +649,8 @@ def _arith_method(self, other, op):
645649
and len(other) == len(self)
646650
):
647651
# Try inferring masked dtype instead of casting to object
648-
inferred_dtype = lib.infer_dtype(other, skipna=True)
649-
if inferred_dtype == "integer":
650-
from pandas.core.arrays import IntegerArray
651-
652-
other = IntegerArray._from_sequence(other)
653-
elif inferred_dtype in ["floating", "mixed-integer-float"]:
654-
from pandas.core.arrays import FloatingArray
655-
656-
other = FloatingArray._from_sequence(other)
657-
658-
elif inferred_dtype in ["boolean"]:
659-
from pandas.core.arrays import BooleanArray
660-
661-
other = BooleanArray._from_sequence(other)
652+
other = pd_array(other)
653+
other = extract_array(other, extract_numpy=True)
662654

663655
if isinstance(other, BaseMaskedArray):
664656
other, omask = other._data, other._mask

pandas/core/dtypes/common.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1654,9 +1654,8 @@ def is_all_strings(value: ArrayLike) -> bool:
16541654
dtype = value.dtype
16551655

16561656
if isinstance(dtype, np.dtype):
1657-
return (
1658-
dtype == np.dtype("object")
1659-
and lib.infer_dtype(value, skipna=False) == "string"
1657+
return dtype == np.dtype("object") and lib.is_string_array(
1658+
np.asarray(value), skipna=False
16601659
)
16611660
elif isinstance(dtype, CategoricalDtype):
16621661
return dtype.categories.inferred_type == "string"

pandas/core/internals/construction.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
)
2828
from pandas.core.dtypes.common import (
2929
is_1d_only_ea_dtype,
30-
is_bool_dtype,
31-
is_float_dtype,
3230
is_integer_dtype,
3331
is_list_like,
3432
is_named_tuple,
@@ -44,14 +42,10 @@
4442
algorithms,
4543
common as com,
4644
)
47-
from pandas.core.arrays import (
48-
BooleanArray,
49-
ExtensionArray,
50-
FloatingArray,
51-
IntegerArray,
52-
)
45+
from pandas.core.arrays import ExtensionArray
5346
from pandas.core.arrays.string_ import StringDtype
5447
from pandas.core.construction import (
48+
array as pd_array,
5549
ensure_wrapped_if_datetimelike,
5650
extract_array,
5751
range_to_ndarray,
@@ -1027,12 +1021,8 @@ def convert(arr):
10271021
if dtype_backend != "numpy" and arr.dtype == np.dtype("O"):
10281022
arr = StringDtype().construct_array_type()._from_sequence(arr)
10291023
elif dtype_backend != "numpy" and isinstance(arr, np.ndarray):
1030-
if is_integer_dtype(arr.dtype):
1031-
arr = IntegerArray(arr, np.zeros(arr.shape, dtype=np.bool_))
1032-
elif is_bool_dtype(arr.dtype):
1033-
arr = BooleanArray(arr, np.zeros(arr.shape, dtype=np.bool_))
1034-
elif is_float_dtype(arr.dtype):
1035-
arr = FloatingArray(arr, np.isnan(arr))
1024+
if arr.dtype.kind in "iufb":
1025+
arr = pd_array(arr, copy=False)
10361026

10371027
elif isinstance(dtype, ExtensionDtype):
10381028
# TODO: test(s) that get here

pandas/io/parsers/base_parser.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,7 @@ def _infer_types(
757757
result = BooleanArray(result, bool_mask)
758758
elif result.dtype == np.object_ and non_default_dtype_backend:
759759
# read_excel sends array of datetime objects
760-
inferred_type = lib.infer_dtype(result)
761-
if inferred_type != "datetime":
760+
if not lib.is_datetime_array(result, skipna=True):
762761
result = StringDtype().construct_array_type()._from_sequence(values)
763762

764763
if dtype_backend == "pyarrow":

0 commit comments

Comments
 (0)