@@ -128,6 +128,43 @@ def clean_interp_method(method, **kwargs):
128
128
return method
129
129
130
130
131
+ def find_valid_index (values , how : str ):
132
+ """
133
+ Retrieves the index of the first valid value.
134
+
135
+ Parameters
136
+ ----------
137
+ values : ndarray or ExtensionArray
138
+ how : {'first', 'last'}
139
+ Use this parameter to change between the first or last valid index.
140
+
141
+ Returns
142
+ -------
143
+ int or None
144
+ """
145
+ assert how in ["first" , "last" ]
146
+
147
+ if len (values ) == 0 : # early stop
148
+ return None
149
+
150
+ is_valid = ~ isna (values )
151
+
152
+ if values .ndim == 2 :
153
+ is_valid = is_valid .any (1 ) # reduce axis 1
154
+
155
+ if how == "first" :
156
+ idxpos = is_valid [::].argmax ()
157
+
158
+ if how == "last" :
159
+ idxpos = len (values ) - 1 - is_valid [::- 1 ].argmax ()
160
+
161
+ chk_notna = is_valid [idxpos ]
162
+
163
+ if not chk_notna :
164
+ return None
165
+ return idxpos
166
+
167
+
131
168
def interpolate_1d (
132
169
xvalues ,
133
170
yvalues ,
@@ -192,14 +229,10 @@ def interpolate_1d(
192
229
# default limit is unlimited GH #16282
193
230
limit = algos ._validate_limit (nobs = None , limit = limit )
194
231
195
- from pandas import Series
196
-
197
- ys = Series (yvalues )
198
-
199
232
# These are sets of index pointers to invalid values... i.e. {0, 1, etc...
200
233
all_nans = set (np .flatnonzero (invalid ))
201
- start_nans = set (range (ys . first_valid_index ( )))
202
- end_nans = set (range (1 + ys . last_valid_index ( ), len (valid )))
234
+ start_nans = set (range (find_valid_index ( yvalues , "first" )))
235
+ end_nans = set (range (1 + find_valid_index ( yvalues , "last" ), len (valid )))
203
236
mid_nans = all_nans - start_nans - end_nans
204
237
205
238
# Like the sets above, preserve_nans contains indices of invalid values,
0 commit comments