2
2
Routines for filling missing data.
3
3
"""
4
4
from functools import partial
5
- from typing import Any , List , Optional , Set , Union
5
+ from typing import TYPE_CHECKING , Any , List , Optional , Set , Union
6
6
7
7
import numpy as np
8
8
19
19
)
20
20
from pandas .core .dtypes .missing import isna
21
21
22
+ if TYPE_CHECKING :
23
+ from pandas import Index
24
+
22
25
23
26
def mask_missing (arr : ArrayLike , values_to_mask ) -> np .ndarray :
24
27
"""
@@ -155,7 +158,7 @@ def find_valid_index(values, how: str):
155
158
156
159
157
160
def interpolate_1d (
158
- xvalues : np . ndarray ,
161
+ xvalues : "Index" ,
159
162
yvalues : np .ndarray ,
160
163
method : Optional [str ] = "linear" ,
161
164
limit : Optional [int ] = None ,
@@ -177,18 +180,15 @@ def interpolate_1d(
177
180
valid = ~ invalid
178
181
179
182
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 )
183
184
result .fill (np .nan )
184
185
return result
185
186
186
187
if valid .all ():
187
188
return yvalues
188
189
189
190
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 ):
192
192
raise ValueError (
193
193
"time-weighted interpolation only works "
194
194
"on Series or DataFrames with a "
@@ -252,20 +252,18 @@ def interpolate_1d(
252
252
# sort preserve_nans and covert to list
253
253
preserve_nans = sorted (preserve_nans )
254
254
255
- yvalues = getattr (yvalues , "values" , yvalues )
256
255
result = yvalues .copy ()
257
256
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" )
259
262
260
- xvalues = getattr (xvalues , "values" , xvalues )
261
263
if method == "linear" :
262
- inds = xvalues
264
+ inds = xarr
263
265
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 )
269
267
270
268
if method in ("values" , "index" ):
271
269
if inds .dtype == np .object_ :
0 commit comments