Skip to content

ENH: warn Series.interpolate(method='index') with unsorted index #29887

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

Closed
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ Other API changes
See :ref:`units registration <whatsnew_1000.matplotlib_units>` for more.
- :meth:`Series.dropna` has dropped its ``**kwargs`` argument in favor of a single ``how`` parameter.
Supplying anything else than ``how`` to ``**kwargs`` raised a ``TypeError`` previously (:issue:`29388`)
- :meth:`Series.interpolate` will issue a warning if using ``index`` or ``values`` method when index is not sorted. (:issue:`21037`)
- When testing pandas, the new minimum required version of pytest is 5.0.1 (:issue:`29664`)
-

Expand Down
12 changes: 12 additions & 0 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Routines for filling missing data.
"""
import warnings

import numpy as np

Expand All @@ -21,6 +22,14 @@
from pandas.core.dtypes.missing import isna


class IndexNotSortedWarning(Warning):
"""
Warning raised when index is not sorted and interpolate with an unsupported method.
"""

pass


def mask_missing(arr, values_to_mask):
"""
Return a masking array of same size/shape as arr
Expand Down Expand Up @@ -277,6 +286,9 @@ def interpolate_1d(
inds = lib.maybe_convert_objects(inds)
else:
inds = xvalues
if not np.all(np.diff(inds) > 0):
msg = "interpolation method '%s' requires sorted index." % method
warnings.warn(msg, IndexNotSortedWarning)
result[invalid] = np.interp(inds[invalid], inds[valid], yvalues[valid])
result[preserve_nans] = np.nan
return result
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
date_range,
isna,
)
from pandas.core.missing import IndexNotSortedWarning
import pandas.util.testing as tm


Expand Down Expand Up @@ -1655,3 +1656,11 @@ def test_interpolate_timedelta_index(self, interp_methods_ind):
pytest.skip(
"This interpolation method is not supported for Timedelta Index yet."
)

def test_interpolate_warn_index_not_sorted(self):
# GH 21037
ts = pd.Series(data=[10, 9, np.nan, 2, 1], index=[10, 9, 3, 2, 1])
with tm.assert_produces_warning(IndexNotSortedWarning):
ts.interpolate(method="index")
with tm.assert_produces_warning(IndexNotSortedWarning):
ts.interpolate(method="values")