Skip to content

DTA/TDA/PA use self._data instead of self.asi8 for self._ndarray #36171

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 5 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
REF: implement _rebox_native
  • Loading branch information
jbrockmendel committed Sep 6, 2020
commit 0ec876ad197160831f15908e1f648bac4ee2f8e2
21 changes: 13 additions & 8 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ def _scalar_from_string(self, value: str) -> DTScalarOrNaT:
"""
raise AbstractMethodError(self)

@classmethod
def _rebox_native(cls, value: int) -> Union[int, np.datetime64, np.timedelta64]:
"""
Box an integer unboxed via _unbox_scalar into the native type for
the underlying ndarray.
"""
raise AbstractMethodError(cls)

def _unbox_scalar(self, value: DTScalarOrNaT) -> int:
"""
Unbox the integer value of a scalar `value`.
Expand Down Expand Up @@ -464,7 +472,9 @@ def _ndarray(self) -> np.ndarray:

def _from_backing_data(self: _T, arr: np.ndarray) -> _T:
# Note: we do not retain `freq`
return type(self)._simple_new(arr, dtype=self.dtype)
return type(self)._simple_new( # type: ignore[attr-defined]
arr, dtype=self.dtype
)

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

Expand Down Expand Up @@ -717,7 +727,7 @@ def _validate_fill_value(self, fill_value):

Returns
-------
fill_value : np.int64
fill_value : np.int64, np.datetime64, or np.timedelta64

Raises
------
Expand All @@ -732,12 +742,7 @@ def _validate_fill_value(self, fill_value):
except TypeError as err:
raise ValueError(msg) from err
rv = self._unbox(fill_value)
if self.dtype.kind == "M":
return np.int64(rv).view("M8[ns]")
elif self.dtype.kind == "m":
return np.int64(rv).view("m8[ns]")
else:
return rv
return self._rebox_native(rv)

def _validate_shift_value(self, fill_value):
# TODO(2.0): once this deprecation is enforced, use _validate_fill_value
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@ def _generate_range(
# -----------------------------------------------------------------
# DatetimeLike Interface

@classmethod
def _rebox_native(cls, value: int) -> np.datetime64:
return np.int64(value).view("M8[ns]")

def _unbox_scalar(self, value):
if not isinstance(value, self._scalar_type) and value is not NaT:
raise ValueError("'value' should be a Timestamp.")
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ def _generate_range(cls, start, end, periods, freq, fields):
# -----------------------------------------------------------------
# DatetimeLike Interface

@classmethod
def _rebox_native(cls, value: int) -> np.int64:
return np.int64(value)

def _unbox_scalar(self, value: Union[Period, NaTType]) -> int:
if value is NaT:
return value.value
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ def _generate_range(cls, start, end, periods, freq, closed=None):
# ----------------------------------------------------------------
# DatetimeLike Interface

@classmethod
def _rebox_native(cls, value: int) -> np.timedelta64:
return np.int64(value).view("m8[ns]")

def _unbox_scalar(self, value):
if not isinstance(value, self._scalar_type) and value is not NaT:
raise ValueError("'value' should be a Timedelta.")
Expand Down