Skip to content

Commit 5b87b6e

Browse files
spencerkclarkshoyer
authored andcommitted
WIP Add a CFTimeIndex-enabled xr.cftime_range function (#2301)
* Initial work on adding a cftime-compatible date_range function Add docstring for xr.date_range Fix failing test Fix test skipping logic Coerce result of zip to a list in test setup Add and clean up tests Fix skip logic Skip roll_forward and roll_backward tests if cftime is not installed Expose all possible arguments to pd.date_range Add more detail to docstrings flake8 Add a what's new entry Add a short example to time-series.rst * Allow empty CFTimeIndexes; add calendar to CFTimeIndex repr * Enable CFTimeIndex constructor to optionally take date_range arguments * Simplify date_range to use new CFTimeIndex constructor * Rename xr.date_range to xr.cftime_range * Follow pandas behavior/naming for rollforward, rollback, and onOffset * Update docstring * Add pandas copyright notice to cftime_offsets.py * Check validity of offset constructor arguments * Fix TypeError versus ValueError uses * Use a module-level importorskip in test_cftime_offsets.py * Only return a CFTimeIndex from cftime_range * Keep CFTimeIndex constructor simple * Add some explicitly calendar-specific tests * Revert back to default repr * lint * return NotImplemented * Convert pandas copyright notices to comments * test_calendar_leap_year_length -> test_calendar_year_length * Use return NotImplemented in __apply__ too
1 parent a0b5af5 commit 5b87b6e

File tree

8 files changed

+1665
-21
lines changed

8 files changed

+1665
-21
lines changed

doc/api.rst

+7
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,13 @@ Custom Indexes
555555

556556
CFTimeIndex
557557

558+
Creating custom indexes
559+
-----------------------
560+
.. autosummary::
561+
:toctree: generated/
562+
563+
cftime_range
564+
558565
Plotting
559566
========
560567

doc/time-series.rst

+10-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,16 @@ coordinate with a no-leap calendar within a context manager setting the
258258
calendar, its times will be decoded into ``cftime.datetime`` objects,
259259
regardless of whether or not they can be represented using
260260
``np.datetime64[ns]`` objects.
261-
261+
262+
xarray also includes a :py:func:`cftime_range` function, which enables creating a
263+
``CFTimeIndex`` with regularly-spaced dates. For instance, we can create the
264+
same dates and DataArray we created above using:
265+
266+
.. ipython:: python
267+
268+
dates = xr.cftime_range(start='0001', periods=24, freq='MS', calendar='noleap')
269+
da = xr.DataArray(np.arange(24), coords=[dates], dims=['time'], name='foo')
270+
262271
For data indexed by a ``CFTimeIndex`` xarray currently supports:
263272

264273
- `Partial datetime string indexing`_ using strictly `ISO 8601-format`_ partial

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ Enhancements
5454
now displayed as `a b ... y z` rather than `a b c d ...`.
5555
(:issue:`1186`)
5656
By `Seth P <https://github.com/seth-p>`_.
57+
- A new CFTimeIndex-enabled :py:func:`cftime_range` function for use in
58+
generating dates from standard or non-standard calendars. By `Spencer Clark
59+
<https://github.com/spencerkclark>`_.
5760

5861
- When interpolating over a ``datetime64`` axis, you can now provide a datetime string instead of a ``datetime64`` object. E.g. ``da.interp(time='1991-02-01')``
5962
(:issue:`2284`)

xarray/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from .conventions import decode_cf, SerializationWarning
2828

29+
from .coding.cftime_offsets import cftime_range
2930
from .coding.cftimeindex import CFTimeIndex
3031

3132
from .util.print_versions import show_versions

0 commit comments

Comments
 (0)