Skip to content
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

REGR: Interpolate with method=index #61183

Merged
merged 5 commits into from
Mar 29, 2025
Merged
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
15 changes: 3 additions & 12 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,18 +312,9 @@ def get_interp_index(method, index: Index) -> Index:
# create/use the index
if method == "linear":
# prior default
from pandas import Index

if isinstance(index.dtype, DatetimeTZDtype) or lib.is_np_dtype(
index.dtype, "mM"
):
# Convert datetime-like indexes to int64
index = Index(index.view("i8"))

elif not is_numeric_dtype(index.dtype):
# We keep behavior consistent with prior versions of pandas for
# non-numeric, non-datetime indexes
index = Index(range(len(index)))
from pandas import RangeIndex

index = RangeIndex(len(index))
else:
methods = {"index", "values", "nearest", "time"}
is_numeric_or_datetime = (
Expand Down
22 changes: 11 additions & 11 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,17 +897,17 @@ def interpolate(
to non-aligned timestamps, as in the following example:

>>> series.resample("400ms").interpolate("linear")
2023-03-01 07:00:00.000 1.0
2023-03-01 07:00:00.400 0.2
2023-03-01 07:00:00.800 -0.6
2023-03-01 07:00:01.200 -0.4
2023-03-01 07:00:01.600 0.8
2023-03-01 07:00:02.000 2.0
2023-03-01 07:00:02.400 1.6
2023-03-01 07:00:02.800 1.2
2023-03-01 07:00:03.200 1.4
2023-03-01 07:00:03.600 2.2
2023-03-01 07:00:04.000 3.0
2023-03-01 07:00:00.000 1.000000
2023-03-01 07:00:00.400 0.333333
2023-03-01 07:00:00.800 -0.333333
2023-03-01 07:00:01.200 0.000000
2023-03-01 07:00:01.600 1.000000
2023-03-01 07:00:02.000 2.000000
2023-03-01 07:00:02.400 1.666667
2023-03-01 07:00:02.800 1.333333
2023-03-01 07:00:03.200 1.666667
2023-03-01 07:00:03.600 2.333333
2023-03-01 07:00:04.000 3.000000
Freq: 400ms, dtype: float64

Note that the series correctly decreases between two anchors
Expand Down
28 changes: 14 additions & 14 deletions pandas/tests/resample/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,20 @@ def test_resample_interpolate_regular_sampling_off_grid(
ser = Series(np.arange(5.0), index)

method = all_1d_no_arg_interpolation_methods
# Resample to 1 hour sampling and interpolate with the given method
ser_resampled = ser.resample("1h").interpolate(method)

# Check that none of the resampled values are NaN, except the first one
# which lies 1 minute before the first actual data point
assert np.isnan(ser_resampled.iloc[0])
assert not ser_resampled.iloc[1:].isna().any()

if method not in ["nearest", "zero"]:
# Check that the resampled values are close to the expected values
# except for methods with known inaccuracies
assert np.all(
np.isclose(ser_resampled.values[1:], np.arange(0.5, 4.5, 0.5), rtol=1.0e-1)
)
result = ser.resample("1h").interpolate(method)

if method == "linear":
Copy link
Member Author

Choose a reason for hiding this comment

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

Only behavior change is with linear; the rest is just improving this test.

values = np.repeat(np.arange(0.0, 4.0), 2) + np.tile([1 / 3, 2 / 3], 4)
elif method == "nearest":
values = np.repeat(np.arange(0.0, 5.0), 2)[1:-1]
elif method == "zero":
values = np.repeat(np.arange(0.0, 4.0), 2)
else:
values = 0.491667 + np.arange(0.0, 4.0, 0.5)
values = np.insert(values, 0, np.nan)
index = date_range("2000-01-01 00:00:00", periods=9, freq="1h")
expected = Series(values, index=index)
tm.assert_series_equal(result, expected)


def test_resample_interpolate_irregular_sampling(all_1d_no_arg_interpolation_methods):
Expand Down
8 changes: 1 addition & 7 deletions pandas/tests/resample/test_time_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,7 @@ def test_groupby_resample_interpolate_with_apply_syntax_off_grid(groupy_test_df)
)

expected = DataFrame(
data={
"price": [
10.0,
9.21131,
11.0,
]
},
data={"price": [10.0, 9.5, 11.0]},
index=expected_ind,
)
tm.assert_frame_equal(result, expected, check_names=False)
2 changes: 1 addition & 1 deletion pandas/tests/series/methods/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def test_nan_interpolate(self, kwargs):
def test_nan_irregular_index(self):
s = Series([1, 2, np.nan, 4], index=[1, 3, 5, 9])
result = s.interpolate()
expected = Series([1.0, 2.0, 2.6666666666666665, 4.0], index=[1, 3, 5, 9])
expected = Series([1.0, 2.0, 3.0, 4.0], index=[1, 3, 5, 9])
tm.assert_series_equal(result, expected)

def test_nan_str_index(self):
Expand Down
Loading