From 6009daa29acd40b91a2adb372f458d5402015cba Mon Sep 17 00:00:00 2001 From: ben Date: Sat, 14 Aug 2021 09:56:28 -0700 Subject: [PATCH 1/2] BUG: interpolation with duplicate index values fails (#42585) --- pandas/core/missing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/missing.py b/pandas/core/missing.py index 1a07b5614eb38..abf848d7070f4 100644 --- a/pandas/core/missing.py +++ b/pandas/core/missing.py @@ -430,7 +430,7 @@ def _interpolate_1d( if method in NP_METHODS: # np.interp requires sorted X values, #21037 - indexer = np.argsort(indices[valid]) + indexer = np.argsort(indices[valid], kind="stable") result[invalid] = np.interp( indices[invalid], indices[valid][indexer], yvalues[valid][indexer] ) From bfa0a89c4ebff0e8b56d12d0d25704a3f8781247 Mon Sep 17 00:00:00 2001 From: ben Date: Sat, 14 Aug 2021 10:37:38 -0700 Subject: [PATCH 2/2] add unit tests --- pandas/tests/series/methods/test_interpolate.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pandas/tests/series/methods/test_interpolate.py b/pandas/tests/series/methods/test_interpolate.py index 8ca2d37016691..8a5961f06a498 100644 --- a/pandas/tests/series/methods/test_interpolate.py +++ b/pandas/tests/series/methods/test_interpolate.py @@ -812,6 +812,23 @@ def test_interpolate_unsorted_index(self, ascending, expected_values): expected = Series(data=expected_values, index=expected_values, dtype=float) tm.assert_series_equal(result, expected) + def test_interpolate_duplicate_index_decreasing(self): + # GH 42585 + ts = Series(data=[-200, -50, np.nan, -50, 1000], index=[79, 79, 0, -61, -2783]) + result = ts.interpolate(method="index") + assert result.loc[0] == -50 + # TODO: also check validity of sort assumption + + def test_interpolate_duplicate_index_increasing(self): + # GH 42585 + ts = Series( + data=[0, 0, 0, 0, 0, 0, 0, -50, np.nan, -50, 0, 0, 0, 0, 0, 0, 0, 0], + index=[1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 6, 7, 7, 8, 8, 9, 10, 11], + ) + result = ts.interpolate(method="index") + assert result.loc[0] == -50 + # TODO: check validity of sort assumption? + def test_interpolate_pos_args_deprecation(self): # https://github.com/pandas-dev/pandas/issues/41485 ser = Series([1, 2, 3])