22Routines for filling missing data.
33"""
44from functools import partial
5- from typing import Any , List , Optional , Set , Union
5+ from typing import TYPE_CHECKING , Any , List , Optional , Set , Union
66
77import numpy as np
88
1919)
2020from pandas .core .dtypes .missing import isna
2121
22+ if TYPE_CHECKING :
23+ from pandas import Index
24+
2225
2326def mask_missing (arr : ArrayLike , values_to_mask ) -> np .ndarray :
2427 """
@@ -155,7 +158,7 @@ def find_valid_index(values, how: str):
155158
156159
157160def 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