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 2 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
11 changes: 1 addition & 10 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,7 @@ def get_interp_index(method, index: Index) -> Index:
# 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)))
index = Index(np.arange(len(index)))
else:
methods = {"index", "values", "nearest", "time"}
is_numeric_or_datetime = (
Expand Down
26 changes: 14 additions & 12 deletions pandas/tests/resample/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,22 @@ 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)
result = 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)
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(
np.arange(1 / 3, 0.7, 1 / 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