Skip to content

Commit 3f1a734

Browse files
committed
BUG: Fix infer_dtype result for float with embedded pd.NA (#61621)
1 parent b8371f5 commit 3f1a734

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ Timezones
711711

712712
Numeric
713713
^^^^^^^
714+
- Bug in :func:`api.types.infer_dtype` returning "mixed-integer-float" for float and ``pd.NA`` mix (:issue:`61621`)
714715
- Bug in :meth:`DataFrame.corr` where numerical precision errors resulted in correlations above ``1.0`` (:issue:`61120`)
715716
- Bug in :meth:`DataFrame.cov` raises a ``TypeError`` instead of returning potentially incorrect results or other errors (:issue:`53115`)
716717
- Bug in :meth:`DataFrame.quantile` where the column type was not preserved when ``numeric_only=True`` with a list-like ``q`` produced an empty result (:issue:`59035`)

pandas/_libs/lib.pyx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,7 +1751,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str:
17511751
return "complex"
17521752

17531753
elif util.is_float_object(val):
1754-
if is_float_array(values):
1754+
if is_float_array(values, skipna=skipna):
17551755
return "floating"
17561756
elif is_integer_float_array(values, skipna=skipna):
17571757
if is_integer_na_array(values, skipna=skipna):
@@ -1953,9 +1953,11 @@ cdef class FloatValidator(Validator):
19531953

19541954

19551955
# Note: only python-exposed for tests
1956-
cpdef bint is_float_array(ndarray values):
1956+
cpdef bint is_float_array(ndarray values, bint skipna=True):
19571957
cdef:
1958-
FloatValidator validator = FloatValidator(values.size, values.dtype)
1958+
FloatValidator validator = FloatValidator(values.size,
1959+
values.dtype,
1960+
skipna=skipna)
19591961
return validator.validate(values)
19601962

19611963

pandas/tests/dtypes/test_inference.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,15 @@ def test_infer_dtype_period_with_na(self, na_value):
13871387
arr = np.array([na_value, Period("2011-01", freq="D"), na_value])
13881388
assert lib.infer_dtype(arr, skipna=True) == "period"
13891389

1390+
@pytest.mark.parametrize("na_value", [pd.NA, np.nan])
1391+
def test_infer_dtype_numeric_with_na(self, na_value):
1392+
# GH61621
1393+
arr = Series([1, 2, na_value], dtype=object)
1394+
assert lib.infer_dtype(arr, skipna=True) == "integer"
1395+
1396+
arr = Series([1.0, 2.0, na_value], dtype=object)
1397+
assert lib.infer_dtype(arr, skipna=True) == "floating"
1398+
13901399
def test_infer_dtype_all_nan_nat_like(self):
13911400
arr = np.array([np.nan, np.nan])
13921401
assert lib.infer_dtype(arr, skipna=True) == "floating"

0 commit comments

Comments
 (0)