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

dropna() for a Series indexed by a CFTimeIndex #2734

Merged
merged 3 commits into from
Feb 2, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
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