-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
POC: _eadata #24394
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
POC: _eadata #24394
Changes from 4 commits
de5430d
91f22e4
3eec768
a4cfac0
8f5fb27
1c83529
00149f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -713,15 +713,6 @@ def snap(self, freq='S'): | |
return DatetimeIndex._simple_new(snapped, freq=freq) | ||
# TODO: what about self.name? tz? if so, use shallow_copy? | ||
|
||
def unique(self, level=None): | ||
if level is not None: | ||
self._validate_index_level(level) | ||
|
||
# TODO(DatetimeArray): change dispatch once inheritance is removed | ||
# call DatetimeArray method | ||
result = DatetimeArray.unique(self) | ||
return self._shallow_copy(result._data) | ||
|
||
def join(self, other, how='left', level=None, return_indexers=False, | ||
sort=False): | ||
""" | ||
|
@@ -1089,6 +1080,11 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None): | |
# -------------------------------------------------------------------- | ||
# Wrapping DatetimeArray | ||
|
||
@property | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't this always be cached? maybe call this _impl instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but for the proof of concept I didn't want to futz with it. If this is a direction we want to move forward with I'll do this. |
||
def _eadata(self): | ||
return DatetimeArray._simple_new(self._data, | ||
tz=self.tz, freq=self.freq) | ||
|
||
# Compat for frequency inference, see GH#23789 | ||
_is_monotonic_increasing = Index.is_monotonic_increasing | ||
_is_monotonic_decreasing = Index.is_monotonic_decreasing | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,8 @@ | |
import pandas.core.common as com | ||
from pandas.core.indexes.base import Index, _index_shared_docs | ||
from pandas.core.indexes.datetimelike import ( | ||
DatetimeIndexOpsMixin, wrap_arithmetic_op, wrap_array_method, | ||
wrap_field_accessor) | ||
DatetimeIndexOpsMixin, maybe_unwrap_index, wrap_arithmetic_op, | ||
wrap_array_method, wrap_field_accessor) | ||
from pandas.core.indexes.numeric import Int64Index | ||
from pandas.core.ops import get_op_result_name | ||
from pandas.core.tools.timedeltas import _coerce_scalar_to_timedelta_type | ||
|
@@ -36,11 +36,7 @@ def _make_wrapped_arith_op(opname): | |
meth = getattr(TimedeltaArray, opname) | ||
|
||
def method(self, other): | ||
oth = other | ||
if isinstance(other, Index): | ||
oth = other._data | ||
|
||
result = meth(self, oth) | ||
result = meth(self._eadata, maybe_unwrap_index(other)) | ||
return wrap_arithmetic_op(self, other, result) | ||
|
||
method.__name__ = opname | ||
|
@@ -239,6 +235,10 @@ def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs): | |
# ------------------------------------------------------------------- | ||
# Wrapping TimedeltaArray | ||
|
||
@property | ||
def _eadata(self): | ||
return TimedeltaArray._simple_new(self._data, freq=self.freq) | ||
|
||
__mul__ = _make_wrapped_arith_op("__mul__") | ||
__rmul__ = _make_wrapped_arith_op("__rmul__") | ||
__floordiv__ = _make_wrapped_arith_op("__floordiv__") | ||
|
@@ -247,6 +247,11 @@ def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs): | |
__rmod__ = _make_wrapped_arith_op("__rmod__") | ||
__divmod__ = _make_wrapped_arith_op("__divmod__") | ||
__rdivmod__ = _make_wrapped_arith_op("__rdivmod__") | ||
__truediv__ = _make_wrapped_arith_op("__truediv__") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
__rtruediv__ = _make_wrapped_arith_op("__rtruediv__") | ||
if compat.PY2: | ||
__div__ = __truediv__ | ||
__rdiv__ = __rtruediv__ | ||
|
||
days = wrap_field_accessor(TimedeltaArray.days) | ||
seconds = wrap_field_accessor(TimedeltaArray.seconds) | ||
|
@@ -255,26 +260,6 @@ def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs): | |
|
||
total_seconds = wrap_array_method(TimedeltaArray.total_seconds, True) | ||
|
||
def __truediv__(self, other): | ||
oth = other | ||
if isinstance(other, Index): | ||
# TimedeltaArray defers, so we need to unwrap | ||
oth = other._values | ||
result = TimedeltaArray.__truediv__(self, oth) | ||
return wrap_arithmetic_op(self, other, result) | ||
|
||
def __rtruediv__(self, other): | ||
oth = other | ||
if isinstance(other, Index): | ||
# TimedeltaArray defers, so we need to unwrap | ||
oth = other._values | ||
result = TimedeltaArray.__rtruediv__(self, oth) | ||
return wrap_arithmetic_op(self, other, result) | ||
|
||
if compat.PY2: | ||
__div__ = __truediv__ | ||
__rdiv__ = __rtruediv__ | ||
|
||
# Compat for frequency inference, see GH#23789 | ||
_is_monotonic_increasing = Index.is_monotonic_increasing | ||
_is_monotonic_decreasing = Index.is_monotonic_decreasing | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1591,7 +1591,8 @@ def check(get_ser, test_ser): | |
# with 'operate' (from core/ops.py) for the ops that are not | ||
# defined | ||
op = getattr(get_ser, op_str, None) | ||
with pytest.raises(TypeError, match='operate|[cC]annot'): | ||
with pytest.raises(TypeError, | ||
match='operate|[cC]annot|unsupported operand'): | ||
op(test_ser) | ||
|
||
# ## timedelta64 ### | ||
|
@@ -1973,15 +1974,15 @@ def test_dti_sub_tdi(self, tz_naive_fixture): | |
result = dti - tdi | ||
tm.assert_index_equal(result, expected) | ||
|
||
msg = 'cannot subtract .*TimedeltaIndex' | ||
msg = 'cannot subtract .*TimedeltaArrayMixin' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mixin is being removed from the class name. These error message will need to be updated again. I'd prefer making the match |
||
with pytest.raises(TypeError, match=msg): | ||
tdi - dti | ||
|
||
# sub with timedelta64 array | ||
result = dti - tdi.values | ||
tm.assert_index_equal(result, expected) | ||
|
||
msg = 'cannot subtract DatetimeIndex from' | ||
msg = 'cannot subtract DatetimeArrayMixin from' | ||
with pytest.raises(TypeError, match=msg): | ||
tdi.values - dti | ||
|
||
|
@@ -1997,7 +1998,7 @@ def test_dti_isub_tdi(self, tz_naive_fixture): | |
result -= tdi | ||
tm.assert_index_equal(result, expected) | ||
|
||
msg = 'cannot subtract .*TimedeltaIndex' | ||
msg = 'cannot subtract .*TimedeltaArrayMixin' | ||
with pytest.raises(TypeError, match=msg): | ||
tdi -= dti | ||
|
||
|
@@ -2008,7 +2009,7 @@ def test_dti_isub_tdi(self, tz_naive_fixture): | |
|
||
msg = '|'.join(['cannot perform __neg__ with this index type:', | ||
'ufunc subtract cannot use operands with types', | ||
'cannot subtract DatetimeIndex from']) | ||
'cannot subtract DatetimeArrayMixin from']) | ||
with pytest.raises(TypeError, match=msg): | ||
tdi.values -= dti | ||
|
||
|
@@ -2028,7 +2029,9 @@ def test_dti_isub_tdi(self, tz_naive_fixture): | |
def test_add_datetimelike_and_dti(self, addend, tz): | ||
# GH#9631 | ||
dti = DatetimeIndex(['2011-01-01', '2011-01-02']).tz_localize(tz) | ||
msg = 'cannot add DatetimeIndex and {0}'.format(type(addend).__name__) | ||
msg = ('cannot add DatetimeArrayMixin and {0}' | ||
.format(type(addend).__name__)).replace('DatetimeIndex', | ||
'DatetimeArrayMixin') | ||
with pytest.raises(TypeError, match=msg): | ||
dti + addend | ||
with pytest.raises(TypeError, match=msg): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a general function no? if you simply check for a
__eadata
attribute. let's put this elsewhere, maybe pandas.core.baseThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is for _eadata to only exist for a few days until we're ready to complete the switchover. Regardless, maybe_unwrap_index should only be relevant for DTI/TDI/PI.