From a1ff90be63667ac4384ec74e82406dbcd1e05165 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Sat, 2 Feb 2019 01:56:12 -0500 Subject: [PATCH] dropna() for a Series indexed by a CFTimeIndex (#2734) * Support dropna() for a Series indexed by a CFTimeIndex * Add a what's new entry * Use == instead of is --- doc/whats-new.rst | 5 +++-- xarray/coding/cftimeindex.py | 8 +++++--- xarray/tests/test_cftimeindex.py | 8 ++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 0149d119595..169950813fd 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -50,13 +50,14 @@ Enhancements - Add ``tolerance`` option to ``resample()`` methods ``bfill``, ``pad``, ``nearest``. (:issue:`2695`) By `Hauke Schulz `_. - - :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 `_. - +- :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 `_. Bug fixes ~~~~~~~~~ diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index f1a05d31a0c..1456f8ce3b3 100644 --- a/xarray/coding/cftimeindex.py +++ b/xarray/coding/cftimeindex.py @@ -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 diff --git a/xarray/tests/test_cftimeindex.py b/xarray/tests/test_cftimeindex.py index 97be993d842..358c9df0497 100644 --- a/xarray/tests/test_cftimeindex.py +++ b/xarray/tests/test_cftimeindex.py @@ -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])