Skip to content

REF/TST: Fix remaining DatetimeArray with DateOffset arithmetic ops #23789

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 25 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
986fdbc
unrelated, change _window->libwindow
jbrockmendel Nov 19, 2018
fd75931
revert non-central
jbrockmendel Nov 19, 2018
66c866b
implement remaining methods to fix dateoffset arithmetic with DTA/TDA
jbrockmendel Nov 19, 2018
4dc17e2
xfail dataframe case
jbrockmendel Nov 19, 2018
da3459c
Merge branch 'master' of https://github.com/pandas-dev/pandas into et…
jbrockmendel Nov 20, 2018
348a8b2
Merge branch 'master' of https://github.com/pandas-dev/pandas into et…
jbrockmendel Nov 20, 2018
dd7e873
dont use verify_integrity, push one more level of test into parametrize
jbrockmendel Nov 20, 2018
c8351bc
Merge branch 'master' of https://github.com/pandas-dev/pandas into et…
jbrockmendel Nov 20, 2018
23a25d1
fix broken tests
jbrockmendel Nov 20, 2018
b4ae288
comment clarification
jbrockmendel Nov 22, 2018
d1ebdbf
Merge branch 'master' of https://github.com/pandas-dev/pandas into et…
jbrockmendel Nov 22, 2018
711ee61
dummy commit to force CI
jbrockmendel Nov 22, 2018
9338b5b
Merge branch 'master' of https://github.com/pandas-dev/pandas into et…
jbrockmendel Nov 25, 2018
5fbe9c8
comments about cached and non-cached implementations
jbrockmendel Nov 25, 2018
c7db0e4
Merge branch 'master' of https://github.com/pandas-dev/pandas into et…
jbrockmendel Nov 26, 2018
a4f9733
coverage
jbrockmendel Nov 26, 2018
b50fedf
special casing in freq_infer
jbrockmendel Nov 26, 2018
7e951e4
special casing followup
jbrockmendel Nov 26, 2018
317e1e7
fix simple_new args
jbrockmendel Nov 26, 2018
5de2d42
Merge branch 'master' of https://github.com/pandas-dev/pandas into et…
jbrockmendel Nov 27, 2018
5433a71
privatize
jbrockmendel Nov 27, 2018
dc137f3
Merge branch 'master' of https://github.com/pandas-dev/pandas into et…
jbrockmendel Nov 27, 2018
2c65f3b
more kludges
jbrockmendel Nov 27, 2018
31c5c0b
Merge branch 'master' of https://github.com/pandas-dev/pandas into et…
jbrockmendel Nov 27, 2018
c3d775e
typo fixup
jbrockmendel Nov 27, 2018
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
6 changes: 4 additions & 2 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,15 @@ cdef class _Timestamp(datetime):
neg_other = -other
return self + neg_other

typ = getattr(other, '_typ', None)

# a Timestamp-DatetimeIndex -> yields a negative TimedeltaIndex
elif getattr(other, '_typ', None) == 'datetimeindex':
if typ in ('datetimeindex', 'datetimearray'):
# timezone comparison is performed in DatetimeIndex._sub_datelike
return -other.__sub__(self)

# a Timestamp-TimedeltaIndex -> yields a negative TimedeltaIndex
elif getattr(other, '_typ', None) == 'timedeltaindex':
elif typ in ('timedeltaindex', 'timedeltaarray'):
return (-other).__add__(self)

elif other is NaT:
Expand Down
43 changes: 36 additions & 7 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import numpy as np

