diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 13da3df93af14..99777bc2f169e 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -54,7 +54,7 @@ ) from pandas.core.dtypes.dtypes import CategoricalDtype from pandas.core.dtypes.generic import ABCIndex, ABCSeries -from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna +from pandas.core.dtypes.missing import is_valid_na_for_dtype, isna, notna from pandas.core import ops from pandas.core.accessor import PandasDelegate, delegate_names @@ -1284,7 +1284,7 @@ def _validate_fill_value(self, fill_value): TypeError """ - if is_valid_nat_for_dtype(fill_value, self.categories.dtype): + if is_valid_na_for_dtype(fill_value, self.categories.dtype): fill_value = -1 elif fill_value in self.categories: fill_value = self._unbox_scalar(fill_value) @@ -1779,7 +1779,7 @@ def __contains__(self, key) -> bool: Returns True if `key` is in this Categorical. """ # if key is a NaN, check if any NaN is in self. - if is_valid_nat_for_dtype(key, self.categories.dtype): + if is_valid_na_for_dtype(key, self.categories.dtype): return self.isna().any() return contains(self, key, container=self._codes) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 5ee7a5715d6af..48df98930244a 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -57,7 +57,7 @@ is_unsigned_integer_dtype, pandas_dtype, ) -from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna +from pandas.core.dtypes.missing import is_valid_na_for_dtype, isna from pandas.core import nanops, ops from pandas.core.algorithms import checked_add_with_arr, isin, unique1d @@ -493,7 +493,7 @@ def _validate_fill_value(self, fill_value): def _validate_shift_value(self, fill_value): # TODO(2.0): once this deprecation is enforced, use _validate_fill_value - if is_valid_nat_for_dtype(fill_value, self.dtype): + if is_valid_na_for_dtype(fill_value, self.dtype): fill_value = NaT elif isinstance(fill_value, self._recognized_scalars): # pandas\core\arrays\datetimelike.py:746: error: Too many arguments @@ -557,7 +557,7 @@ def _validate_scalar( msg = self._validation_error_message(value, allow_listlike) raise TypeError(msg) from err - elif is_valid_nat_for_dtype(value, self.dtype): + elif is_valid_na_for_dtype(value, self.dtype): # GH#18295 value = NaT diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index f4db68a2d7ac5..0f3e028c34c05 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -43,7 +43,7 @@ ABCPeriodIndex, ABCSeries, ) -from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna +from pandas.core.dtypes.missing import is_valid_na_for_dtype, isna, notna from pandas.core.algorithms import isin, take, value_counts from pandas.core.arrays.base import ExtensionArray, _extension_array_shared_docs @@ -979,7 +979,7 @@ def _validate_scalar(self, value): if isinstance(value, Interval): self._check_closed_matches(value, name="value") left, right = value.left, value.right - elif is_valid_nat_for_dtype(value, self.left.dtype): + elif is_valid_na_for_dtype(value, self.left.dtype): # GH#18295 left = right = value else: @@ -994,7 +994,7 @@ def _validate_fill_value(self, value): def _validate_setitem_value(self, value): needs_float_conversion = False - if is_valid_nat_for_dtype(value, self.left.dtype): + if is_valid_na_for_dtype(value, self.left.dtype): # na value: need special casing to set directly on numpy arrays if is_integer_dtype(self.dtype.subtype): # can't set NaN on a numpy integer array diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index ed36beb80986e..67e9aaa99debd 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -87,7 +87,7 @@ ABCSeries, ) from pandas.core.dtypes.inference import is_list_like -from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna +from pandas.core.dtypes.missing import is_valid_na_for_dtype, isna, notna if TYPE_CHECKING: from pandas import Series @@ -159,7 +159,7 @@ def maybe_unbox_datetimelike(value: Scalar, dtype: DtypeObj) -> Scalar: ----- Caller is responsible for checking dtype.kind in ["m", "M"] """ - if is_valid_nat_for_dtype(value, dtype): + if is_valid_na_for_dtype(value, dtype): # GH#36541: can't fill array directly with pd.NaT # > np.empty(10, dtype="datetime64[64]").fill(pd.NaT) # ValueError: cannot convert float NaN to integer @@ -535,7 +535,7 @@ def maybe_promote(dtype, fill_value=np.nan): dtype = np.dtype(np.object_) elif is_integer(fill_value) or (is_float(fill_value) and not isna(fill_value)): dtype = np.dtype(np.object_) - elif is_valid_nat_for_dtype(fill_value, dtype): + elif is_valid_na_for_dtype(fill_value, dtype): # e.g. pd.NA, which is not accepted by Timestamp constructor fill_value = np.datetime64("NaT", "ns") else: @@ -551,7 +551,7 @@ def maybe_promote(dtype, fill_value=np.nan): ): # TODO: What about str that can be a timedelta? dtype = np.dtype(np.object_) - elif is_valid_nat_for_dtype(fill_value, dtype): + elif is_valid_na_for_dtype(fill_value, dtype): # e.g pd.NA, which is not accepted by the Timedelta constructor fill_value = np.timedelta64("NaT", "ns") else: diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 0db0b1f6a97ef..ef645313de614 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -29,7 +29,6 @@ is_string_dtype, is_string_like_dtype, needs_i8_conversion, - pandas_dtype, ) from pandas.core.dtypes.generic import ( ABCDataFrame, @@ -535,7 +534,7 @@ def maybe_fill(arr, fill_value=np.nan): return arr -def na_value_for_dtype(dtype, compat: bool = True): +def na_value_for_dtype(dtype: DtypeObj, compat: bool = True): """ Return a dtype compat na value @@ -561,7 +560,6 @@ def na_value_for_dtype(dtype, compat: bool = True): >>> na_value_for_dtype(np.dtype('datetime64[ns]')) numpy.datetime64('NaT') """ - dtype = pandas_dtype(dtype) if is_extension_array_dtype(dtype): return dtype.na_value @@ -590,7 +588,7 @@ def remove_na_arraylike(arr): return arr[notna(np.asarray(arr))] -def is_valid_nat_for_dtype(obj, dtype: DtypeObj) -> bool: +def is_valid_na_for_dtype(obj, dtype: DtypeObj) -> bool: """ isna check that excludes incompatible dtypes diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 550c1ac906a99..c493bdea64d6b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -88,7 +88,7 @@ ABCTimedeltaIndex, ) from pandas.core.dtypes.inference import is_dict_like -from pandas.core.dtypes.missing import array_equivalent, is_valid_nat_for_dtype, isna +from pandas.core.dtypes.missing import array_equivalent, is_valid_na_for_dtype, isna from pandas.core import missing, ops from pandas.core.accessor import CachedAccessor @@ -5216,7 +5216,7 @@ def _find_common_type_compat(self, target) -> DtypeObj: Implementation of find_common_type that adjusts for Index-specific special cases. """ - if is_interval_dtype(self.dtype) and is_valid_nat_for_dtype(target, self.dtype): + if is_interval_dtype(self.dtype) and is_valid_na_for_dtype(target, self.dtype): # e.g. setting NA value into IntervalArray[int64] self = cast("IntervalIndex", self) return IntervalDtype(np.float64, closed=self.closed) @@ -5770,7 +5770,7 @@ def insert(self, loc: int, item): # Note: this method is overridden by all ExtensionIndex subclasses, # so self is never backed by an EA. item = lib.item_from_zerodim(item) - if is_valid_nat_for_dtype(item, self.dtype) and self.dtype != object: + if is_valid_na_for_dtype(item, self.dtype) and self.dtype != object: item = self._na_value try: diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 32da2c1cd1f3f..265170dd28a3b 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -15,7 +15,7 @@ is_categorical_dtype, is_scalar, ) -from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna, notna +from pandas.core.dtypes.missing import is_valid_na_for_dtype, isna, notna from pandas.core import accessor from pandas.core.arrays.categorical import Categorical, contains @@ -348,7 +348,7 @@ def inferred_type(self) -> str: @doc(Index.__contains__) def __contains__(self, key: Any) -> bool: # if key is a NaN, check if any NaN is in self. - if is_valid_nat_for_dtype(key, self.categories.dtype): + if is_valid_na_for_dtype(key, self.categories.dtype): return self.hasnans return contains(self, key, container=self._engine) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index e306698fe0202..2ef703de85dbe 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -26,7 +26,7 @@ is_datetime64tz_dtype, is_scalar, ) -from pandas.core.dtypes.missing import is_valid_nat_for_dtype +from pandas.core.dtypes.missing import is_valid_na_for_dtype from pandas.core.arrays.datetimes import DatetimeArray, tz_to_dtype import pandas.core.common as com @@ -636,7 +636,7 @@ def get_loc(self, key, method=None, tolerance=None): raise InvalidIndexError(key) orig_key = key - if is_valid_nat_for_dtype(key, self.dtype): + if is_valid_na_for_dtype(key, self.dtype): key = NaT if isinstance(key, self._data._recognized_scalars): diff --git a/pandas/core/series.py b/pandas/core/series.py index 559b27aeb7e50..b8d6deb353cd2 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -60,6 +60,7 @@ is_list_like, is_object_dtype, is_scalar, + pandas_dtype, validate_all_hashable, ) from pandas.core.dtypes.generic import ABCDataFrame @@ -405,7 +406,7 @@ def _init_dict(self, data, index=None, dtype: Optional[Dtype] = None): elif index is not None: # fastpath for Series(data=None). Just use broadcasting a scalar # instead of reindexing. - values = na_value_for_dtype(dtype) + values = na_value_for_dtype(pandas_dtype(dtype)) keys = index else: keys, values = (), []