Skip to content

Commit ab51981

Browse files
Backport PR #45519: EA interface: rename ExtensionArray._hasnans to ._hasna (#45543)
1 parent 61b835a commit ab51981

File tree

7 files changed

+25
-17
lines changed

7 files changed

+25
-17
lines changed

pandas/core/arrays/base.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ def __contains__(self, item: object) -> bool | np.bool_:
427427
if not self._can_hold_na:
428428
return False
429429
elif item is self.dtype.na_value or isinstance(item, self.dtype.type):
430-
return self._hasnans
430+
return self._hasna
431431
else:
432432
return False
433433
else:
@@ -606,7 +606,7 @@ def isna(self) -> np.ndarray | ExtensionArraySupportsAnyAll:
606606
raise AbstractMethodError(self)
607607

608608
@property
609-
def _hasnans(self) -> bool:
609+
def _hasna(self) -> bool:
610610
# GH#22680
611611
"""
612612
Equivalent to `self.isna().any()`.
@@ -698,7 +698,7 @@ def argmin(self, skipna: bool = True) -> int:
698698
ExtensionArray.argmax
699699
"""
700700
validate_bool_kwarg(skipna, "skipna")
701-
if not skipna and self._hasnans:
701+
if not skipna and self._hasna:
702702
raise NotImplementedError
703703
return nargminmax(self, "argmin")
704704

@@ -722,7 +722,7 @@ def argmax(self, skipna: bool = True) -> int:
722722
ExtensionArray.argmin
723723
"""
724724
validate_bool_kwarg(skipna, "skipna")
725-
if not skipna and self._hasnans:
725+
if not skipna and self._hasna:
726726
raise NotImplementedError
727727
return nargminmax(self, "argmax")
728728

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

15391542
taker = np.broadcast_to(np.intp(-1), shape)

pandas/core/arrays/datetimelike.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ def _isnan(self) -> npt.NDArray[np.bool_]:
841841
return self.asi8 == iNaT
842842

843843
@property # NB: override with cache_readonly in immutable subclasses
844-
def _hasnans(self) -> bool:
844+
def _hasna(self) -> bool:
845845
"""
846846
return if I have any nans; enables various perf speedups
847847
"""
@@ -866,7 +866,7 @@ def _maybe_mask_results(
866866
867867
This is an internal routine.
868868
"""
869-
if self._hasnans:
869+
if self._hasna:
870870
if convert:
871871
result = result.astype(convert)
872872
if fill_value is None:
@@ -1125,7 +1125,7 @@ def _add_timedelta_arraylike(self, other):
11251125
new_values = checked_add_with_arr(
11261126
self_i8, other_i8, arr_mask=self._isnan, b_mask=other._isnan
11271127
)
1128-
if self._hasnans or other._hasnans:
1128+
if self._hasna or other._hasna:
11291129
mask = self._isnan | other._isnan
11301130
np.putmask(new_values, mask, iNaT)
11311131

pandas/core/arrays/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ def _sub_datetime_arraylike(self, other):
738738
other_i8 = other.asi8
739739
arr_mask = self._isnan | other._isnan
740740
new_values = checked_add_with_arr(self_i8, -other_i8, arr_mask=arr_mask)
741-
if self._hasnans or other._hasnans:
741+
if self._hasna or other._hasna:
742742
np.putmask(new_values, arr_mask, iNaT)
743743
return new_values.view("timedelta64[ns]")
744744

@@ -1356,7 +1356,7 @@ def isocalendar(self) -> DataFrame:
13561356
iso_calendar_df = DataFrame(
13571357
sarray, columns=["year", "week", "day"], dtype="UInt32"
13581358
)
1359-
if self._hasnans:
1359+
if self._hasna:
13601360
iso_calendar_df.iloc[self._isnan] = None
13611361
return iso_calendar_df
13621362

pandas/core/arrays/period.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ def _time_shift(self, periods: int, freq=None) -> PeriodArray:
548548
f"{type(self).__name__}._time_shift"
549549
)
550550
values = self.asi8 + periods * self.freq.n
551-
if self._hasnans:
551+
if self._hasna:
552552
values[self._isnan] = iNaT
553553
return type(self)(values, freq=self.freq)
554554

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

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

621-
if self._hasnans:
621+
if self._hasna:
622622
new_data[self._isnan] = iNaT
623623

624624
return type(self)(new_data, freq=freq)
@@ -645,7 +645,7 @@ def _format_native_types(
645645
else:
646646
formatter = lambda dt: str(dt)
647647

648-
if self._hasnans:
648+
if self._hasna:
649649
mask = self._isnan
650650
values[mask] = na_rep
651651
imask = ~mask
@@ -712,7 +712,7 @@ def _sub_period(self, other):
712712
new_data = asi8 - other.ordinal
713713
new_data = np.array([self.freq * x for x in new_data])
714714

715-
if self._hasnans:
715+
if self._hasna:
716716
new_data[self._isnan] = NaT
717717

718718
return new_data
@@ -739,7 +739,7 @@ def _sub_period_array(self, other):
739739
)
740740

741741
new_values = np.array([self.freq.base * x for x in new_values])
742-
if self._hasnans or other._hasnans:
742+
if self._hasna or other._hasna:
743743
mask = self._isnan | other._isnan
744744
new_values[mask] = NaT
745745
return new_values

pandas/core/arrays/timedeltas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def _field_accessor(name: str, alias: str, docstring: str):
8282
def f(self) -> np.ndarray:
8383
values = self.asi8
8484
result = get_timedelta_field(values, alias)
85-
if self._hasnans:
85+
if self._hasna:
8686
result = self._maybe_mask_results(
8787
result, fill_value=None, convert="float64"
8888
)
@@ -911,7 +911,7 @@ def components(self) -> DataFrame:
911911
"microseconds",
912912
"nanoseconds",
913913
]
914-
hasnans = self._hasnans
914+
hasnans = self._hasna
915915
if hasnans:
916916

917917
def f(x):

pandas/core/indexes/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex):
9696
hasnans = cast(
9797
bool,
9898
cache_readonly(
99-
DatetimeLikeArrayMixin._hasnans.fget # type: ignore[attr-defined]
99+
DatetimeLikeArrayMixin._hasna.fget # type: ignore[attr-defined]
100100
),
101101
)
102102

pandas/tests/extension/base/constructors.py

+5
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,8 @@ def test_empty(self, dtype):
135135
assert isinstance(result2, cls)
136136
assert result2.dtype == dtype
137137
assert result2.shape == (4,)
138+
139+
result2 = dtype.empty(4)
140+
assert isinstance(result2, cls)
141+
assert result2.dtype == dtype
142+
assert result2.shape == (4,)

0 commit comments

Comments
 (0)