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] ) 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])