Skip to content

Commit 490a999

Browse files
authored
REF: implement Categorical._box_func, make _box_func a method (#36206)
1 parent 8c7efd1 commit 490a999

File tree

6 files changed

+30
-22
lines changed

6 files changed

+30
-22
lines changed

pandas/core/arrays/categorical.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,11 @@ def _ndarray(self) -> np.ndarray:
17341734
def _from_backing_data(self, arr: np.ndarray) -> "Categorical":
17351735
return self._constructor(arr, dtype=self.dtype, fastpath=True)
17361736

1737+
def _box_func(self, i: int):
1738+
if i == -1:
1739+
return np.NaN
1740+
return self.categories[i]
1741+
17371742
# ------------------------------------------------------------------
17381743

17391744
def take_nd(self, indexer, allow_fill: bool = False, fill_value=None):
@@ -1874,10 +1879,7 @@ def __getitem__(self, key):
18741879
"""
18751880
if isinstance(key, (int, np.integer)):
18761881
i = self._codes[key]
1877-
if i == -1:
1878-
return np.nan
1879-
else:
1880-
return self.categories[i]
1882+
return self._box_func(i)
18811883

18821884
key = check_array_indexer(self, key)
18831885

pandas/core/arrays/datetimelike.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,7 @@ def _from_backing_data(self: _T, arr: np.ndarray) -> _T:
478478

479479
# ------------------------------------------------------------------
480480

481-
@property
482-
def _box_func(self):
481+
def _box_func(self, x):
483482
"""
484483
box function to get object from internal representation
485484
"""

pandas/core/arrays/datetimes.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pandas._libs import lib, tslib
88
from pandas._libs.tslibs import (
99
NaT,
10+
NaTType,
1011
Resolution,
1112
Timestamp,
1213
conversion,
@@ -475,9 +476,8 @@ def _maybe_clear_freq(self):
475476
# -----------------------------------------------------------------
476477
# Descriptive Properties
477478

478-
@property
479-
def _box_func(self):
480-
return lambda x: Timestamp(x, freq=self.freq, tz=self.tz)
479+
def _box_func(self, x) -> Union[Timestamp, NaTType]:
480+
return Timestamp(x, freq=self.freq, tz=self.tz)
481481

482482
@property
483483
def dtype(self) -> Union[np.dtype, DatetimeTZDtype]:

pandas/core/arrays/period.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,8 @@ def _time_shift(self, periods, freq=None):
484484
values[self._isnan] = iNaT
485485
return type(self)(values, freq=self.freq)
486486

487-
@property
488-
def _box_func(self):
489-
return lambda x: Period._from_ordinal(ordinal=x, freq=self.freq)
487+
def _box_func(self, x) -> Union[Period, NaTType]:
488+
return Period._from_ordinal(ordinal=x, freq=self.freq)
490489

491490
def asfreq(self, freq=None, how: str = "E") -> "PeriodArray":
492491
"""

pandas/core/arrays/timedeltas.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
from datetime import timedelta
2-
from typing import List
2+
from typing import List, Union
33

44
import numpy as np
55

66
from pandas._libs import lib, tslibs
7-
from pandas._libs.tslibs import NaT, Period, Tick, Timedelta, Timestamp, iNaT, to_offset
7+
from pandas._libs.tslibs import (
8+
NaT,
9+
NaTType,
10+
Period,
11+
Tick,
12+
Timedelta,
13+
Timestamp,
14+
iNaT,
15+
to_offset,
16+
)
817
from pandas._libs.tslibs.conversion import precision_from_unit
918
from pandas._libs.tslibs.fields import get_timedelta_field
1019
from pandas._libs.tslibs.timedeltas import array_to_timedelta64, parse_timedelta_unit
@@ -108,9 +117,8 @@ class TimedeltaArray(dtl.DatetimeLikeArrayMixin, dtl.TimelikeOps):
108117
# Note: ndim must be defined to ensure NaT.__richcmp(TimedeltaArray)
109118
# operates pointwise.
110119

111-
@property
112-
def _box_func(self):
113-
return lambda x: Timedelta(x, unit="ns")
120+
def _box_func(self, x) -> Union[Timedelta, NaTType]:
121+
return Timedelta(x, unit="ns")
114122

115123
@property
116124
def dtype(self):

pandas/core/indexes/datetimelike.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def wrapper(left, right):
8181
DatetimeLikeArrayMixin,
8282
cache=True,
8383
)
84-
@inherit_names(["mean", "asi8", "freq", "freqstr", "_box_func"], DatetimeLikeArrayMixin)
84+
@inherit_names(["mean", "asi8", "freq", "freqstr"], DatetimeLikeArrayMixin)
8585
class DatetimeIndexOpsMixin(ExtensionIndex):
8686
"""
8787
Common ops mixin to support a unified interface datetimelike Index.
@@ -244,7 +244,7 @@ def min(self, axis=None, skipna=True, *args, **kwargs):
244244
# quick check
245245
if len(i8) and self.is_monotonic:
246246
if i8[0] != iNaT:
247-
return self._box_func(i8[0])
247+
return self._data._box_func(i8[0])
248248

249249
if self.hasnans:
250250
if skipna:
@@ -253,7 +253,7 @@ def min(self, axis=None, skipna=True, *args, **kwargs):
253253
return self._na_value
254254
else:
255255
min_stamp = i8.min()
256-
return self._box_func(min_stamp)
256+
return self._data._box_func(min_stamp)
257257
except ValueError:
258258
return self._na_value
259259

@@ -301,7 +301,7 @@ def max(self, axis=None, skipna=True, *args, **kwargs):
301301
# quick check
302302
if len(i8) and self.is_monotonic:
303303
if i8[-1] != iNaT:
304-
return self._box_func(i8[-1])
304+
return self._data._box_func(i8[-1])
305305

306306
if self.hasnans:
307307
if skipna:
@@ -310,7 +310,7 @@ def max(self, axis=None, skipna=True, *args, **kwargs):
310310
return self._na_value
311311
else:
312312
max_stamp = i8.max()
313-
return self._box_func(max_stamp)
313+
return self._data._box_func(max_stamp)
314314
except ValueError:
315315
return self._na_value
316316

0 commit comments

Comments
 (0)