Skip to content

Commit

Permalink
dropna() for a Series indexed by a CFTimeIndex (#2734)
Browse files Browse the repository at this point in the history
* Support dropna() for a Series indexed by a CFTimeIndex

* Add a what's new entry

* Use == instead of is
  • Loading branch information
spencerkclark authored and shoyer committed Feb 2, 2019
1 parent d634f64 commit a1ff90b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
5 changes: 3 additions & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ Enhancements
- Add ``tolerance`` option to ``resample()`` methods ``bfill``, ``pad``,
``nearest``. (:issue:`2695`)
By `Hauke Schulz <https://github.com/observingClouds>`_.

- :py:meth:`~xarray.DataArray.integrate` and
:py:meth:`~xarray.Dataset.integrate` are newly added.
See :ref:`_compute.using_coordinates` for the detail.
(:issue:`1332`)
By `Keisuke Fujii <https://github.com/fujiisoup>`_.

- :py:meth:`pandas.Series.dropna` is now supported for a
:py:class:`pandas.Series` indexed by a :py:class:`~xarray.CFTimeIndex`
(:issue:`2688`). By `Spencer Clark <https://github.com/spencerkclark>`_.

Bug fixes
~~~~~~~~~
Expand Down
8 changes: 5 additions & 3 deletions xarray/coding/cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,13 @@ def _maybe_cast_slice_bound(self, label, side, kind):
# e.g. series[1:5].
def get_value(self, series, key):
"""Adapted from pandas.tseries.index.DatetimeIndex.get_value"""
if not isinstance(key, slice):
return series.iloc[self.get_loc(key)]
else:
if np.asarray(key).dtype == np.dtype(bool):
return series.iloc[key]
elif isinstance(key, slice):
return series.iloc[self.slice_indexer(
key.start, key.stop, key.step)]
else:
return series.iloc[self.get_loc(key)]

def __contains__(self, key):
"""Adapted from
Expand Down
8 changes: 8 additions & 0 deletions xarray/tests/test_cftimeindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,14 @@ def test_indexing_in_series_iloc(series, index):
assert series.iloc[:2].equals(expected)


@pytest.mark.skipif(not has_cftime, reason='cftime not installed')
def test_series_dropna(index):
series = pd.Series([0., 1., np.nan, np.nan], index=index)
expected = series.iloc[:2]
result = series.dropna()
assert result.equals(expected)


@pytest.mark.skipif(not has_cftime, reason='cftime not installed')
def test_indexing_in_dataframe_loc(df, index, scalar_args, range_args):
expected = pd.Series([1], name=index[0])
Expand Down

0 comments on commit a1ff90b

Please sign in to comment.