Skip to content

REF: implement Categorical._box_func, make _box_func a method #36206

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 6 commits into from
Sep 8, 2020
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
10 changes: 6 additions & 4 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1734,6 +1734,11 @@ def _ndarray(self) -> np.ndarray:
def _from_backing_data(self, arr: np.ndarray) -> "Categorical":
return self._constructor(arr, dtype=self.dtype, fastpath=True)

def _box_func(self, i: int):
if i == -1:
return np.NaN
return self.categories[i]

# ------------------------------------------------------------------

def take_nd(self, indexer, allow_fill: bool = False, fill_value=None):
Expand Down Expand Up @@ -1874,10 +1879,7 @@ def __getitem__(self, key):
"""
if isinstance(key, (int, np.integer)):
i = self._codes[key]
if i == -1:
return np.nan
else:
return self.categories[i]
return self._box_func(i)

key = check_array_indexer(self, key)

Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,7 @@ def _from_backing_data(self: _T, arr: np.ndarray) -> _T:

# ------------------------------------------------------------------

@property
def _box_func(self):
def _box_func(self, x):
"""
box function to get object from internal representation
"""
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pandas._libs import lib, tslib
from pandas._libs.tslibs import (
NaT,
NaTType,
Resolution,
Timestamp,
conversion,
Expand Down Expand Up @@ -475,9 +476,8 @@ def _maybe_clear_freq(self):
# -----------------------------------------------------------------
# Descriptive Properties

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

@property
def dtype(self) -> Union[np.dtype, DatetimeTZDtype]:
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,8 @@ def _time_shift(self, periods, freq=None):
values[self._isnan] = iNaT
return type(self)(values, freq=self.freq)

@property
def _box_func(self):
return lambda x: Period._from_ordinal(ordinal=x, freq=self.freq)
def _box_func(self, x) -> Union[Period, NaTType]:
Copy link
Contributor

Choose a reason for hiding this comment

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

should alias this kind of type at somepoint, maybe PeriodOrNaT (and the others as well)

return Period._from_ordinal(ordinal=x, freq=self.freq)

def asfreq(self, freq=None, how: str = "E") -> "PeriodArray":
"""
Expand Down
18 changes: 13 additions & 5 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
from datetime import timedelta
from typing import List
from typing import List, Union

import numpy as np

from pandas._libs import lib, tslibs
from pandas._libs.tslibs import NaT, Period, Tick, Timedelta, Timestamp, iNaT, to_offset
from pandas._libs.tslibs import (
NaT,
NaTType,
Period,
Tick,
Timedelta,
Timestamp,
iNaT,
to_offset,
)
from pandas._libs.tslibs.conversion import precision_from_unit
from pandas._libs.tslibs.fields import get_timedelta_field
from pandas._libs.tslibs.timedeltas import array_to_timedelta64, parse_timedelta_unit
Expand Down Expand Up @@ -108,9 +117,8 @@ class TimedeltaArray(dtl.DatetimeLikeArrayMixin, dtl.TimelikeOps):
# Note: ndim must be defined to ensure NaT.__richcmp(TimedeltaArray)
# operates pointwise.

@property
def _box_func(self):
return lambda x: Timedelta(x, unit="ns")
def _box_func(self, x) -> Union[Timedelta, NaTType]:
return Timedelta(x, unit="ns")

@property
def dtype(self):
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def wrapper(left, right):
DatetimeLikeArrayMixin,
cache=True,
)
@inherit_names(["mean", "asi8", "freq", "freqstr", "_box_func"], DatetimeLikeArrayMixin)
@inherit_names(["mean", "asi8", "freq", "freqstr"], DatetimeLikeArrayMixin)
class DatetimeIndexOpsMixin(ExtensionIndex):
"""
Common ops mixin to support a unified interface datetimelike Index.
Expand Down Expand Up @@ -244,7 +244,7 @@ def min(self, axis=None, skipna=True, *args, **kwargs):
# quick check
if len(i8) and self.is_monotonic:
if i8[0] != iNaT:
return self._box_func(i8[0])
return self._data._box_func(i8[0])

if self.hasnans:
if skipna:
Expand All @@ -253,7 +253,7 @@ def min(self, axis=None, skipna=True, *args, **kwargs):
return self._na_value
else:
min_stamp = i8.min()
return self._box_func(min_stamp)
return self._data._box_func(min_stamp)
except ValueError:
return self._na_value

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

if self.hasnans:
if skipna:
Expand All @@ -310,7 +310,7 @@ def max(self, axis=None, skipna=True, *args, **kwargs):
return self._na_value
else:
max_stamp = i8.max()
return self._box_func(max_stamp)
return self._data._box_func(max_stamp)
except ValueError:
return self._na_value

Expand Down