Skip to content

BUG: fix Series.interpolate(method='index') with unsorted index #29943

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

Conversation

immaxchen
Copy link
Contributor

Series.interpolate(method='index') use numpy.interp under the hood, which cannot work with unsorted X values. Here we sort the known data for numpy.interp.

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

def test_interpolate_unsorted_index(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add all of the examples from the OP.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can likely parameterize this test

# GH 21037
ts = pd.Series(data=[10, 9, np.nan, 2, 1], index=[10, 9, 3, 2, 1])
result = ts.interpolate(method="index").sort_index(ascending=True)
expected = ts.sort_index(ascending=True).interpolate(method="index")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you hard-code the expected result (e.g. don't use interpolate)

@@ -277,7 +277,10 @@ def interpolate_1d(
inds = lib.maybe_convert_objects(inds)
else:
inds = xvalues
result[invalid] = np.interp(inds[invalid], inds[valid], yvalues[valid])
seq = np.argsort(inds[valid])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment here on what you are doing

@@ -277,7 +277,10 @@ def interpolate_1d(
inds = lib.maybe_convert_objects(inds)
else:
inds = xvalues
result[invalid] = np.interp(inds[invalid], inds[valid], yvalues[valid])
seq = np.argsort(inds[valid])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call this indexer

@@ -550,6 +550,7 @@ Numeric
- Bug in :class:`UInt64Index` precision loss while constructing from a list with values in the ``np.uint64`` range (:issue:`29526`)
- Bug in :class:`NumericIndex` construction that caused indexing to fail when integers in the ``np.uint64`` range were used (:issue:`28023`)
- Bug in :class:`NumericIndex` construction that caused :class:`UInt64Index` to be casted to :class:`Float64Index` when integers in the ``np.uint64`` range were used to index a :class:`DataFrame` (:issue:`28279`)
- Bug in :meth:`Series.interpolate` when using method ``index`` with unsorted index. (:issue:`21037`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method=`index` with an unsorted index, would previously return incorrect results.

@jreback jreback added the Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate label Dec 1, 2019
@immaxchen
Copy link
Contributor Author

Hi @jreback, all done, please have a look.

@jreback jreback added this to the 1.0 milestone Dec 3, 2019
@jreback
Copy link
Contributor

jreback commented Dec 3, 2019

lgtm. @TomAugspurger if any comments

@TomAugspurger
Copy link
Contributor

Thanks!

@TomAugspurger TomAugspurger merged commit 386494d into pandas-dev:master Dec 3, 2019
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
…as-dev#29943)

* fix series interpolate bug with unsorted index GH21037
proost pushed a commit to proost/pandas that referenced this pull request Dec 19, 2019
…as-dev#29943)

* fix series interpolate bug with unsorted index GH21037
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

raise warning when calling .interpolate(method='index') when Index is not sorted with ascending=True
3 participants