Skip to content

Commit 9310cb7

Browse files
authored
TYP/BUG: fix passing incorrect type to interpolate_1d (pandas-dev#38306)
1 parent 0c004fc commit 9310cb7

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

pandas/core/generic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7182,6 +7182,7 @@ def interpolate(
71827182
if method == "linear":
71837183
# prior default
71847184
index = np.arange(len(obj.index))
7185+
index = Index(index)
71857186
else:
71867187
index = obj.index
71877188
methods = {"index", "values", "nearest", "time"}

pandas/core/missing.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Routines for filling missing data.
33
"""
44
from functools import partial
5-
from typing import Any, List, Optional, Set, Union
5+
from typing import TYPE_CHECKING, Any, List, Optional, Set, Union
66

77
import numpy as np
88

@@ -19,6 +19,9 @@
1919
)
2020
from pandas.core.dtypes.missing import isna
2121

22+
if TYPE_CHECKING:
23+
from pandas import Index
24+
2225

2326
def mask_missing(arr: ArrayLike, values_to_mask) -> np.ndarray:
2427
"""
@@ -155,7 +158,7 @@ def find_valid_index(values, how: str):
155158

156159

157160
def interpolate_1d(
158-
xvalues: np.ndarray,
161+
xvalues: "Index",
159162
yvalues: np.ndarray,
160163
method: Optional[str] = "linear",
161164
limit: Optional[int] = None,
@@ -177,18 +180,15 @@ def interpolate_1d(
177180
valid = ~invalid
178181

179182
if not valid.any():
180-
# have to call np.asarray(xvalues) since xvalues could be an Index
181-
# which can't be mutated
182-
result = np.empty_like(np.asarray(xvalues), dtype=np.float64)
183+
result = np.empty(xvalues.shape, dtype=np.float64)
183184
result.fill(np.nan)
184185
return result
185186

186187
if valid.all():
187188
return yvalues
188189

189190
if method == "time":
190-
if not getattr(xvalues, "_is_all_dates", None):
191-
# if not issubclass(xvalues.dtype.type, np.datetime64):
191+
if not needs_i8_conversion(xvalues.dtype):
192192
raise ValueError(
193193
"time-weighted interpolation only works "
194194
"on Series or DataFrames with a "
@@ -252,20 +252,18 @@ def interpolate_1d(
252252
# sort preserve_nans and covert to list
253253
preserve_nans = sorted(preserve_nans)
254254

255-
yvalues = getattr(yvalues, "values", yvalues)
256255
result = yvalues.copy()
257256

258-
# xvalues to pass to NumPy/SciPy
257+
# xarr to pass to NumPy/SciPy
258+
xarr = xvalues._values
259+
if needs_i8_conversion(xarr.dtype):
260+
# GH#1646 for dt64tz
261+
xarr = xarr.view("i8")
259262

260-
xvalues = getattr(xvalues, "values", xvalues)
261263
if method == "linear":
262-
inds = xvalues
264+
inds = xarr
263265
else:
264-
inds = np.asarray(xvalues)
265-
266-
# hack for DatetimeIndex, #1646
267-
if needs_i8_conversion(inds.dtype):
268-
inds = inds.view(np.int64)
266+
inds = np.asarray(xarr)
269267

270268
if method in ("values", "index"):
271269
if inds.dtype == np.object_:

0 commit comments

Comments
 (0)