Skip to content

REF: Rename is_valid_nat_for_dtype -> is_valid_na_for_dtype #39711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
is_string_dtype,
is_string_like_dtype,
needs_i8_conversion,
pandas_dtype,
)
from pandas.core.dtypes.generic import (
ABCDataFrame,
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
is_list_like,
is_object_dtype,
is_scalar,
pandas_dtype,
validate_all_hashable,
)
from pandas.core.dtypes.generic import ABCDataFrame
Expand Down Expand Up @@ -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 = (), []
Expand Down