Skip to content

Implement CFPeriodIndex #2481

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

Closed
huard opened this issue Oct 12, 2018 · 3 comments
Closed

Implement CFPeriodIndex #2481

huard opened this issue Oct 12, 2018 · 3 comments

Comments

@huard
Copy link
Contributor

huard commented Oct 12, 2018

A CFPeriodIndex supporting non-standard calendars would be useful to facilitate climate analyses. The use case for me would be to find the start and end date of a resampling group. This is useful to spot missing values in a resampled time series, or to create time_bnds arrays in a netCDF file.

import xarray as xr
import pandas as pd

cftime = xr.cftime_range(start='2000-01-01', periods=361, freq='D', calendar='360_day') 
pdtime = pd.date_range(start='2000-01-01', periods=361, freq='D')  

cf_da = xr.DataArray(range(361), coords={'time': cftime}, dims='time')
pd_da = xr.DataArray(range(361), coords={'time': pdtime}, dims='time') 

#cf_c = cf_da.resample(time='M').count()
pd_c = pd_da.resample(time='M').count()

#cf_p = cf_c.indexes['time'].to_period()
pd_p = pd_c.indexes['time'].to_period()

#cf_expected_days_in_group = cf_p.end_time - cf_p.start_time + pd.offsets.Day(1)
pd_expected_days_in_group = pd_p.end_time - pd_p.start_time + pd.offsets.Day(1)

Depends on #2191

@spencerkclark
Copy link
Member

I think this would be a fair amount of work to implement :), but in principle it would be a natural step forward in the pattern we have been following in porting time series functionality from pandas for use with calendars supported by cftime.

In the short term, at least for the use-case you describe above, I think a potentially simpler option would be to use a call to CFTimeIndex.shift; based on your example I'll illustrate with the DatetimeIndex version, since resample doesn't exist yet for a CFTimeIndex:

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

In [2]: times = pd.date_range('2000', periods=361)

In [3]: da = xr.DataArray(range(361), [('time', times)])

In [4]: monthly_count = da.resample(time='M').count()

In [5]: end_time = monthly_count.indexes['time']

In [6]: start_time = end_time.shift(-1, 'M')

In [7]: expected_days_in_group = end_time - start_time

In [8]: expected_days_in_group
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]', name=u'time', freq=None)

Note that currently subtracting one CFTimeIndex from another (analogous to end_time - start_time in the example above) raises an error (I think it should also return a TimedeltaIndex), but that's probably a straightforward fix (I'll create a separate issue):

In [8]: a = xr.cftime_range('2000', periods=12, freq='M')

In [9]: b = a.shift(-1, 'M')

In [10]: a - b
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-09bd029d0285> in <module>()
----> 1 a - b

/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'

As a side note, support even for standard PeriodIndexes in xarray needs some work -- see the following existing open issues: #1270, #1565. Serialization to netCDF files would also be nice to support (but I don't see an existing issue for that). I'm sure work to fix these issues would also be appreciated!

@huard
Copy link
Contributor Author

huard commented Oct 15, 2018

Got it, thanks for the workaround !

@stale
Copy link

stale bot commented Sep 27, 2020

In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

@stale stale bot added the stale label Sep 27, 2020
@stale stale bot closed this as completed Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants