Skip to content

EA interface: rename ExtensionArray._hasnans to ._hasna #45519

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 2 commits into from
Jan 21, 2022
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
11 changes: 7 additions & 4 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ def __contains__(self, item: object) -> bool | np.bool_:
if not self._can_hold_na:
return False
elif item is self.dtype.na_value or isinstance(item, self.dtype.type):
return self._hasnans
return self._hasna
else:
return False
else:
Expand Down Expand Up @@ -606,7 +606,7 @@ def isna(self) -> np.ndarray | ExtensionArraySupportsAnyAll:
raise AbstractMethodError(self)

@property
def _hasnans(self) -> bool:
def _hasna(self) -> bool:
# GH#22680
"""
Equivalent to `self.isna().any()`.
Expand Down Expand Up @@ -698,7 +698,7 @@ def argmin(self, skipna: bool = True) -> int:
ExtensionArray.argmax
"""
validate_bool_kwarg(skipna, "skipna")
if not skipna and self._hasnans:
if not skipna and self._hasna:
raise NotImplementedError
return nargminmax(self, "argmin")

Expand All @@ -722,7 +722,7 @@ def argmax(self, skipna: bool = True) -> int:
ExtensionArray.argmin
"""
validate_bool_kwarg(skipna, "skipna")
if not skipna and self._hasnans:
if not skipna and self._hasna:
raise NotImplementedError
return nargminmax(self, "argmax")

Expand Down Expand Up @@ -1534,6 +1534,9 @@ def _empty(cls, shape: Shape, dtype: ExtensionDtype):
ExtensionDtype.empty
ExtensionDtype.empty is the 'official' public version of this API.
"""
# Implementer note: while ExtensionDtype.empty is the public way to
# call this method, it is still required to implement this `_empty`
# method as well (it is called internally in pandas)
obj = cls._from_sequence([], dtype=dtype)

taker = np.broadcast_to(np.intp(-1), shape)
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 @@ -841,7 +841,7 @@ def _isnan(self) -> npt.NDArray[np.bool_]:
return self.asi8 == iNaT

@property # NB: override with cache_readonly in immutable subclasses
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment here should be updated, might no longer be relevant

def _hasnans(self) -> bool:
def _hasna(self) -> bool:
"""
return if I have any nans; enables various perf speedups
"""
Expand All @@ -866,7 +866,7 @@ def _maybe_mask_results(

This is an internal routine.
"""
if self._hasnans:
if self._hasna:
if convert:
result = result.astype(convert)
if fill_value is None:
Expand Down Expand Up @@ -1125,7 +1125,7 @@ def _add_timedelta_arraylike(self, other):
new_values = checked_add_with_arr(
self_i8, other_i8, arr_mask=self._isnan, b_mask=other._isnan
)
if self._hasnans or other._hasnans:
if self._hasna or other._hasna:
mask = self._isnan | other._isnan
np.putmask(new_values, mask, iNaT)

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def _sub_datetime_arraylike(self, other):
other_i8 = other.asi8
arr_mask = self._isnan | other._isnan
new_values = checked_add_with_arr(self_i8, -other_i8, arr_mask=arr_mask)
if self._hasnans or other._hasnans:
if self._hasna or other._hasna:
np.putmask(new_values, arr_mask, iNaT)
return new_values.view("timedelta64[ns]")

Expand Down Expand Up @@ -1356,7 +1356,7 @@ def isocalendar(self) -> DataFrame:
iso_calendar_df = DataFrame(
sarray, columns=["year", "week", "day"], dtype="UInt32"
)
if self._hasnans:
if self._hasna:
iso_calendar_df.iloc[self._isnan] = None
return iso_calendar_df

Expand Down
10 changes: 5 additions & 5 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def _time_shift(self, periods: int, freq=None) -> PeriodArray:
f"{type(self).__name__}._time_shift"
)
values = self.asi8 + periods * self.freq.n
if self._hasnans:
if self._hasna:
values[self._isnan] = iNaT
return type(self)(values, freq=self.freq)

Expand Down Expand Up @@ -618,7 +618,7 @@ def asfreq(self, freq=None, how: str = "E") -> PeriodArray:

new_data = period_asfreq_arr(ordinal, base1, base2, end)

if self._hasnans:
if self._hasna:
new_data[self._isnan] = iNaT

return type(self)(new_data, freq=freq)
Expand All @@ -645,7 +645,7 @@ def _format_native_types(
else:
formatter = lambda dt: str(dt)

if self._hasnans:
if self._hasna:
mask = self._isnan
values[mask] = na_rep
imask = ~mask
Expand Down Expand Up @@ -712,7 +712,7 @@ def _sub_period(self, other):
new_data = asi8 - other.ordinal
new_data = np.array([self.freq * x for x in new_data])

if self._hasnans:
if self._hasna:
new_data[self._isnan] = NaT

return new_data
Expand All @@ -739,7 +739,7 @@ def _sub_period_array(self, other):
)

new_values = np.array([self.freq.base * x for x in new_values])
if self._hasnans or other._hasnans:
if self._hasna or other._hasna:
mask = self._isnan | other._isnan
new_values[mask] = NaT
return new_values
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _field_accessor(name: str, alias: str, docstring: str):
def f(self) -> np.ndarray:
values = self.asi8
result = get_timedelta_field(values, alias)
if self._hasnans:
if self._hasna:
result = self._maybe_mask_results(
result, fill_value=None, convert="float64"
)
Expand Down Expand Up @@ -911,7 +911,7 @@ def components(self) -> DataFrame:
"microseconds",
"nanoseconds",
]
hasnans = self._hasnans
hasnans = self._hasna
if hasnans:

def f(x):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@ def common_dtype_categorical_compat(
hasnas = obj.hasnans
else:
# Categorical
hasnas = cast("Categorical", obj)._hasnans
hasnas = cast("Categorical", obj)._hasna

if hasnas:
# see test_union_int_categorical_with_nan
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex):
hasnans = cast(
bool,
cache_readonly(
DatetimeLikeArrayMixin._hasnans.fget # type: ignore[attr-defined]
DatetimeLikeArrayMixin._hasna.fget # type: ignore[attr-defined]
),
)

Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/extension/base/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,8 @@ def test_empty(self, dtype):
assert isinstance(result2, cls)
assert result2.dtype == dtype
assert result2.shape == (4,)

result2 = dtype.empty(4)
assert isinstance(result2, cls)
assert result2.dtype == dtype
assert result2.shape == (4,)