From b41d189801d2cd15227b8e43ac38c7096bc14ae0 Mon Sep 17 00:00:00 2001 From: Max Chen Date: Wed, 20 Nov 2019 23:32:01 +0800 Subject: [PATCH 1/4] add warning for not sorted index --- pandas/core/missing.py | 13 +++++++++++++ pandas/tests/series/test_missing.py | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/pandas/core/missing.py b/pandas/core/missing.py index fc54c03c042b7..4562f7d40775a 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -1,6 +1,8 @@ """ Routines for filling missing data. """ +import warnings + import numpy as np from pandas._libs import algos, lib @@ -19,6 +21,14 @@ from pandas.core.dtypes.missing import isna +class IndexNotSortedWarning(Warning): + """ + Warning raised when index not sorted and interpolate with unsupported method. + """ + + pass + + def mask_missing(arr, values_to_mask): """ Return a masking array of same size/shape as arr @@ -265,6 +275,9 @@ def interpolate_1d( inds = inds.view(np.int64) if inds.dtype == np.object_: inds = lib.maybe_convert_objects(inds) + if not np.all(np.diff(inds) > 0): + msg = "interpolation method '%s' requires sorted index." % method + warnings.warn(msg, IndexNotSortedWarning) else: inds = xvalues result[invalid] = np.interp(inds[invalid], inds[valid], yvalues[valid]) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 0751e1fb8b906..d7ba4678a7567 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -20,6 +20,7 @@ date_range, isna, ) +from pandas.core.missing import IndexNotSortedWarning from pandas.core.series import remove_na import pandas.util.testing as tm @@ -1661,3 +1662,9 @@ 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") From 1f355d9df62e4afaac4a73ea728998c3eab9a873 Mon Sep 17 00:00:00 2001 From: Max Chen Date: Wed, 27 Nov 2019 23:16:35 +0800 Subject: [PATCH 2/4] issue warning when interpolate with unsorted index GH21037 --- doc/source/whatsnew/v1.0.0.rst | 1 + pandas/core/missing.py | 8 ++++---- pandas/tests/series/test_missing.py | 2 ++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 54e54751a1f89..260d31fd9b93d 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -201,6 +201,7 @@ Other API changes See :ref:`units registration ` 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 when using ``index`` or ``values`` method but index is not sorted. (:issue:`21037`) - diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 4562f7d40775a..42ba1b8ce8263 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -23,7 +23,7 @@ class IndexNotSortedWarning(Warning): """ - Warning raised when index not sorted and interpolate with unsupported method. + Warning raised when index is not sorted and interpolate with an unsupported method. """ pass @@ -275,11 +275,11 @@ def interpolate_1d( inds = inds.view(np.int64) if inds.dtype == np.object_: inds = lib.maybe_convert_objects(inds) - if not np.all(np.diff(inds) > 0): - msg = "interpolation method '%s' requires sorted index." % method - warnings.warn(msg, IndexNotSortedWarning) 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 diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index d7ba4678a7567..47c58af36192e 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -1668,3 +1668,5 @@ def test_interpolate_warn_index_not_sorted(self): 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") From b17c0bc92e174dadbc842fdd662e21457ae77b79 Mon Sep 17 00:00:00 2001 From: Max Chen Date: Wed, 27 Nov 2019 23:19:35 +0800 Subject: [PATCH 3/4] issue warning when interpolate with unsorted index GH21037 --- doc/source/whatsnew/v1.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 260d31fd9b93d..e7441c4f064e1 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -201,7 +201,7 @@ Other API changes See :ref:`units registration ` 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 when using ``index`` or ``values`` method but index is not sorted. (:issue:`21037`) +- :meth:`Series.interpolate` will issue a warning if using ``index`` or ``values`` method when index is not sorted. (:issue:`21037`) - From 60f6a79eae6c9780e682089073835e1a4f1af174 Mon Sep 17 00:00:00 2001 From: Max Chen Date: Thu, 28 Nov 2019 00:12:17 +0800 Subject: [PATCH 4/4] resolve merge conflict --- pandas/tests/series/test_missing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 0f7f0ce52f27e..b5d173f70d06a 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -21,7 +21,6 @@ isna, ) from pandas.core.missing import IndexNotSortedWarning -from pandas.core.series import remove_na import pandas.util.testing as tm