From f8d95bab1ec9e5f3ad0e7d44e504e8656951c14a Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Fri, 1 Feb 2019 07:57:25 -0500 Subject: [PATCH 1/3] Support dropna() for a Series indexed by a CFTimeIndex --- xarray/coding/cftimeindex.py | 8 +++++--- xarray/tests/test_cftimeindex.py | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index f1a05d31a0c..6e43d41d402 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 is 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]) From 47b489bc181dd8879e99e6ff90601871a4f2e5e8 Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Fri, 1 Feb 2019 08:18:32 -0500 Subject: [PATCH 2/3] Add a what's new entry --- doc/whats-new.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 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 ~~~~~~~~~ From 655b2c26c4fd1c32e481432375c696c519c1985e Mon Sep 17 00:00:00 2001 From: Spencer Clark Date: Fri, 1 Feb 2019 14:25:39 -0500 Subject: [PATCH 3/3] Use == instead of is --- xarray/coding/cftimeindex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/coding/cftimeindex.py b/xarray/coding/cftimeindex.py index 6e43d41d402..1456f8ce3b3 100644 --- a/xarray/coding/cftimeindex.py +++ b/xarray/coding/cftimeindex.py @@ -335,7 +335,7 @@ 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 np.asarray(key).dtype is np.dtype(bool): + if np.asarray(key).dtype == np.dtype(bool): return series.iloc[key] elif isinstance(key, slice): return series.iloc[self.slice_indexer(