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 CFTimeIndex.shift #2431

Merged
merged 6 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions doc/api-hidden.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,5 @@
plot.FacetGrid.set_titles
plot.FacetGrid.set_ticks
plot.FacetGrid.map

CFTimeIndex.shift
13 changes: 13 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,25 @@ Enhancements
- Added :py:meth:`~xarray.CFTimeIndex.shift` for shifting the values of a
CFTimeIndex by a specified frequency. (:issue:`2244`). By `Spencer Clark
<https://github.com/spencerkclark>`_.
- Added support for using ``cftime.datetime`` coordinates with
:py:meth:`~xarray.DataArray.differentiate`,
:py:meth:`~xarray.Dataset.differentiate`,
:py:meth:`~xarray.DataArray.interp`, and
:py:meth:`~xarray.Dataset.interp`.
By `Spencer Clark <https://github.com/spencerkclark>`_

Bug fixes
~~~~~~~~~

- Addition and subtraction operators used with a CFTimeIndex now preserve the
index's type. (:issue:`2244`). By `Spencer Clark <https://github.com/spencerkclark>`_.
- ``xarray.DataArray.roll`` correctly handles multidimensional arrays.
(:issue:`2445`)
By `Keisuke Fujii <https://github.com/fujiisoup>`_.

- ``xarray.DataArray.std()`` now correctly accepts ``ddof`` keyword argument.
(:issue:`2240`)
By `Keisuke Fujii <https://github.com/fujiisoup>`_.

.. _whats-new.0.10.9:

Expand Down
32 changes: 30 additions & 2 deletions xarray/coding/cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def contains(self, key):
def shift(self, n, freq):
"""Shift the CFTimeIndex a multiple of the given frequency.

See the documentation for :py:func:`~xarray.cftime_range()` for a
See the documentation for :py:func:`~xarray.cftime_range` for a
complete listing of valid frequency strings.

Parameters
Expand All @@ -338,7 +338,6 @@ def shift(self, n, freq):

Examples
--------

>>> index = xr.cftime_range('2000', periods=1, freq='M')
>>> index
CFTimeIndex([2000-01-31 00:00:00], dtype='object')
Expand Down Expand Up @@ -366,3 +365,32 @@ def __radd__(self, other):

def __sub__(self, other):
return CFTimeIndex(np.array(self) - other)


def _parse_iso8601_without_reso(date_type, datetime_str):
date, _ = _parse_iso8601_with_reso(date_type, datetime_str)
return date


def _parse_array_of_cftime_strings(strings, date_type):
"""Create a numpy array from an array of strings.

For use in generating dates from strings for use with interp. Assumes the
array is either 0-dimensional or 1-dimensional.

Parameters
----------
strings : array of strings
Strings to convert to dates
date_type : cftime.datetime type
Calendar type to use for dates

Returns
-------
np.array
"""
if strings.ndim == 0:
return np.array(_parse_iso8601_without_reso(date_type, strings.item()))
else:
return np.array([_parse_iso8601_without_reso(date_type, s)
Copy link
Member

Choose a reason for hiding this comment

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

The cleanest way to do this is probably to flatten and then reshape the result, e.g.,
`np.array([_parse_iso8601_without_reso(date_type, s) for s in strings.ravel()]).reshape(strings.shape)

Copy link
Member

Choose a reason for hiding this comment

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

nevermind, let's fix this later -- this was only from the merge commit :)

Copy link
Member Author

Choose a reason for hiding this comment

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

No worries -- that probably is better. It snuck in with #2434. I'll submit a fix in the next few days.

for s in strings])
18 changes: 18 additions & 0 deletions xarray/tests/test_cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,21 @@ def test_cftimeindex_shift_invalid_freq():
index = xr.cftime_range('2000', periods=3)
with pytest.raises(TypeError):
index.shift(1, 1)


@requires_cftime
def test_parse_array_of_cftime_strings():
from cftime import DatetimeNoLeap

strings = np.array(['2000-01-01', '2000-01-02'])
expected = np.array([DatetimeNoLeap(2000, 1, 1),
DatetimeNoLeap(2000, 1, 2)])

result = _parse_array_of_cftime_strings(strings, DatetimeNoLeap)
np.testing.assert_array_equal(result, expected)

# Test scalar array case
strings = np.array('2000-01-01')
expected = np.array(DatetimeNoLeap(2000, 1, 1))
result = _parse_array_of_cftime_strings(strings, DatetimeNoLeap)
np.testing.assert_array_equal(result, expected)