Skip to content

Commit e45b348

Browse files
TYP: some type annotations for interpolate (#34631)
1 parent 69cc851 commit e45b348

File tree

3 files changed

+58
-49
lines changed

3 files changed

+58
-49
lines changed

pandas/core/generic.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -6863,16 +6863,16 @@ def replace(
68636863

68646864
@Appender(_shared_docs["interpolate"] % _shared_doc_kwargs)
68656865
def interpolate(
6866-
self,
6867-
method="linear",
6868-
axis=0,
6869-
limit=None,
6870-
inplace=False,
6871-
limit_direction="forward",
6872-
limit_area=None,
6873-
downcast=None,
6866+
self: FrameOrSeries,
6867+
method: str = "linear",
6868+
axis: Axis = 0,
6869+
limit: Optional[int] = None,
6870+
inplace: bool_t = False,
6871+
limit_direction: str = "forward",
6872+
limit_area: Optional[str] = None,
6873+
downcast: Optional[str] = None,
68746874
**kwargs,
6875-
):
6875+
) -> Optional[FrameOrSeries]:
68766876
"""
68776877
Interpolate values according to different methods.
68786878
"""

pandas/core/internals/blocks.py

+36-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime, timedelta
22
import inspect
33
import re
4-
from typing import Any, List
4+
from typing import TYPE_CHECKING, Any, List, Optional
55
import warnings
66

77
import numpy as np
@@ -83,6 +83,9 @@
8383
import pandas.core.missing as missing
8484
from pandas.core.nanops import nanpercentile
8585

86+
if TYPE_CHECKING:
87+
from pandas import Index
88+
8689

8790
class Block(PandasObject):
8891
"""
@@ -1066,16 +1069,16 @@ def coerce_to_target_dtype(self, other):
10661069

10671070
def interpolate(
10681071
self,
1069-
method="pad",
1070-
axis=0,
1071-
index=None,
1072-
inplace=False,
1073-
limit=None,
1074-
limit_direction="forward",
1075-
limit_area=None,
1076-
fill_value=None,
1077-
coerce=False,
1078-
downcast=None,
1072+
method: str = "pad",
1073+
axis: int = 0,
1074+
index: Optional["Index"] = None,
1075+
inplace: bool = False,
1076+
limit: Optional[int] = None,
1077+
limit_direction: str = "forward",
1078+
limit_area: Optional[str] = None,
1079+
fill_value: Optional[Any] = None,
1080+
coerce: bool = False,
1081+
downcast: Optional[str] = None,
10791082
**kwargs,
10801083
):
10811084

@@ -1115,6 +1118,9 @@ def check_int_bool(self, inplace):
11151118
r = check_int_bool(self, inplace)
11161119
if r is not None:
11171120
return r
1121+
1122+
assert index is not None # for mypy
1123+
11181124
return self._interpolate(
11191125
method=m,
11201126
index=index,
@@ -1130,13 +1136,13 @@ def check_int_bool(self, inplace):
11301136

11311137
def _interpolate_with_fill(
11321138
self,
1133-
method="pad",
1134-
axis=0,
1135-
inplace=False,
1136-
limit=None,
1137-
fill_value=None,
1138-
coerce=False,
1139-
downcast=None,
1139+
method: str = "pad",
1140+
axis: int = 0,
1141+
inplace: bool = False,
1142+
limit: Optional[int] = None,
1143+
fill_value: Optional[Any] = None,
1144+
coerce: bool = False,
1145+
downcast: Optional[str] = None,
11401146
) -> List["Block"]:
11411147
""" fillna but using the interpolate machinery """
11421148
inplace = validate_bool_kwarg(inplace, "inplace")
@@ -1169,15 +1175,15 @@ def _interpolate_with_fill(
11691175

11701176
def _interpolate(
11711177
self,
1172-
method=None,
1173-
index=None,
1174-
fill_value=None,
1175-
axis=0,
1176-
limit=None,
1177-
limit_direction="forward",
1178-
limit_area=None,
1179-
inplace=False,
1180-
downcast=None,
1178+
method: str,
1179+
index: "Index",
1180+
fill_value: Optional[Any] = None,
1181+
axis: int = 0,
1182+
limit: Optional[int] = None,
1183+
limit_direction: str = "forward",
1184+
limit_area: Optional[str] = None,
1185+
inplace: bool = False,
1186+
downcast: Optional[str] = None,
11811187
**kwargs,
11821188
) -> List["Block"]:
11831189
""" interpolate using scipy wrappers """
@@ -1200,14 +1206,14 @@ def _interpolate(
12001206
)
12011207
# process 1-d slices in the axis direction
12021208

1203-
def func(x):
1209+
def func(yvalues: np.ndarray) -> np.ndarray:
12041210

12051211
# process a 1-d slice, returning it
12061212
# should the axis argument be handled below in apply_along_axis?
12071213
# i.e. not an arg to missing.interpolate_1d
12081214
return missing.interpolate_1d(
1209-
index,
1210-
x,
1215+
xvalues=index,
1216+
yvalues=yvalues,
12111217
method=method,
12121218
limit=limit,
12131219
limit_direction=limit_direction,

pandas/core/missing.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Routines for filling missing data.
33
"""
44

5+
from typing import Any, List, Optional, Set, Union
6+
57
import numpy as np
68

79
from pandas._libs import algos, lib
@@ -92,7 +94,7 @@ def clean_fill_method(method, allow_nearest=False):
9294
return method
9395

9496

95-
def clean_interp_method(method, **kwargs):
97+
def clean_interp_method(method: str, **kwargs) -> str:
9698
order = kwargs.get("order")
9799
valid = [
98100
"linear",
@@ -160,15 +162,15 @@ def find_valid_index(values, how: str):
160162

161163

162164
def interpolate_1d(
163-
xvalues,
164-
yvalues,
165-
method="linear",
166-
limit=None,
167-
limit_direction="forward",
168-
limit_area=None,
169-
fill_value=None,
170-
bounds_error=False,
171-
order=None,
165+
xvalues: np.ndarray,
166+
yvalues: np.ndarray,
167+
method: Optional[str] = "linear",
168+
limit: Optional[int] = None,
169+
limit_direction: str = "forward",
170+
limit_area: Optional[str] = None,
171+
fill_value: Optional[Any] = None,
172+
bounds_error: bool = False,
173+
order: Optional[int] = None,
172174
**kwargs,
173175
):
174176
"""
@@ -238,6 +240,7 @@ def interpolate_1d(
238240
# are more than'limit' away from the prior non-NaN.
239241

240242
# set preserve_nans based on direction using _interp_limit
243+
preserve_nans: Union[List, Set]
241244
if limit_direction == "forward":
242245
preserve_nans = start_nans | set(_interp_limit(invalid, limit, 0))
243246
elif limit_direction == "backward":

0 commit comments

Comments
 (0)