@@ -214,9 +214,11 @@ def interpolate_array_2d(
214
214
coerce : bool = False ,
215
215
downcast : str | None = None ,
216
216
** kwargs ,
217
- ):
217
+ ) -> np . ndarray :
218
218
"""
219
219
Wrapper to dispatch to either interpolate_2d or _interpolate_2d_with_fill.
220
+
221
+ Returned ndarray has same dtype as 'data'.
220
222
"""
221
223
try :
222
224
m = clean_fill_method (method )
@@ -228,13 +230,14 @@ def interpolate_array_2d(
228
230
# similar to validate_fillna_kwargs
229
231
raise ValueError ("Cannot pass both fill_value and method" )
230
232
231
- interp_values = interpolate_2d (
233
+ interpolate_2d (
232
234
data ,
233
235
method = m ,
234
236
axis = axis ,
235
237
limit = limit ,
236
238
limit_area = limit_area ,
237
239
)
240
+ interp_values = data
238
241
else :
239
242
assert index is not None # for mypy
240
243
@@ -687,14 +690,14 @@ def _cubicspline_interpolate(xi, yi, x, axis=0, bc_type="not-a-knot", extrapolat
687
690
688
691
689
692
def _interpolate_with_limit_area (
690
- values : ArrayLike , method : str , limit : int | None , limit_area : str | None
691
- ) -> ArrayLike :
693
+ values : np . ndarray , method : str , limit : int | None , limit_area : str | None
694
+ ) -> None :
692
695
"""
693
696
Apply interpolation and limit_area logic to values along a to-be-specified axis.
694
697
695
698
Parameters
696
699
----------
697
- values: array-like
700
+ values: np.ndarray
698
701
Input array.
699
702
method: str
700
703
Interpolation method. Could be "bfill" or "pad"
@@ -703,10 +706,9 @@ def _interpolate_with_limit_area(
703
706
limit_area: str
704
707
Limit area for interpolation. Can be "inside" or "outside"
705
708
706
- Returns
707
- -------
708
- values: array-like
709
- Interpolated array.
709
+ Notes
710
+ -----
711
+ Modifies values in-place.
710
712
"""
711
713
712
714
invalid = isna (values )
@@ -719,7 +721,7 @@ def _interpolate_with_limit_area(
719
721
if last is None :
720
722
last = len (values )
721
723
722
- values = interpolate_2d (
724
+ interpolate_2d (
723
725
values ,
724
726
method = method ,
725
727
limit = limit ,
@@ -732,23 +734,23 @@ def _interpolate_with_limit_area(
732
734
733
735
values [invalid ] = np .nan
734
736
735
- return values
737
+ return
736
738
737
739
738
740
def interpolate_2d (
739
- values ,
741
+ values : np . ndarray ,
740
742
method : str = "pad" ,
741
743
axis : Axis = 0 ,
742
744
limit : int | None = None ,
743
745
limit_area : str | None = None ,
744
- ):
746
+ ) -> None :
745
747
"""
746
748
Perform an actual interpolation of values, values will be make 2-d if
747
749
needed fills inplace, returns the result.
748
750
749
751
Parameters
750
752
----------
751
- values: array-like
753
+ values: np.ndarray
752
754
Input array.
753
755
method: str, default "pad"
754
756
Interpolation method. Could be "bfill" or "pad"
@@ -759,13 +761,12 @@ def interpolate_2d(
759
761
limit_area: str, optional
760
762
Limit area for interpolation. Can be "inside" or "outside"
761
763
762
- Returns
763
- -------
764
- values: array-like
765
- Interpolated array.
764
+ Notes
765
+ -----
766
+ Modifies values in-place.
766
767
"""
767
768
if limit_area is not None :
768
- return np .apply_along_axis (
769
+ np .apply_along_axis (
769
770
partial (
770
771
_interpolate_with_limit_area ,
771
772
method = method ,
@@ -775,32 +776,31 @@ def interpolate_2d(
775
776
axis ,
776
777
values ,
777
778
)
779
+ return
778
780
779
781
transf = (lambda x : x ) if axis == 0 else (lambda x : x .T )
780
782
781
783
# reshape a 1 dim if needed
782
- ndim = values .ndim
783
784
if values .ndim == 1 :
784
785
if axis != 0 : # pragma: no cover
785
786
raise AssertionError ("cannot interpolate on a ndim == 1 with axis != 0" )
786
787
values = values .reshape (tuple ((1 ,) + values .shape ))
787
788
788
789
method = clean_fill_method (method )
789
790
tvalues = transf (values )
791
+
792
+ # _pad_2d and _backfill_2d both modify tvalues inplace
790
793
if method == "pad" :
791
- result , _ = _pad_2d (tvalues , limit = limit )
794
+ _pad_2d (tvalues , limit = limit )
792
795
else :
793
- result , _ = _backfill_2d (tvalues , limit = limit )
794
-
795
- result = transf (result )
796
- # reshape back
797
- if ndim == 1 :
798
- result = result [0 ]
796
+ _backfill_2d (tvalues , limit = limit )
799
797
800
- return result
798
+ return
801
799
802
800
803
- def _fillna_prep (values , mask : np .ndarray | None = None ) -> np .ndarray :
801
+ def _fillna_prep (
802
+ values , mask : npt .NDArray [np .bool_ ] | None = None
803
+ ) -> npt .NDArray [np .bool_ ]:
804
804
# boilerplate for _pad_1d, _backfill_1d, _pad_2d, _backfill_2d
805
805
806
806
if mask is None :
@@ -834,8 +834,8 @@ def new_func(values, limit=None, mask=None):
834
834
def _pad_1d (
835
835
values : np .ndarray ,
836
836
limit : int | None = None ,
837
- mask : np .ndarray | None = None ,
838
- ) -> tuple [np .ndarray , np .ndarray ]:
837
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
838
+ ) -> tuple [np .ndarray , npt . NDArray [ np .bool_ ] ]:
839
839
mask = _fillna_prep (values , mask )
840
840
algos .pad_inplace (values , mask , limit = limit )
841
841
return values , mask
@@ -845,15 +845,15 @@ def _pad_1d(
845
845
def _backfill_1d (
846
846
values : np .ndarray ,
847
847
limit : int | None = None ,
848
- mask : np .ndarray | None = None ,
849
- ) -> tuple [np .ndarray , np .ndarray ]:
848
+ mask : npt . NDArray [ np .bool_ ] | None = None ,
849
+ ) -> tuple [np .ndarray , npt . NDArray [ np .bool_ ] ]:
850
850
mask = _fillna_prep (values , mask )
851
851
algos .backfill_inplace (values , mask , limit = limit )
852
852
return values , mask
853
853
854
854
855
855
@_datetimelike_compat
856
- def _pad_2d (values , limit = None , mask = None ):
856
+ def _pad_2d (values : np . ndarray , limit = None , mask : npt . NDArray [ np . bool_ ] | None = None ):
857
857
mask = _fillna_prep (values , mask )
858
858
859
859
if np .all (values .shape ):
@@ -865,7 +865,7 @@ def _pad_2d(values, limit=None, mask=None):
865
865
866
866
867
867
@_datetimelike_compat
868
- def _backfill_2d (values , limit = None , mask = None ):
868
+ def _backfill_2d (values , limit = None , mask : npt . NDArray [ np . bool_ ] | None = None ):
869
869
mask = _fillna_prep (values , mask )
870
870
871
871
if np .all (values .shape ):
@@ -890,7 +890,7 @@ def clean_reindex_fill_method(method):
890
890
return clean_fill_method (method , allow_nearest = True )
891
891
892
892
893
- def _interp_limit (invalid : np .ndarray , fw_limit , bw_limit ):
893
+ def _interp_limit (invalid : npt . NDArray [ np .bool_ ] , fw_limit , bw_limit ):
894
894
"""
895
895
Get indexers of values that won't be filled
896
896
because they exceed the limits.
@@ -955,7 +955,7 @@ def inner(invalid, limit):
955
955
return f_idx & b_idx
956
956
957
957
958
- def _rolling_window (a : np .ndarray , window : int ):
958
+ def _rolling_window (a : npt . NDArray [ np .bool_ ] , window : int ) -> npt . NDArray [ np . bool_ ] :
959
959
"""
960
960
[True, True, False, True, False], 2 ->
961
961
0 commit comments