Skip to content

TYP: some type annotations for interpolate #34631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 7, 2020
18 changes: 9 additions & 9 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6863,16 +6863,16 @@ def replace(

@Appender(_shared_docs["interpolate"] % _shared_doc_kwargs)
def interpolate(
self,
method="linear",
axis=0,
limit=None,
inplace=False,
limit_direction="forward",
limit_area=None,
downcast=None,
self: FrameOrSeries,
method: str = "linear",
axis: Axis = 0,
limit: Optional[int] = None,
inplace: bool_t = False,
limit_direction: str = "forward",
limit_area: Optional[str] = None,
downcast: Optional[str] = None,
**kwargs,
):
) -> Optional[FrameOrSeries]:
"""
Interpolate values according to different methods.
"""
Expand Down
66 changes: 36 additions & 30 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime, timedelta
import inspect
import re
from typing import Any, List
from typing import TYPE_CHECKING, Any, List, Optional
import warnings

import numpy as np
Expand Down Expand Up @@ -83,6 +83,9 @@
import pandas.core.missing as missing
from pandas.core.nanops import nanpercentile

if TYPE_CHECKING:
from pandas import Index


class Block(PandasObject):
"""
Expand Down Expand Up @@ -1066,16 +1069,16 @@ def coerce_to_target_dtype(self, other):

def interpolate(
self,
method="pad",
axis=0,
index=None,
inplace=False,
limit=None,
limit_direction="forward",
limit_area=None,
fill_value=None,
coerce=False,
downcast=None,
method: str = "pad",
axis: int = 0,
index: Optional["Index"] = None,
inplace: bool = False,
limit: Optional[int] = None,
limit_direction: str = "forward",
limit_area: Optional[str] = None,
fill_value: Optional[Any] = None,
coerce: bool = False,
downcast: Optional[str] = None,
**kwargs,
):

Expand Down Expand Up @@ -1115,6 +1118,9 @@ def check_int_bool(self, inplace):
r = check_int_bool(self, inplace)
if r is not None:
return r

assert index is not None # for mypy

return self._interpolate(
method=m,
index=index,
Expand All @@ -1130,13 +1136,13 @@ def check_int_bool(self, inplace):

def _interpolate_with_fill(
self,
method="pad",
axis=0,
inplace=False,
limit=None,
fill_value=None,
coerce=False,
downcast=None,
method: str = "pad",
axis: int = 0,
inplace: bool = False,
limit: Optional[int] = None,
fill_value: Optional[Any] = None,
coerce: bool = False,
downcast: Optional[str] = None,
) -> List["Block"]:
""" fillna but using the interpolate machinery """
inplace = validate_bool_kwarg(inplace, "inplace")
Expand Down Expand Up @@ -1169,15 +1175,15 @@ def _interpolate_with_fill(

def _interpolate(
self,
method=None,
index=None,
fill_value=None,
axis=0,
limit=None,
limit_direction="forward",
limit_area=None,
inplace=False,
downcast=None,
method: str,
index: "Index",
fill_value: Optional[Any] = None,
axis: int = 0,
limit: Optional[int] = None,
limit_direction: str = "forward",
limit_area: Optional[str] = None,
inplace: bool = False,
downcast: Optional[str] = None,
**kwargs,
) -> List["Block"]:
""" interpolate using scipy wrappers """
Expand All @@ -1200,14 +1206,14 @@ def _interpolate(
)
# process 1-d slices in the axis direction

def func(x):
def func(yvalues: np.ndarray) -> np.ndarray:

# process a 1-d slice, returning it
# should the axis argument be handled below in apply_along_axis?
# i.e. not an arg to missing.interpolate_1d
return missing.interpolate_1d(
index,
x,
xvalues=index,
yvalues=yvalues,
method=method,
limit=limit,
limit_direction=limit_direction,
Expand Down
24 changes: 13 additions & 11 deletions pandas/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Routines for filling missing data.
"""

from typing import Any, List, Optional, Set, Union

import numpy as np

from pandas._libs import algos, lib
Expand Down Expand Up @@ -92,8 +94,7 @@ def clean_fill_method(method, allow_nearest=False):
return method


def clean_interp_method(method, **kwargs):
order = kwargs.get("order")
def clean_interp_method(method: str, order: Optional[int] = None, **kwargs) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are kwargs even required here any more?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would imagine so, split out order so that can add type annotation for internal checks. will double check though.

Copy link
Member Author

@simonjayhawkins simonjayhawkins Jun 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't appear that we pass the kwargs through to np.interp (left, right and period) but do for scipy.interpolate.UnivariateSpline

did you mean kwargs not used in this function? That's true, but would need to do order = kwargs.get("order") in the calling functions if I remove kwargs here. I don't see that as an improvement.

should I revert this change?

valid = [
"linear",
"time",
Expand Down Expand Up @@ -160,15 +161,15 @@ def find_valid_index(values, how: str):


def interpolate_1d(
xvalues,
yvalues,
method="linear",
limit=None,
limit_direction="forward",
limit_area=None,
fill_value=None,
bounds_error=False,
order=None,
xvalues: np.ndarray,
yvalues: np.ndarray,
method: Optional[str] = "linear",
limit: Optional[int] = None,
limit_direction: str = "forward",
limit_area: Optional[str] = None,
fill_value: Optional[Any] = None,
bounds_error: bool = False,
order: Optional[int] = None,
**kwargs,
):
"""
Expand Down Expand Up @@ -238,6 +239,7 @@ def interpolate_1d(
# are more than'limit' away from the prior non-NaN.

# set preserve_nans based on direction using _interp_limit
preserve_nans: Union[List, Set]
if limit_direction == "forward":
preserve_nans = start_nans | set(_interp_limit(invalid, limit, 0))
elif limit_direction == "backward":
Expand Down