from pandas._libs import tslibs
from pandas._libs import algos, tslibs
from pandas._libs.tslibs import NaT, Timedelta, Timestamp, iNaT
from pandas._libs.tslibs.fields import get_timedelta_field
from pandas._libs.tslibs.timedeltas import (
Expand All @@ -24,7 +24,7 @@
from pandas.core.dtypes.missing import isna

from pandas.core import ops
from pandas.core.algorithms import checked_add_with_arr
from pandas.core.algorithms import checked_add_with_arr, unique1d
import pandas.core.common as com

from pandas.tseries.frequencies import to_offset
Expand Down Expand Up @@ -162,15 +162,29 @@ def _simple_new(cls, values, freq=None, dtype=_TD_DTYPE):
result._freq = freq
return result

def __new__(cls, values, freq=None, dtype=_TD_DTYPE):
def __new__(cls, values, freq=None, dtype=_TD_DTYPE, copy=False):

freq, freq_infer = dtl.maybe_infer_freq(freq)

values = np.array(values, copy=False)
if values.dtype == np.object_:
values = array_to_timedelta64(values)
values, inferred_freq = sequence_to_td64ns(
values, copy=copy, unit=None)
if inferred_freq is not None:
if freq is not None and freq != inferred_freq:
raise ValueError('Inferred frequency {inferred} from passed '
'values does not conform to passed frequency '
'{passed}'
.format(inferred=inferred_freq,
passed=freq.freqstr))
elif freq is None:
freq = inferred_freq
freq_infer = False

result = cls._simple_new(values, freq=freq)
# check that we are matching freqs
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you could simplify a lot of these checks if also pass inferred_freq (optionally to ._validate_frequencey, and for example handle the fact that result could be a scalar (just ignore the validation) / sequence (do the validation)

Copy link
Member Author

Choose a reason for hiding this comment

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

ill take a look at this

Copy link
Member Author

Choose a reason for hiding this comment

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

I think you're right that we can change this from a 3-4 liner into a 1-2 liner. Since this pattern shows up in all four of TDA/DTA/TDI/DTI constructors (actually, future tense for DTA), I'd like to do change them all at once in a dedicated follow-up

if inferred_freq is None and len(result) > 0:
if freq is not None and not freq_infer:
cls._validate_frequency(result, freq)

if freq_infer:
result.freq = to_offset(result.inferred_freq)

Expand Down Expand Up @@ -227,6 +241,21 @@ def _validate_fill_value(self, fill_value):
"Got '{got}'.".format(got=fill_value))
return fill_value

# monotonicity/uniqueness properties are called via frequencies.infer_freq,
# see GH#23789

@property
def _is_monotonic_increasing(self):
return algos.is_monotonic(self.asi8, timelike=True)[0]

@property
def _is_monotonic_decreasing(self):
return algos.is_monotonic(self.asi8, timelike=True)[1]

@property
def _is_unique(self):
return len(unique1d(self.asi8)) == len(self)

# ----------------------------------------------------------------
# Arithmetic Methods

Expand Down Expand Up @@ -283,7 +312,7 @@ def _add_datetimelike_scalar(self, other):
result = checked_add_with_arr(i8, other.value,
arr_mask=self._isnan)
result = self._maybe_mask_results(result)
return DatetimeArrayMixin(result, tz=other.tz)
return DatetimeArrayMixin(result, tz=other.tz, freq=self.freq)

def _addsub_offset_array(self, other, op):
# Add or subtract Array-like of DateOffset objects
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,11 @@ def slice_indexer(self, start=None, end=None, step=None, kind=None):
# --------------------------------------------------------------------
# Wrapping DatetimeArray

# Compat for frequency inference, see GH#23789
_is_monotonic_increasing = Index.is_monotonic_increasing
_is_monotonic_decreasing = Index.is_monotonic_decreasing
_is_unique = Index.is_unique

_timezone = cache_readonly(DatetimeArray._timezone.fget)
is_normalized = cache_readonly(DatetimeArray.is_normalized.fget)
_resolution = cache_readonly(DatetimeArray._resolution.fget)
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ def _format_native_types(self, na_rep=u'NaT', date_format=None, **kwargs):

total_seconds = wrap_array_method(TimedeltaArray.total_seconds, True)

# Compat for frequency inference, see GH#23789
_is_monotonic_increasing = Index.is_monotonic_increasing
_is_monotonic_decreasing = Index.is_monotonic_decreasing
_is_unique = Index.is_unique

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

@Appender(_index_shared_docs['astype'])
Expand Down
Loading