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 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
50 changes: 28 additions & 22 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from pandas.compat import set_function_name
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError, NullFrequencyError, PerformanceWarning
from pandas.util._decorators import Appender, Substitution
from pandas.util._decorators import Appender, Substitution, cache_readonly
from pandas.util._validators import validate_fillna_kwargs

from pandas.core.dtypes.common import (
Expand Down 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 @@ -458,18 +466,15 @@ class DatetimeLikeArrayMixin(
# ------------------------------------------------------------------
# NDArrayBackedExtensionArray compat

# TODO: make this a cache_readonly; need to get around _index_data
# kludge in libreduction
@property
@cache_readonly
def _ndarray(self) -> np.ndarray:
# NB: A bunch of Interval tests fail if we use ._data
return self.asi8
return self._data

def _from_backing_data(self: _T, arr: np.ndarray) -> _T:
# Note: we do not retain `freq`
# error: Too many arguments for "NDArrayBackedExtensionArray"
# error: Unexpected keyword argument "dtype" for "NDArrayBackedExtensionArray"
return type(self)(arr, dtype=self.dtype) # type: ignore[call-arg]
return type(self)._simple_new( # type: ignore[attr-defined]
arr, dtype=self.dtype
)

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

Expand Down Expand Up @@ -526,7 +531,7 @@ def __array__(self, dtype=None) -> np.ndarray:
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
if is_object_dtype(dtype):
return np.array(list(self), dtype=object)
return self._data
return self._ndarray

def __getitem__(self, key):
"""
Expand All @@ -536,7 +541,7 @@ def __getitem__(self, key):

if lib.is_integer(key):
# fast-path
result = self._data[key]
result = self._ndarray[key]
if self.ndim == 1:
return self._box_func(result)
return self._simple_new(result, dtype=self.dtype)
Expand All @@ -557,7 +562,7 @@ def __getitem__(self, key):
key = check_array_indexer(self, key)

freq = self._get_getitem_freq(key)
result = self._data[key]
result = self._ndarray[key]
if lib.is_scalar(result):
return self._box_func(result)
return self._simple_new(result, dtype=self.dtype, freq=freq)
Expand Down Expand Up @@ -612,7 +617,7 @@ def __setitem__(

value = self._validate_setitem_value(value)
key = check_array_indexer(self, key)
self._data[key] = value
self._ndarray[key] = value
self._maybe_clear_freq()

def _maybe_clear_freq(self):
Expand Down Expand Up @@ -663,8 +668,8 @@ def astype(self, dtype, copy=True):

def view(self, dtype=None):
if dtype is None or dtype is self.dtype:
return type(self)(self._data, dtype=self.dtype)
return self._data.view(dtype=dtype)
return type(self)(self._ndarray, dtype=self.dtype)
return self._ndarray.view(dtype=dtype)

# ------------------------------------------------------------------
# ExtensionArray Interface
Expand Down Expand Up @@ -705,7 +710,7 @@ def _from_factorized(cls, values, original):
return cls(values, dtype=original.dtype)

def _values_for_argsort(self):
return self._data
return self._ndarray

# ------------------------------------------------------------------
# Validation Methods
Expand All @@ -722,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 @@ -736,7 +741,8 @@ def _validate_fill_value(self, fill_value):
fill_value = self._validate_scalar(fill_value, msg)
except TypeError as err:
raise ValueError(msg) from err
return self._unbox(fill_value)
rv = self._unbox(fill_value)
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 Expand Up @@ -951,9 +957,9 @@ def value_counts(self, dropna=False):
from pandas import Index, Series

if dropna:
values = self[~self.isna()]._data
values = self[~self.isna()]._ndarray
else:
values = self._data
values = self._ndarray

cls = type(self)

Expand Down Expand Up @@ -1044,9 +1050,9 @@ def fillna(self, value=None, method=None, limit=None):
else:
func = missing.backfill_1d

values = self._data
values = self._ndarray
if not is_period_dtype(self.dtype):
# For PeriodArray self._data is i8, which gets copied
# For PeriodArray self._ndarray is i8, which gets copied
# by `func`. Otherwise we need to make a copy manually
# to avoid modifying `self` in-place.
values = values.copy()
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
4 changes: 3 additions & 1 deletion pandas/tests/frame/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def test_setitem(self, timezone_frame):
b1 = df._mgr.blocks[1]
b2 = df._mgr.blocks[2]
tm.assert_extension_array_equal(b1.values, b2.values)
assert id(b1.values._data.base) != id(b2.values._data.base)
b1base = b1.values._data.base
b2base = b2.values._data.base
assert b1base is None or (id(b1base) != id(b2base))

# with nan
df2 = df.copy()
Expand Down