Skip to content

Commit b69a2ae

Browse files
authored
REGR: Interpolate with method=index (#61183)
* REGR: Interpolate with method=index * Magic trailing comma * perf * Update docstring
1 parent 543680d commit b69a2ae

File tree

5 files changed

+30
-45
lines changed

5 files changed

+30
-45
lines changed

pandas/core/missing.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,9 @@ def get_interp_index(method, index: Index) -> Index:
312312
# create/use the index
313313
if method == "linear":
314314
# prior default
315-
from pandas import Index
316-
317-
if isinstance(index.dtype, DatetimeTZDtype) or lib.is_np_dtype(
318-
index.dtype, "mM"
319-
):
320-
# Convert datetime-like indexes to int64
321-
index = Index(index.view("i8"))
322-
323-
elif not is_numeric_dtype(index.dtype):
324-
# We keep behavior consistent with prior versions of pandas for
325-
# non-numeric, non-datetime indexes
326-
index = Index(range(len(index)))
315+
from pandas import RangeIndex
316+
317+
index = RangeIndex(len(index))
327318
else:
328319
methods = {"index", "values", "nearest", "time"}
329320
is_numeric_or_datetime = (

pandas/core/resample.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -897,17 +897,17 @@ def interpolate(
897897
to non-aligned timestamps, as in the following example:
898898
899899
>>> series.resample("400ms").interpolate("linear")
900-
2023-03-01 07:00:00.000 1.0
901-
2023-03-01 07:00:00.400 0.2
902-
2023-03-01 07:00:00.800 -0.6
903-
2023-03-01 07:00:01.200 -0.4
904-
2023-03-01 07:00:01.600 0.8
905-
2023-03-01 07:00:02.000 2.0
906-
2023-03-01 07:00:02.400 1.6
907-
2023-03-01 07:00:02.800 1.2
908-
2023-03-01 07:00:03.200 1.4
909-
2023-03-01 07:00:03.600 2.2
910-
2023-03-01 07:00:04.000 3.0
900+
2023-03-01 07:00:00.000 1.000000
901+
2023-03-01 07:00:00.400 0.333333
902+
2023-03-01 07:00:00.800 -0.333333
903+
2023-03-01 07:00:01.200 0.000000
904+
2023-03-01 07:00:01.600 1.000000
905+
2023-03-01 07:00:02.000 2.000000
906+
2023-03-01 07:00:02.400 1.666667
907+
2023-03-01 07:00:02.800 1.333333
908+
2023-03-01 07:00:03.200 1.666667
909+
2023-03-01 07:00:03.600 2.333333
910+
2023-03-01 07:00:04.000 3.000000
911911
Freq: 400ms, dtype: float64
912912
913913
Note that the series correctly decreases between two anchors

pandas/tests/resample/test_base.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,20 @@ def test_resample_interpolate_regular_sampling_off_grid(
123123
ser = Series(np.arange(5.0), index)
124124

125125
method = all_1d_no_arg_interpolation_methods
126-
# Resample to 1 hour sampling and interpolate with the given method
127-
ser_resampled = ser.resample("1h").interpolate(method)
128-
129-
# Check that none of the resampled values are NaN, except the first one
130-
# which lies 1 minute before the first actual data point
131-
assert np.isnan(ser_resampled.iloc[0])
132-
assert not ser_resampled.iloc[1:].isna().any()
133-
134-
if method not in ["nearest", "zero"]:
135-
# Check that the resampled values are close to the expected values
136-
# except for methods with known inaccuracies
137-
assert np.all(
138-
np.isclose(ser_resampled.values[1:], np.arange(0.5, 4.5, 0.5), rtol=1.0e-1)
139-
)
126+
result = ser.resample("1h").interpolate(method)
127+
128+
if method == "linear":
129+
values = np.repeat(np.arange(0.0, 4.0), 2) + np.tile([1 / 3, 2 / 3], 4)
130+
elif method == "nearest":
131+
values = np.repeat(np.arange(0.0, 5.0), 2)[1:-1]
132+
elif method == "zero":
133+
values = np.repeat(np.arange(0.0, 4.0), 2)
134+
else:
135+
values = 0.491667 + np.arange(0.0, 4.0, 0.5)
136+
values = np.insert(values, 0, np.nan)
137+
index = date_range("2000-01-01 00:00:00", periods=9, freq="1h")
138+
expected = Series(values, index=index)
139+
tm.assert_series_equal(result, expected)
140140

141141

142142
def test_resample_interpolate_irregular_sampling(all_1d_no_arg_interpolation_methods):

pandas/tests/resample/test_time_grouper.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -430,13 +430,7 @@ def test_groupby_resample_interpolate_with_apply_syntax_off_grid(groupy_test_df)
430430
)
431431

432432
expected = DataFrame(
433-
data={
434-
"price": [
435-
10.0,
436-
9.21131,
437-
11.0,
438-
]
439-
},
433+
data={"price": [10.0, 9.5, 11.0]},
440434
index=expected_ind,
441435
)
442436
tm.assert_frame_equal(result, expected, check_names=False)

pandas/tests/series/methods/test_interpolate.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def test_nan_interpolate(self, kwargs):
270270
def test_nan_irregular_index(self):
271271
s = Series([1, 2, np.nan, 4], index=[1, 3, 5, 9])
272272
result = s.interpolate()
273-
expected = Series([1.0, 2.0, 2.6666666666666665, 4.0], index=[1, 3, 5, 9])
273+
expected = Series([1.0, 2.0, 3.0, 4.0], index=[1, 3, 5, 9])
274274
tm.assert_series_equal(result, expected)
275275

276276
def test_nan_str_index(self):

0 commit comments

Comments
 (0)