Skip to content
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

Enable add/sub operations involving a CFTimeIndex and a TimedeltaIndex #2484

Closed
spencerkclark opened this issue Oct 13, 2018 · 1 comment · Fixed by #2485
Closed

Enable add/sub operations involving a CFTimeIndex and a TimedeltaIndex #2484

spencerkclark opened this issue Oct 13, 2018 · 1 comment · Fixed by #2485

Comments

@spencerkclark
Copy link
Member

In [1]: import xarray as xr

In [2]: start_dates = xr.cftime_range('1999-12', periods=12, freq='M')

In [3]: end_dates = start_dates.shift(1, 'M')

In [4]: end_dates - start_dates
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-43c24409020b> in <module>()
----> 1 end_dates - start_dates

/Users/spencerclark/xarray-dev/xarray/xarray/coding/cftimeindex.pyc in __sub__(self, other)
    365
    366     def __sub__(self, other):
--> 367         return CFTimeIndex(np.array(self) - other)
    368
    369

TypeError: unsupported operand type(s) for -: 'numpy.ndarray' and 'CFTimeIndex'

Problem description

Subtracting one DatetimeIndex from another produces a TimedeltaIndex:

In [5]: import pandas as pd

In [6]: start_dates = pd.date_range('1999-12', periods=12, freq='M')

In [7]: end_dates = start_dates.shift(1, 'M')

In [8]: end_dates - start_dates
Out[8]:
TimedeltaIndex(['31 days', '29 days', '31 days', '30 days', '31 days',
                '30 days', '31 days', '31 days', '30 days', '31 days',
                '30 days', '31 days'],
               dtype='timedelta64[ns]', freq=None)

This should also be straightforward to enable for CFTimeIndexes and would be useful, for example, in the problem described in #2481 (comment).

Expected Output

In [1]: import xarray as xr

In [2]: start_dates = xr.cftime_range('1999-12', periods=12, freq='M')

In [3]: end_dates = start_dates.shift(1, 'M')

In [4]: end_dates - start_dates
Out[4]:
TimedeltaIndex(['31 days', '29 days', '31 days', '30 days', '31 days',
                '30 days', '31 days', '31 days', '30 days', '31 days',
                '30 days', '31 days'],
               dtype='timedelta64[ns]', freq=None)
@spencerkclark
Copy link
Member Author

spencerkclark commented Oct 13, 2018

Adding a TimedeltaIndex to a CFTimeIndex would also make sense:

In [1]: import xarray as xr; import pandas as pd

In [2]: a = xr.cftime_range('2000', periods=5)

In [3]: deltas = pd.TimedeltaIndex([pd.Timedelta(days=1) for _ in range(5)])

In [4]: a + deltas
Out[4]:
CFTimeIndex([2000-01-02 00:00:00, 2000-01-03 00:00:00, 2000-01-04 00:00:00,
             2000-01-05 00:00:00, 2000-01-06 00:00:00],
            dtype='object')

In [5]: deltas + a
Out[5]:
CFTimeIndex([2000-01-02 00:00:00, 2000-01-03 00:00:00, 2000-01-04 00:00:00,
             2000-01-05 00:00:00, 2000-01-06 00:00:00],
            dtype='object')

So would subtracting a TimedeltaIndex from a CFTimeIndex:

In [6]: a - deltas
Out[6]:
CFTimeIndex([1999-12-31 00:00:00, 2000-01-01 00:00:00, 2000-01-02 00:00:00,
             2000-01-03 00:00:00, 2000-01-04 00:00:00],
            dtype='object')

@spencerkclark spencerkclark changed the title Subtracting a CFTimeIndex from a CFTimeIndex should return a TimedeltaIndex Enable add/sub operations involving a CFTimeIndex and a TimedeltaIndex Oct 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant