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

Add a days_in_month accessor to CFTimeIndex #3935

Merged
merged 7 commits into from
Apr 6, 2020

Conversation

spencerkclark
Copy link
Member

@spencerkclark spencerkclark commented Apr 5, 2020

  • Tests added
  • Passes isort -rc . && black . && mypy . && flake8
  • Fully documented, including whats-new.rst for all changes and api.rst for new API

This adds a days_in_month accessor to CFTimeIndex, which allows for easy computation of monthly time weights for non-standard calendars:

In [1]: import xarray as xr

In [2]: times = xr.cftime_range("2000", periods=24, freq="MS", calendar="noleap")

In [3]: da = xr.DataArray(times, dims=["time"])

In [4]: da.dt.days_in_month
Out[4]:
<xarray.DataArray 'days_in_month' (time: 24)>
array([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31,
       30, 31, 31, 30, 31, 30, 31])
Coordinates:
  * time     (time) object 2000-01-01 00:00:00 ... 2001-12-01 00:00:00

This simplifies the "Calculating Seasonal Averages from Timeseries of Monthly Means" example @jhamman wrote for the docs a while back, which I've taken the liberty of updating.

The ability to add this feature to xarray is thanks in large part to @huard, who added a daysinmonth attribute to cftime.datetime objects late last year: Unidata/cftime#138.

Copy link
Contributor

@dcherian dcherian left a comment

Choose a reason for hiding this comment

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

Thanks @spencerkclark . LGTM.

Can you strip outputs from the notebook? That should reduce the diff here and later when it gets updated next.

doc/whats-new.rst Show resolved Hide resolved
@spencerkclark
Copy link
Member Author

Can you strip outputs from the notebook? That should reduce the diff here and later when it gets updated next.

That didn't occur to me, thanks. That's indeed much better. I added a documentation note as well; things should hopefully be all set now.

@jhamman
Copy link
Member

jhamman commented Apr 5, 2020

Fantastic! Thanks @spencerkclark!

@dcherian dcherian merged commit 6048356 into pydata:master Apr 6, 2020
@dcherian
Copy link
Contributor

dcherian commented Apr 6, 2020

Thanks @spencerkclark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants