Skip to content

Improve arithmetic operations involving CFTimeIndexes and TimedeltaIndexes #2485

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 3 commits into from
Oct 17, 2018
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
9 changes: 8 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Enhancements
:py:meth:`~xarray.Dataset.differentiate`,
:py:meth:`~xarray.DataArray.interp`, and
:py:meth:`~xarray.Dataset.interp`.
By `Spencer Clark <https://github.com/spencerkclark>`_
By `Spencer Clark <https://github.com/spencerkclark>`_.

Bug fixes
~~~~~~~~~
Expand All @@ -97,6 +97,13 @@ Bug fixes
``open_rasterio`` to error (:issue:`2454`).
By `Stephan Hoyer <https://github.com/shoyer>`_.

- Subtracting one CFTimeIndex from another now returns a
``pandas.TimedeltaIndex``, analogous to the behavior for DatetimeIndexes
(:issue:`2484`). By `Spencer Clark <https://github.com/spencerkclark>`_.
- Adding a TimedeltaIndex to, or subtracting a TimedeltaIndex from a
CFTimeIndex is now allowed (:issue:`2484`).
By `Spencer Clark <https://github.com/spencerkclark>`_.

.. _whats-new.0.10.9:

v0.10.9 (21 September 2018)
Expand Down
16 changes: 15 additions & 1 deletion xarray/coding/cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,27 @@ def shift(self, n, freq):
"str or datetime.timedelta, got {}.".format(freq))

def __add__(self, other):
if isinstance(other, pd.TimedeltaIndex):
other = other.to_pytimedelta()
return CFTimeIndex(np.array(self) + other)

def __radd__(self, other):
if isinstance(other, pd.TimedeltaIndex):
other = other.to_pytimedelta()
return CFTimeIndex(other + np.array(self))

def __sub__(self, other):
return CFTimeIndex(np.array(self) - other)
if isinstance(other, CFTimeIndex):
return pd.TimedeltaIndex(np.array(self) - np.array(other))
elif isinstance(other, pd.TimedeltaIndex):
return CFTimeIndex(np.array(self) - other.to_pytimedelta())
else:
return CFTimeIndex(np.array(self) - other)

def _add_delta(self, deltas):
# To support TimedeltaIndex + CFTimeIndex with older versions of
# pandas. No longer used as of pandas 0.23.
return self + deltas


def _parse_iso8601_without_reso(date_type, datetime_str):
Expand Down
44 changes: 44 additions & 0 deletions xarray/tests/test_cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,17 @@ def test_cftimeindex_add(index):
assert isinstance(result, CFTimeIndex)


@pytest.mark.skipif(not has_cftime, reason='cftime not installed')
@pytest.mark.parametrize('calendar', _CFTIME_CALENDARS)
def test_cftimeindex_add_timedeltaindex(calendar):
a = xr.cftime_range('2000', periods=5, calendar=calendar)
deltas = pd.TimedeltaIndex([timedelta(days=2) for _ in range(5)])
result = a + deltas
expected = a.shift(2, 'D')
assert result.equals(expected)
assert isinstance(result, CFTimeIndex)


@pytest.mark.skipif(not has_cftime, reason='cftime not installed')
def test_cftimeindex_radd(index):
date_type = index.date_type
Expand All @@ -640,6 +651,17 @@ def test_cftimeindex_radd(index):
assert isinstance(result, CFTimeIndex)


@pytest.mark.skipif(not has_cftime, reason='cftime not installed')
@pytest.mark.parametrize('calendar', _CFTIME_CALENDARS)
def test_timedeltaindex_add_cftimeindex(calendar):
a = xr.cftime_range('2000', periods=5, calendar=calendar)
deltas = pd.TimedeltaIndex([timedelta(days=2) for _ in range(5)])
result = deltas + a
expected = a.shift(2, 'D')
assert result.equals(expected)
assert isinstance(result, CFTimeIndex)


@pytest.mark.skipif(not has_cftime, reason='cftime not installed')
def test_cftimeindex_sub(index):
date_type = index.date_type
Expand All @@ -652,6 +674,28 @@ def test_cftimeindex_sub(index):
assert isinstance(result, CFTimeIndex)


@pytest.mark.skipif(not has_cftime, reason='cftime not installed')
@pytest.mark.parametrize('calendar', _CFTIME_CALENDARS)
def test_cftimeindex_sub_cftimeindex(calendar):
a = xr.cftime_range('2000', periods=5, calendar=calendar)
b = a.shift(2, 'D')
result = b - a
expected = pd.TimedeltaIndex([timedelta(days=2) for _ in range(5)])
assert result.equals(expected)
assert isinstance(result, pd.TimedeltaIndex)


@pytest.mark.skipif(not has_cftime, reason='cftime not installed')
@pytest.mark.parametrize('calendar', _CFTIME_CALENDARS)
def test_cftimeindex_sub_timedeltaindex(calendar):
a = xr.cftime_range('2000', periods=5, calendar=calendar)
deltas = pd.TimedeltaIndex([timedelta(days=2) for _ in range(5)])
result = a - deltas
expected = a.shift(-2, 'D')
assert result.equals(expected)
assert isinstance(result, CFTimeIndex)


@pytest.mark.skipif(not has_cftime, reason='cftime not installed')
def test_cftimeindex_rsub(index):
with pytest.raises(TypeError):
Expand Down