From 50c2af1a6322375359a8bacfd79056ca4ab02df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Mon, 15 Aug 2022 19:22:45 -0400 Subject: [PATCH] TYP: pandas.core.series annotations from pandas-stubs (#47926) * TYP: pandas.core.series annotations from pandas-stubs * and DataFrame * more compatibility with pandas-stub tests * mypy address line-off-by-one (merge?) issue --- pandas/_testing/asserters.py | 4 +- pandas/_typing.py | 4 +- pandas/core/arrays/base.py | 2 +- pandas/core/arrays/interval.py | 2 +- pandas/core/arrays/string_arrow.py | 2 +- pandas/core/frame.py | 432 ++++++++++++++--------------- pandas/core/generic.py | 170 ++++++++---- pandas/core/groupby/groupby.py | 4 +- pandas/core/indexes/base.py | 2 +- pandas/core/series.py | 375 ++++++++++++------------- pandas/io/excel/_base.py | 2 +- pandas/io/formats/format.py | 2 +- 12 files changed, 507 insertions(+), 494 deletions(-) diff --git a/pandas/_testing/asserters.py b/pandas/_testing/asserters.py index 369e4b3454b65..5e0af3c0bc07d 100644 --- a/pandas/_testing/asserters.py +++ b/pandas/_testing/asserters.py @@ -866,7 +866,7 @@ def assert_series_equal( left, right, check_dtype: bool | Literal["equiv"] = True, - check_index_type="equiv", + check_index_type: bool | Literal["equiv"] = "equiv", check_series_type=True, check_less_precise: bool | int | NoDefault = no_default, check_names=True, @@ -1134,7 +1134,7 @@ def assert_frame_equal( left, right, check_dtype: bool | Literal["equiv"] = True, - check_index_type="equiv", + check_index_type: bool | Literal["equiv"] = "equiv", check_column_type="equiv", check_frame_type=True, check_less_precise=no_default, diff --git a/pandas/_typing.py b/pandas/_typing.py index 48fd8f2d1256d..88d826ec454b2 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -39,6 +39,7 @@ Timedelta, Timestamp, ) + from pandas._libs.tslibs import BaseOffset from pandas.core.dtypes.dtypes import ExtensionDtype @@ -63,7 +64,6 @@ from pandas.core.window.rolling import BaseWindow from pandas.io.formats.format import EngFormatter - from pandas.tseries.offsets import DateOffset # numpy compatible types NumpyValueArrayLike = Union[npt._ScalarLike_co, npt.ArrayLike] @@ -113,7 +113,7 @@ Suffixes = Tuple[Optional[str], Optional[str]] Ordered = Optional[bool] JSONSerializable = Optional[Union[PythonScalar, List, Dict]] -Frequency = Union[str, "DateOffset"] +Frequency = Union[str, "BaseOffset"] Axes = Union[AnyArrayLike, List, range] RandomState = Union[ diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index f268c24ca766d..043917376b8c1 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -985,7 +985,7 @@ def equals(self, other: object) -> bool: equal_na = self.isna() & other.isna() # type: ignore[operator] return bool((equal_values | equal_na).all()) - def isin(self, values) -> np.ndarray: + def isin(self, values) -> npt.NDArray[np.bool_]: """ Pointwise comparison for set containment in the given values. diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index bd765b4601b01..552c1a82e75e0 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -1738,7 +1738,7 @@ def contains(self, other): other < self._right if self.open_right else other <= self._right ) - def isin(self, values) -> np.ndarray: + def isin(self, values) -> npt.NDArray[np.bool_]: if not hasattr(values, "dtype"): values = np.array(values) values = extract_array(values, extract_numpy=True) diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index caddd12a2c2b4..9e2cbd86e83a8 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -202,7 +202,7 @@ def _maybe_convert_setitem_value(self, value): raise ValueError("Scalar must be NA or str") return value - def isin(self, values): + def isin(self, values) -> npt.NDArray[np.bool_]: if pa_version_under2p0: fallback_performancewarning(version="2") return super().isin(values) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8c4924a2483be..6cfca4ebdc612 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -26,6 +26,7 @@ Iterable, Iterator, Literal, + Mapping, Sequence, cast, overload, @@ -68,6 +69,7 @@ Level, NaPosition, PythonFuncType, + QuantileInterpolation, ReadBuffer, Renamer, Scalar, @@ -1618,10 +1620,10 @@ def __rmatmul__(self, other) -> DataFrame: @classmethod def from_dict( cls, - data, + data: dict, orient: str = "columns", dtype: Dtype | None = None, - columns=None, + columns: Axes | None = None, ) -> DataFrame: """ Construct DataFrame from dict of array-like or dicts. @@ -1713,7 +1715,10 @@ def from_dict( if isinstance(list(data.values())[0], (Series, dict)): data = _from_nested_dict(data) else: - data, index = list(data.values()), list(data.keys()) + index = list(data.keys()) + # error: Incompatible types in assignment (expression has type + # "List[Any]", variable has type "Dict[Any, Any]") + data = list(data.values()) # type: ignore[assignment] elif orient == "columns" or orient == "tight": if columns is not None: raise ValueError(f"cannot use columns parameter with orient='{orient}'") @@ -1809,7 +1814,25 @@ def to_numpy( return result - def to_dict(self, orient: str = "dict", into=dict): + @overload + def to_dict( + self, + orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., + into: type[dict] = ..., + ) -> dict: + ... + + @overload + def to_dict(self, orient: Literal["records"], into: type[dict] = ...) -> list[dict]: + ... + + def to_dict( + self, + orient: Literal[ + "dict", "list", "series", "split", "tight", "records", "index" + ] = "dict", + into: type[dict] = dict, + ) -> dict | list[dict]: """ Convert the DataFrame to a dictionary. @@ -1915,7 +1938,10 @@ def to_dict(self, orient: str = "dict", into=dict): # GH16122 into_c = com.standardize_mapping(into) - orient = orient.lower() + # error: Incompatible types in assignment (expression has type "str", + # variable has type "Literal['dict', 'list', 'series', 'split', 'tight', + # 'records', 'index']") + orient = orient.lower() # type: ignore[assignment] # GH32515 if orient.startswith(("d", "l", "s", "r", "i")) and orient not in { "dict", @@ -2333,7 +2359,7 @@ def maybe_reorder( return cls(mgr) def to_records( - self, index=True, column_dtypes=None, index_dtypes=None + self, index: bool = True, column_dtypes=None, index_dtypes=None ) -> np.recarray: """ Convert DataFrame to a NumPy record array. @@ -2442,7 +2468,7 @@ def to_records( formats = [] for i, v in enumerate(arrays): - index = i + index_int = i # When the names and arrays are collected, we # first collect those in the DataFrame's index, @@ -2453,13 +2479,13 @@ def to_records( # # This check allows us to see whether we are # handling a name / array in the index or column. - if index < index_len: + if index_int < index_len: dtype_mapping = index_dtypes - name = index_names[index] + name = index_names[index_int] else: - index -= index_len + index_int -= index_len dtype_mapping = column_dtypes - name = self.columns[index] + name = self.columns[index_int] # We have a dictionary, so we get the data type # associated with the index or column (which can @@ -2469,8 +2495,8 @@ def to_records( if is_dict_like(dtype_mapping): if name in dtype_mapping: dtype_mapping = dtype_mapping[name] - elif index in dtype_mapping: - dtype_mapping = dtype_mapping[index] + elif index_int in dtype_mapping: + dtype_mapping = dtype_mapping[index_int] else: dtype_mapping = None @@ -4984,14 +5010,14 @@ def _reindex_multi( @doc(NDFrame.align, **_shared_doc_kwargs) def align( self, - other, - join: str = "outer", + other: DataFrame, + join: Literal["outer", "inner", "left", "right"] = "outer", axis: Axis | None = None, - level: Level | None = None, + level: Level = None, copy: bool = True, fill_value=None, - method: str | None = None, - limit=None, + method: FillnaOptions | None = None, + limit: int | None = None, fill_axis: Axis = 0, broadcast_axis: Axis | None = None, ) -> DataFrame: @@ -5024,6 +5050,7 @@ def set_axis( ) -> DataFrame | None: ... + # error: Signature of "set_axis" incompatible with supertype "NDFrame" @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "labels"]) @Appender( """ @@ -5064,7 +5091,9 @@ def set_axis( see_also_sub=" or columns", ) @Appender(NDFrame.set_axis.__doc__) - def set_axis(self, labels, axis: Axis = 0, inplace: bool = False): + def set_axis( # type: ignore[override] + self, labels, axis: Axis = 0, inplace: bool = False + ) -> DataFrame | None: return super().set_axis(labels, axis=axis, inplace=inplace) @Substitution(**_shared_doc_kwargs) @@ -5096,7 +5125,7 @@ def drop( axis: Axis = ..., index: IndexLabel = ..., columns: IndexLabel = ..., - level: Level | None = ..., + level: Level = ..., inplace: Literal[True], errors: IgnoreRaise = ..., ) -> None: @@ -5110,7 +5139,7 @@ def drop( axis: Axis = ..., index: IndexLabel = ..., columns: IndexLabel = ..., - level: Level | None = ..., + level: Level = ..., inplace: Literal[False] = ..., errors: IgnoreRaise = ..., ) -> DataFrame: @@ -5124,7 +5153,7 @@ def drop( axis: Axis = ..., index: IndexLabel = ..., columns: IndexLabel = ..., - level: Level | None = ..., + level: Level = ..., inplace: bool = ..., errors: IgnoreRaise = ..., ) -> DataFrame | None: @@ -5139,7 +5168,7 @@ def drop( # type: ignore[override] axis: Axis = 0, index: IndexLabel = None, columns: IndexLabel = None, - level: Level | None = None, + level: Level = None, inplace: bool = False, errors: IgnoreRaise = "raise", ) -> DataFrame | None: @@ -5300,7 +5329,7 @@ def rename( axis: Axis | None = ..., copy: bool = ..., inplace: Literal[True], - level: Level | None = ..., + level: Level = ..., errors: IgnoreRaise = ..., ) -> None: ... @@ -5315,7 +5344,7 @@ def rename( axis: Axis | None = ..., copy: bool = ..., inplace: Literal[False] = ..., - level: Level | None = ..., + level: Level = ..., errors: IgnoreRaise = ..., ) -> DataFrame: ... @@ -5330,7 +5359,7 @@ def rename( axis: Axis | None = ..., copy: bool = ..., inplace: bool = ..., - level: Level | None = ..., + level: Level = ..., errors: IgnoreRaise = ..., ) -> DataFrame | None: ... @@ -5344,7 +5373,7 @@ def rename( axis: Axis | None = None, copy: bool = True, inplace: bool = False, - level: Level | None = None, + level: Level = None, errors: IgnoreRaise = "ignore", ) -> DataFrame | None: """ @@ -5468,128 +5497,53 @@ def rename( @overload def fillna( self, - value=..., + value: Hashable | Mapping | Series | DataFrame = ..., + *, method: FillnaOptions | None = ..., axis: Axis | None = ..., inplace: Literal[False] = ..., - limit=..., - downcast=..., + limit: int | None = ..., + downcast: dict | None = ..., ) -> DataFrame: ... @overload def fillna( self, - value, - method: FillnaOptions | None, - axis: Axis | None, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - *, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - value, - *, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - *, - method: FillnaOptions | None, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - *, - axis: Axis | None, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, + value: Hashable | Mapping | Series | DataFrame = ..., *, - method: FillnaOptions | None, - axis: Axis | None, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - value, - *, - axis: Axis | None, + method: FillnaOptions | None = ..., + axis: Axis | None = ..., inplace: Literal[True], - limit=..., - downcast=..., + limit: int | None = ..., + downcast: dict | None = ..., ) -> None: ... @overload def fillna( self, - value, - method: FillnaOptions | None, + value: Hashable | Mapping | Series | DataFrame = ..., *, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - value=..., method: FillnaOptions | None = ..., axis: Axis | None = ..., inplace: bool = ..., - limit=..., - downcast=..., + limit: int | None = ..., + downcast: dict | None = ..., ) -> DataFrame | None: ... + # error: Signature of "fillna" incompatible with supertype "NDFrame" @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "value"]) @doc(NDFrame.fillna, **_shared_doc_kwargs) - def fillna( + def fillna( # type: ignore[override] self, - value: object | ArrayLike | None = None, + value: Hashable | Mapping | Series | DataFrame = None, method: FillnaOptions | None = None, axis: Axis | None = None, inplace: bool = False, - limit=None, - downcast=None, + limit: int | None = None, + downcast: dict | None = None, ) -> DataFrame | None: return super().fillna( value=value, @@ -5730,10 +5684,10 @@ def _replace_columnwise( @doc(NDFrame.shift, klass=_shared_doc_kwargs["klass"]) def shift( self, - periods=1, + periods: int = 1, freq: Frequency | None = None, axis: Axis = 0, - fill_value=lib.no_default, + fill_value: Hashable = lib.no_default, ) -> DataFrame: axis = self._get_axis_number(axis) @@ -6060,7 +6014,8 @@ def set_index( @overload def reset_index( self, - level: Hashable | Sequence[Hashable] | None = ..., + level: IndexLabel = ..., + *, drop: bool = ..., inplace: Literal[False] = ..., col_level: Hashable = ..., @@ -6073,34 +6028,9 @@ def reset_index( @overload def reset_index( self, - level: Hashable | Sequence[Hashable] | None, - drop: bool, - inplace: Literal[True], - col_level: Hashable = ..., - col_fill: Hashable = ..., - allow_duplicates: bool | lib.NoDefault = ..., - names: Hashable | Sequence[Hashable] = None, - ) -> None: - ... - - @overload - def reset_index( - self, - *, - drop: bool, - inplace: Literal[True], - col_level: Hashable = ..., - col_fill: Hashable = ..., - allow_duplicates: bool | lib.NoDefault = ..., - names: Hashable | Sequence[Hashable] = None, - ) -> None: - ... - - @overload - def reset_index( - self, - level: Hashable | Sequence[Hashable] | None, + level: IndexLabel = ..., *, + drop: bool = ..., inplace: Literal[True], col_level: Hashable = ..., col_fill: Hashable = ..., @@ -6112,19 +6042,8 @@ def reset_index( @overload def reset_index( self, + level: IndexLabel = ..., *, - inplace: Literal[True], - col_level: Hashable = ..., - col_fill: Hashable = ..., - allow_duplicates: bool | lib.NoDefault = ..., - names: Hashable | Sequence[Hashable] = None, - ) -> None: - ... - - @overload - def reset_index( - self, - level: Hashable | Sequence[Hashable] | None = ..., drop: bool = ..., inplace: bool = ..., col_level: Hashable = ..., @@ -6137,7 +6056,7 @@ def reset_index( @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "level"]) def reset_index( self, - level: Hashable | Sequence[Hashable] | None = None, + level: IndexLabel = None, drop: bool = False, inplace: bool = False, col_level: Hashable = 0, @@ -6596,7 +6515,7 @@ def dropna( def drop_duplicates( self, subset: Hashable | Sequence[Hashable] | None = None, - keep: Literal["first"] | Literal["last"] | Literal[False] = "first", + keep: Literal["first", "last", False] = "first", inplace: bool = False, ignore_index: bool = False, ) -> DataFrame | None: @@ -6693,7 +6612,7 @@ def drop_duplicates( def duplicated( self, subset: Hashable | Sequence[Hashable] | None = None, - keep: Literal["first"] | Literal["last"] | Literal[False] = "first", + keep: Literal["first", "last", False] = "first", ) -> Series: """ Return boolean Series denoting duplicate rows. @@ -6839,7 +6758,7 @@ def f(vals) -> tuple[np.ndarray, int]: @overload # type: ignore[override] def sort_values( self, - by, + by: IndexLabel, *, axis: Axis = ..., ascending=..., @@ -6854,7 +6773,7 @@ def sort_values( @overload def sort_values( self, - by, + by: IndexLabel, *, axis: Axis = ..., ascending=..., @@ -6873,9 +6792,9 @@ def sort_values( @Appender(NDFrame.sort_values.__doc__) def sort_values( # type: ignore[override] self, - by, + by: IndexLabel, axis: Axis = 0, - ascending=True, + ascending: bool | list[bool] | tuple[bool, ...] = True, inplace: bool = False, kind: str = "quicksort", na_position: str = "last", @@ -6887,9 +6806,16 @@ def sort_values( # type: ignore[override] ascending = validate_ascending(ascending) if not isinstance(by, list): by = [by] - if is_sequence(ascending) and len(by) != len(ascending): + # error: Argument 1 to "len" has incompatible type "Union[bool, List[bool]]"; + # expected "Sized" + if is_sequence(ascending) and ( + len(by) != len(ascending) # type: ignore[arg-type] + ): + # error: Argument 1 to "len" has incompatible type "Union[bool, + # List[bool]]"; expected "Sized" raise ValueError( - f"Length of ascending ({len(ascending)}) != length of by ({len(by)})" + f"Length of ascending ({len(ascending)})" # type: ignore[arg-type] + f" != length of by ({len(by)})" ) if len(by) > 1: @@ -6948,7 +6874,7 @@ def sort_index( self, *, axis: Axis = ..., - level: Level | None = ..., + level: IndexLabel = ..., ascending: bool | Sequence[bool] = ..., inplace: Literal[True], kind: SortKind = ..., @@ -6964,7 +6890,7 @@ def sort_index( self, *, axis: Axis = ..., - level: Level | None = ..., + level: IndexLabel = ..., ascending: bool | Sequence[bool] = ..., inplace: Literal[False] = ..., kind: SortKind = ..., @@ -6980,7 +6906,7 @@ def sort_index( self, *, axis: Axis = ..., - level: Level | None = ..., + level: IndexLabel = ..., ascending: bool | Sequence[bool] = ..., inplace: bool = ..., kind: SortKind = ..., @@ -6996,7 +6922,7 @@ def sort_index( def sort_index( # type: ignore[override] self, axis: Axis = 0, - level: Level | None = None, + level: IndexLabel = None, ascending: bool | Sequence[bool] = True, inplace: bool = False, kind: SortKind = "quicksort", @@ -7858,7 +7784,11 @@ def compare( ) def combine( - self, other: DataFrame, func, fill_value=None, overwrite: bool = True + self, + other: DataFrame, + func: Callable[[Series, Series], Series | Hashable], + fill_value=None, + overwrite: bool = True, ) -> DataFrame: """ Perform column-wise combine with another DataFrame. @@ -8020,7 +7950,11 @@ def combine( if isinstance(new_dtype, np.dtype): # if new_dtype is an EA Dtype, then `func` is expected to return # the correct dtype without any additional casting - arr = maybe_downcast_to_dtype(arr, new_dtype) + # error: No overload variant of "maybe_downcast_to_dtype" matches + # argument types "Union[Series, Hashable]", "dtype[Any]" + arr = maybe_downcast_to_dtype( # type: ignore[call-overload] + arr, new_dtype + ) result[col] = arr @@ -9111,7 +9045,7 @@ def melt( value_vars=None, var_name=None, value_name="value", - col_level: Level | None = None, + col_level: Level = None, ignore_index: bool = True, ) -> DataFrame: @@ -9379,7 +9313,7 @@ def any( axis: Axis = 0, bool_only: bool | None = None, skipna: bool = True, - level: Level | None = None, + level: Level = None, **kwargs, ) -> DataFrame | Series: ... @@ -9404,7 +9338,7 @@ def apply( func: AggFuncType, axis: Axis = 0, raw: bool = False, - result_type=None, + result_type: Literal["expand", "reduce", "broadcast"] | None = None, args=(), **kwargs, ): @@ -10471,10 +10405,11 @@ def cov( def corrwith( self, - other, + other: DataFrame | Series, axis: Axis = 0, - drop=False, - method="pearson", + drop: bool = False, + method: Literal["pearson", "kendall", "spearman"] + | Callable[[np.ndarray, np.ndarray], float] = "pearson", numeric_only: bool | lib.NoDefault = lib.no_default, ) -> Series: """ @@ -10642,9 +10577,7 @@ def c(x): # ---------------------------------------------------------------------- # ndarray-like stats methods - def count( - self, axis: Axis = 0, level: Level | None = None, numeric_only: bool = False - ): + def count(self, axis: Axis = 0, level: Level = None, numeric_only: bool = False): """ Count non-NA cells for each column or row. @@ -11160,13 +11093,43 @@ def f(s): return data + @overload + def quantile( + self, + q: float = ..., + axis: Axis = ..., + numeric_only: bool | lib.NoDefault = ..., + interpolation: QuantileInterpolation = ..., + ) -> Series: + ... + + @overload def quantile( self, - q=0.5, + q: AnyArrayLike | Sequence[float], + axis: Axis = ..., + numeric_only: bool | lib.NoDefault = ..., + interpolation: QuantileInterpolation = ..., + ) -> Series | DataFrame: + ... + + @overload + def quantile( + self, + q: float | AnyArrayLike | Sequence[float] = ..., + axis: Axis = ..., + numeric_only: bool | lib.NoDefault = ..., + interpolation: QuantileInterpolation = ..., + ) -> Series | DataFrame: + ... + + def quantile( + self, + q: float | AnyArrayLike | Sequence[float] = 0.5, axis: Axis = 0, numeric_only: bool | lib.NoDefault = no_default, - interpolation: str = "linear", - ): + interpolation: QuantileInterpolation = "linear", + ) -> Series | DataFrame: """ Return values at the given quantile over requested axis. @@ -11246,8 +11209,14 @@ def quantile( if not is_list_like(q): # BlockManager.quantile expects listlike, so we wrap and unwrap here + # error: List item 0 has incompatible type "Union[float, Union[Union[ + # ExtensionArray, ndarray[Any, Any]], Index, Series], Sequence[float]]"; + # expected "float" res_df = self.quantile( - [q], axis=axis, numeric_only=numeric_only, interpolation=interpolation + [q], # type: ignore[list-item] + axis=axis, + numeric_only=numeric_only, + interpolation=interpolation, ) res = res_df.iloc[0] if axis == 1 and len(self) == 0: @@ -11277,7 +11246,11 @@ def quantile( res = self._constructor([], index=q, columns=cols, dtype=dtype) return res.__finalize__(self, method="quantile") - res = data._mgr.quantile(qs=q, axis=1, interpolation=interpolation) + # error: Argument "qs" to "quantile" of "BlockManager" has incompatible type + # "Index"; expected "Float64Index" + res = data._mgr.quantile( + qs=q, axis=1, interpolation=interpolation # type: ignore[arg-type] + ) result = self._constructor(res) return result.__finalize__(self, method="quantile") @@ -11286,10 +11259,10 @@ def quantile( def asfreq( self, freq: Frequency, - method=None, + method: FillnaOptions | None = None, how: str | None = None, normalize: bool = False, - fill_value=None, + fill_value: Hashable = None, ) -> DataFrame: return super().asfreq( freq=freq, @@ -11303,15 +11276,15 @@ def asfreq( def resample( self, rule, - axis=0, + axis: Axis = 0, closed: str | None = None, label: str | None = None, convention: str = "start", kind: str | None = None, loffset=None, base: int | None = None, - on=None, - level=None, + on: Level = None, + level: Level = None, origin: str | TimestampConvertibleTypes = "start_day", offset: TimedeltaConvertibleTypes | None = None, group_keys: bool | lib.NoDefault = no_default, @@ -11426,7 +11399,7 @@ def to_period( setattr(new_obj, axis_name, new_ax) return new_obj - def isin(self, values) -> DataFrame: + def isin(self, values: Series | DataFrame | Sequence | Mapping) -> DataFrame: """ Whether each element in the DataFrame is contained in values. @@ -11521,8 +11494,13 @@ def isin(self, values) -> DataFrame: "to be passed to DataFrame.isin(), " f"you passed a '{type(values).__name__}'" ) + # error: Argument 2 to "isin" has incompatible type "Union[Sequence[Any], + # Mapping[Any, Any]]"; expected "Union[Union[ExtensionArray, + # ndarray[Any, Any]], Index, Series]" result = self._constructor( - algorithms.isin(self.values.ravel(), values).reshape(self.shape), + algorithms.isin( + self.values.ravel(), values # type: ignore[arg-type] + ).reshape(self.shape), self.index, self.columns, ) @@ -11667,7 +11645,7 @@ def ffill( axis: None | Axis = ..., inplace: Literal[False] = ..., limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> DataFrame: ... @@ -11678,7 +11656,7 @@ def ffill( axis: None | Axis = ..., inplace: Literal[True], limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> None: ... @@ -11689,7 +11667,7 @@ def ffill( axis: None | Axis = ..., inplace: bool = ..., limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> DataFrame | None: ... @@ -11700,7 +11678,7 @@ def ffill( # type: ignore[override] axis: None | Axis = None, inplace: bool = False, limit: None | int = None, - downcast=None, + downcast: dict | None = None, ) -> DataFrame | None: return super().ffill(axis=axis, inplace=inplace, limit=limit, downcast=downcast) @@ -11753,8 +11731,8 @@ def bfill( # type: ignore[override] ) def clip( self: DataFrame, - lower=None, - upper=None, + lower: float | None = None, + upper: float | None = None, axis: Axis | None = None, inplace: bool = False, *args, @@ -11792,10 +11770,10 @@ def where( other=..., *, inplace: Literal[False] = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> DataFrame: ... @@ -11806,10 +11784,10 @@ def where( other=..., *, inplace: Literal[True], - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> None: ... @@ -11820,10 +11798,10 @@ def where( other=..., *, inplace: bool = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> DataFrame | None: ... @@ -11837,10 +11815,10 @@ def where( # type: ignore[override] cond, other=lib.no_default, inplace: bool = False, - axis=None, - level=None, + axis: Axis | None = None, + level: Level = None, errors: IgnoreRaise | lib.NoDefault = "raise", - try_cast=lib.no_default, + try_cast: bool | lib.NoDefault = lib.no_default, ) -> DataFrame | None: return super().where( cond, @@ -11858,10 +11836,10 @@ def mask( other=..., *, inplace: Literal[False] = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> DataFrame: ... @@ -11872,10 +11850,10 @@ def mask( other=..., *, inplace: Literal[True], - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> None: ... @@ -11886,10 +11864,10 @@ def mask( other=..., *, inplace: bool = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> DataFrame | None: ... @@ -11903,10 +11881,10 @@ def mask( # type: ignore[override] cond, other=np.nan, inplace: bool = False, - axis=None, - level=None, + axis: Axis | None = None, + level: Level = None, errors: IgnoreRaise | lib.NoDefault = "raise", - try_cast=lib.no_default, + try_cast: bool | lib.NoDefault = lib.no_default, ) -> DataFrame | None: return super().mask( cond, diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8096b57168d8c..d9264e8a18f2e 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -49,8 +49,10 @@ DtypeArg, DtypeObj, FilePath, + FillnaOptions, FloatFormatType, FormattersType, + Frequency, IgnoreRaise, IndexKeyFunc, IndexLabel, @@ -1741,7 +1743,7 @@ def _check_label_or_level_ambiguity(self, key, axis: int = 0) -> None: raise ValueError(msg) @final - def _get_label_or_level_values(self, key: str, axis: int = 0) -> np.ndarray: + def _get_label_or_level_values(self, key: Level, axis: int = 0) -> np.ndarray: """ Return a 1-D array of values associated with `key`, a label or level from the given `axis`. @@ -3191,7 +3193,7 @@ def to_latex( multicolumn: bool_t | None = ..., multicolumn_format: str | None = ..., multirow: bool_t | None = ..., - caption: str | None = ..., + caption: str | tuple[str, str] | None = ..., label: str | None = ..., position: str | None = ..., ) -> str: @@ -3219,7 +3221,7 @@ def to_latex( multicolumn: bool_t | None = ..., multicolumn_format: str | None = ..., multirow: bool_t | None = ..., - caption: str | None = ..., + caption: str | tuple[str, str] | None = ..., label: str | None = ..., position: str | None = ..., ) -> None: @@ -3248,7 +3250,7 @@ def to_latex( multicolumn: bool_t | None = None, multicolumn_format: str | None = None, multirow: bool_t | None = None, - caption: str | None = None, + caption: str | tuple[str, str] | None = None, label: str | None = None, position: str | None = None, ) -> str | None: @@ -4895,7 +4897,7 @@ def sort_index( self, *, axis: Axis = ..., - level: Level | None = ..., + level: IndexLabel = ..., ascending: bool_t | Sequence[bool_t] = ..., inplace: Literal[True], kind: SortKind = ..., @@ -4911,7 +4913,7 @@ def sort_index( self: NDFrameT, *, axis: Axis = ..., - level: Level | None = ..., + level: IndexLabel = ..., ascending: bool_t | Sequence[bool_t] = ..., inplace: Literal[False] = ..., kind: SortKind = ..., @@ -4927,7 +4929,7 @@ def sort_index( self: NDFrameT, *, axis: Axis = ..., - level: Level | None = ..., + level: IndexLabel = ..., ascending: bool_t | Sequence[bool_t] = ..., inplace: bool_t = ..., kind: SortKind = ..., @@ -4941,7 +4943,7 @@ def sort_index( def sort_index( self: NDFrameT, axis: Axis = 0, - level: Level | None = None, + level: IndexLabel = None, ascending: bool_t | Sequence[bool_t] = True, inplace: bool_t = False, kind: SortKind = "quicksort", @@ -6584,15 +6586,54 @@ def convert_dtypes( # ---------------------------------------------------------------------- # Filling NA's + @overload + def fillna( + self: NDFrameT, + value: Hashable | Mapping | Series | DataFrame = ..., + *, + method: FillnaOptions | None = ..., + axis: Axis | None = ..., + inplace: Literal[False] = ..., + limit: int | None = ..., + downcast: dict | None = ..., + ) -> NDFrameT: + ... + + @overload + def fillna( + self, + value: Hashable | Mapping | Series | DataFrame = ..., + *, + method: FillnaOptions | None = ..., + axis: Axis | None = ..., + inplace: Literal[True], + limit: int | None = ..., + downcast: dict | None = ..., + ) -> None: + ... + + @overload + def fillna( + self: NDFrameT, + value: Hashable | Mapping | Series | DataFrame = ..., + *, + method: FillnaOptions | None = ..., + axis: Axis | None = ..., + inplace: bool_t = ..., + limit: int | None = ..., + downcast: dict | None = ..., + ) -> NDFrameT | None: + ... + @doc(**_shared_doc_kwargs) def fillna( self: NDFrameT, - value=None, - method=None, - axis=None, + value: Hashable | Mapping | Series | DataFrame = None, + method: FillnaOptions | None = None, + axis: Axis | None = None, inplace: bool_t = False, - limit=None, - downcast=None, + limit: int | None = None, + downcast: dict | None = None, ) -> NDFrameT | None: """ Fill NA/NaN values using the specified method. @@ -6769,7 +6810,13 @@ def fillna( for k, v in value.items(): if k not in result: continue - downcast_k = downcast if not is_dict else downcast.get(k) + # error: Item "None" of "Optional[Dict[Any, Any]]" has no + # attribute "get" + downcast_k = ( + downcast + if not is_dict + else downcast.get(k) # type: ignore[union-attr] + ) result.loc[:, k] = result[k].fillna( v, limit=limit, downcast=downcast_k ) @@ -6780,7 +6827,10 @@ def fillna( result = self.T.fillna(value=value, limit=limit).T - new_data = result + # error: Incompatible types in assignment (expression has type + # "NDFrameT", variable has type "Union[ArrayManager, + # SingleArrayManager, BlockManager, SingleBlockManager]") + new_data = result # type: ignore[assignment] else: new_data = self._mgr.fillna( @@ -6805,7 +6855,7 @@ def ffill( axis: None | Axis = ..., inplace: Literal[False] = ..., limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> NDFrameT: ... @@ -6816,7 +6866,7 @@ def ffill( axis: None | Axis = ..., inplace: Literal[True], limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> None: ... @@ -6827,7 +6877,7 @@ def ffill( axis: None | Axis = ..., inplace: bool_t = ..., limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> NDFrameT | None: ... @@ -6838,7 +6888,7 @@ def ffill( axis: None | Axis = None, inplace: bool_t = False, limit: None | int = None, - downcast=None, + downcast: dict | None = None, ) -> NDFrameT | None: """ Synonym for :meth:`DataFrame.fillna` with ``method='ffill'``. @@ -6861,7 +6911,7 @@ def bfill( axis: None | Axis = ..., inplace: Literal[False] = ..., limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> NDFrameT: ... @@ -6872,7 +6922,7 @@ def bfill( axis: None | Axis = ..., inplace: Literal[True], limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> None: ... @@ -6883,7 +6933,7 @@ def bfill( axis: None | Axis = ..., inplace: bool_t = ..., limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> NDFrameT | None: ... @@ -6894,7 +6944,7 @@ def bfill( axis: None | Axis = None, inplace: bool_t = False, limit: None | int = None, - downcast=None, + downcast: dict | None = None, ) -> NDFrameT | None: """ Synonym for :meth:`DataFrame.fillna` with ``method='bfill'``. @@ -7983,11 +8033,11 @@ def clip( @doc(**_shared_doc_kwargs) def asfreq( self: NDFrameT, - freq, - method=None, + freq: Frequency, + method: FillnaOptions | None = None, how: str | None = None, normalize: bool_t = False, - fill_value=None, + fill_value: Hashable = None, ) -> NDFrameT: """ Convert time series to specified frequency. @@ -8297,15 +8347,15 @@ def between_time( def resample( self, rule, - axis=0, + axis: Axis = 0, closed: str | None = None, label: str | None = None, convention: str = "start", kind: str | None = None, loffset=None, base: int | None = None, - on=None, - level=None, + on: Level = None, + level: Level = None, origin: str | TimestampConvertibleTypes = "start_day", offset: TimedeltaConvertibleTypes | None = None, group_keys: bool_t | lib.NoDefault = lib.no_default, @@ -9136,18 +9186,18 @@ def compare( @doc(**_shared_doc_kwargs) def align( - self, - other, - join="outer", - axis=None, - level=None, - copy=True, - fill_value=None, - method=None, - limit=None, - fill_axis=0, - broadcast_axis=None, - ): + self: NDFrameT, + other: NDFrameT, + join: Literal["outer", "inner", "left", "right"] = "outer", + axis: Axis | None = None, + level: Level = None, + copy: bool_t = True, + fill_value: Hashable = None, + method: FillnaOptions | None = None, + limit: int | None = None, + fill_axis: Axis = 0, + broadcast_axis: Axis | None = None, + ) -> NDFrameT: """ Align two objects on their axes with the specified join method. @@ -9614,8 +9664,8 @@ def where( other=..., *, inplace: Literal[False] = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT: @@ -9628,8 +9678,8 @@ def where( other=..., *, inplace: Literal[True], - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., try_cast: bool_t | lib.NoDefault = ..., ) -> None: @@ -9642,8 +9692,8 @@ def where( other=..., *, inplace: bool_t = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT | None: @@ -9665,8 +9715,8 @@ def where( cond, other=np.nan, inplace: bool_t = False, - axis=None, - level=None, + axis: Axis | None = None, + level: Level = None, errors: IgnoreRaise | lib.NoDefault = "raise", try_cast: bool_t | lib.NoDefault = lib.no_default, ) -> NDFrameT | None: @@ -9835,8 +9885,8 @@ def mask( other=..., *, inplace: Literal[False] = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT: @@ -9849,8 +9899,8 @@ def mask( other=..., *, inplace: Literal[True], - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., try_cast: bool_t | lib.NoDefault = ..., ) -> None: @@ -9863,8 +9913,8 @@ def mask( other=..., *, inplace: bool_t = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., try_cast: bool_t | lib.NoDefault = ..., ) -> NDFrameT | None: @@ -9887,8 +9937,8 @@ def mask( cond, other=np.nan, inplace: bool_t = False, - axis=None, - level=None, + axis: Axis | None = None, + level: Level = None, errors: IgnoreRaise | lib.NoDefault = "raise", try_cast: bool_t | lib.NoDefault = lib.no_default, ) -> NDFrameT | None: @@ -9918,7 +9968,11 @@ def mask( @doc(klass=_shared_doc_kwargs["klass"]) def shift( - self: NDFrameT, periods=1, freq=None, axis=0, fill_value=None + self: NDFrameT, + periods: int = 1, + freq=None, + axis: Axis = 0, + fill_value: Hashable = None, ) -> NDFrameT: """ Shift index by desired number of periods with an optional time `freq`. diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 138474e21fb57..44b4fa810deee 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2403,7 +2403,9 @@ def size(self) -> DataFrame | Series: result = self._obj_1d_constructor(result) if not self.as_index: - result = result.rename("size").reset_index() + # error: Incompatible types in assignment (expression has + # type "DataFrame", variable has type "Series") + result = result.rename("size").reset_index() # type: ignore[assignment] return self._reindex_output(result, fill_value=0) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 7e2a9184f04d9..1383f850ab043 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6427,7 +6427,7 @@ def _transform_index(self, func, *, level=None) -> Index: items = [func(x) for x in self] return Index(items, name=self.name, tupleize_cols=False) - def isin(self, values, level=None) -> np.ndarray: + def isin(self, values, level=None) -> npt.NDArray[np.bool_]: """ Return a boolean array where the index values are in `values`. diff --git a/pandas/core/series.py b/pandas/core/series.py index 20f0ecd06fbd1..b1ad3ab175d1b 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -13,6 +13,7 @@ Hashable, Iterable, Literal, + Mapping, Sequence, Union, cast, @@ -41,6 +42,7 @@ DtypeObj, FilePath, FillnaOptions, + Frequency, IgnoreRaise, IndexKeyFunc, IndexLabel, @@ -162,7 +164,6 @@ import pandas.plotting if TYPE_CHECKING: - from pandas._typing import ( NumpySorter, NumpyValueArrayLike, @@ -743,7 +744,7 @@ def array(self) -> ExtensionArray: return self._mgr.array_values() # ops - def ravel(self, order="C"): + def ravel(self, order: str = "C") -> np.ndarray: """ Return the flattened underlying data as an ndarray. @@ -911,7 +912,9 @@ def axes(self) -> list[Index]: # Indexing Methods @Appender(NDFrame.take.__doc__) - def take(self, indices, axis=0, is_copy=None, **kwargs) -> Series: + def take( + self, indices, axis: Axis = 0, is_copy: bool | None = None, **kwargs + ) -> Series: if is_copy is not None: warnings.warn( "is_copy is deprecated and will be removed in a future version. " @@ -1319,7 +1322,7 @@ def _maybe_update_cacher( def _is_mixed_type(self): return False - def repeat(self, repeats, axis=None) -> Series: + def repeat(self, repeats: int | Sequence[int], axis: None = None) -> Series: """ Repeat elements of a Series. @@ -1380,9 +1383,21 @@ def repeat(self, repeats, axis=None) -> Series: @overload def reset_index( self, - level: Level = ..., + level: IndexLabel = ..., *, - drop: bool = ..., + drop: Literal[False] = ..., + name: Level = ..., + inplace: Literal[False] = ..., + allow_duplicates: bool = ..., + ) -> DataFrame: + ... + + @overload + def reset_index( + self, + level: IndexLabel = ..., + *, + drop: Literal[True], name: Level = ..., inplace: Literal[False] = ..., allow_duplicates: bool = ..., @@ -1392,7 +1407,7 @@ def reset_index( @overload def reset_index( self, - level: Level = ..., + level: IndexLabel = ..., *, drop: bool = ..., name: Level = ..., @@ -1404,12 +1419,12 @@ def reset_index( @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "level"]) def reset_index( self, - level: Level = None, + level: IndexLabel = None, drop: bool = False, name: Level = lib.no_default, inplace: bool = False, allow_duplicates: bool = False, - ) -> Series | None: + ) -> DataFrame | Series | None: """ Generate a new DataFrame or Series with the index reset. @@ -1554,9 +1569,7 @@ def reset_index( name = self.name df = self.to_frame(name) - # error: Incompatible return value type (got "DataFrame", expected - # "Optional[Series]") - return df.reset_index( # type: ignore[return-value] + return df.reset_index( level=level, drop=drop, allow_duplicates=allow_duplicates ) return None @@ -1818,7 +1831,7 @@ def keys(self) -> Index: """ return self.index - def to_dict(self, into=dict): + def to_dict(self, into: type[dict] = dict) -> dict: """ Convert Series to {label -> value} dict or dict-like object. @@ -2003,8 +2016,8 @@ def _set_name(self, name, inplace=False) -> Series: def groupby( self, by=None, - axis=0, - level=None, + axis: Axis = 0, + level: Level = None, as_index: bool = True, sort: bool = True, group_keys: bool | lib.NoDefault = no_default, @@ -2047,8 +2060,7 @@ def groupby( # Statistics, overridden ndarray methods # TODO: integrate bottleneck - - def count(self, level=None): + def count(self, level: Level = None): """ Return number of non-NA/null observations in the Series. @@ -2200,23 +2212,30 @@ def unique(self) -> ArrayLike: return super().unique() @overload - def drop_duplicates(self, keep=..., inplace: Literal[False] = ...) -> Series: - ... - - @overload - def drop_duplicates(self, keep, inplace: Literal[True]) -> None: + def drop_duplicates( + self, + keep: Literal["first", "last", False] = ..., + *, + inplace: Literal[False] = ..., + ) -> Series: ... @overload - def drop_duplicates(self, *, inplace: Literal[True]) -> None: + def drop_duplicates( + self, keep: Literal["first", "last", False] = ..., *, inplace: Literal[True] + ) -> None: ... @overload - def drop_duplicates(self, keep=..., inplace: bool = ...) -> Series | None: + def drop_duplicates( + self, keep: Literal["first", "last", False] = ..., *, inplace: bool = ... + ) -> Series | None: ... @deprecate_nonkeyword_arguments(version=None, allowed_args=["self"]) - def drop_duplicates(self, keep="first", inplace=False) -> Series | None: + def drop_duplicates( + self, keep: Literal["first", "last", False] = "first", inplace=False + ) -> Series | None: """ Return Series with duplicate values removed. @@ -2300,7 +2319,7 @@ def drop_duplicates(self, keep="first", inplace=False) -> Series | None: else: return result - def duplicated(self, keep="first") -> Series: + def duplicated(self, keep: Literal["first", "last", False] = "first") -> Series: """ Indicate duplicate Series values. @@ -2380,7 +2399,7 @@ def duplicated(self, keep="first") -> Series: result = self._constructor(res, index=self.index) return result.__finalize__(self, method="duplicated") - def idxmin(self, axis=0, skipna=True, *args, **kwargs): + def idxmin(self, axis: Axis = 0, skipna: bool = True, *args, **kwargs) -> Hashable: """ Return the row label of the minimum value. @@ -2448,7 +2467,7 @@ def idxmin(self, axis=0, skipna=True, *args, **kwargs): return np.nan return self.index[i] - def idxmax(self, axis=0, skipna=True, *args, **kwargs): + def idxmax(self, axis: Axis = 0, skipna: bool = True, *args, **kwargs) -> Hashable: """ Return the row label of the maximum value. @@ -2517,7 +2536,7 @@ def idxmax(self, axis=0, skipna=True, *args, **kwargs): return np.nan return self.index[i] - def round(self, decimals=0, *args, **kwargs) -> Series: + def round(self, decimals: int = 0, *args, **kwargs) -> Series: """ Round each value in a Series to the given number of decimals. @@ -2642,7 +2661,13 @@ def quantile( # scalar return result.iloc[0] - def corr(self, other, method="pearson", min_periods=None) -> float: + def corr( + self, + other: Series, + method: Literal["pearson", "kendall", "spearman"] + | Callable[[np.ndarray, np.ndarray], float] = "pearson", + min_periods: int | None = None, + ) -> float: """ Compute correlation with `other` Series, excluding missing values. @@ -2850,7 +2875,7 @@ def diff(self, periods: int = 1) -> Series: self, method="diff" ) - def autocorr(self, lag=1) -> float: + def autocorr(self, lag: int = 1) -> float: """ Compute the lag-N autocorrelation. @@ -2895,7 +2920,7 @@ def autocorr(self, lag=1) -> float: """ return self.corr(self.shift(lag)) - def dot(self, other): + def dot(self, other: AnyArrayLike) -> Series | np.ndarray: """ Compute the dot product between the Series and the columns of other. @@ -3253,7 +3278,12 @@ def compare( result_names=result_names, ) - def combine(self, other, func, fill_value=None) -> Series: + def combine( + self, + other: Series | Hashable, + func: Callable[[Hashable, Hashable], Hashable], + fill_value: Hashable = None, + ) -> Series: """ Combine the Series with a Series or scalar according to `func`. @@ -3400,7 +3430,7 @@ def combine_first(self, other) -> Series: return this.where(notna(this), other) - def update(self, other) -> None: + def update(self, other: Series | Sequence | Mapping) -> None: """ Modify Series in place using values from passed Series. @@ -3724,7 +3754,7 @@ def sort_index( self, *, axis: Axis = ..., - level: Level | None = ..., + level: IndexLabel = ..., ascending: bool | Sequence[bool] = ..., inplace: Literal[True], kind: SortKind = ..., @@ -3740,7 +3770,7 @@ def sort_index( self, *, axis: Axis = ..., - level: Level | None = ..., + level: IndexLabel = ..., ascending: bool | Sequence[bool] = ..., inplace: Literal[False] = ..., kind: SortKind = ..., @@ -3756,7 +3786,7 @@ def sort_index( self, *, axis: Axis = ..., - level: Level | None = ..., + level: IndexLabel = ..., ascending: bool | Sequence[bool] = ..., inplace: bool = ..., kind: SortKind = ..., @@ -3772,7 +3802,7 @@ def sort_index( def sort_index( # type: ignore[override] self, axis: Axis = 0, - level: Level | None = None, + level: IndexLabel = None, ascending: bool | Sequence[bool] = True, inplace: bool = False, kind: SortKind = "quicksort", @@ -3928,7 +3958,12 @@ def sort_index( # type: ignore[override] key=key, ) - def argsort(self, axis=0, kind="quicksort", order=None) -> Series: + def argsort( + self, + axis: Axis = 0, + kind: SortKind = "quicksort", + order: None = None, + ) -> Series: """ Return the integer indices that would sort the Series values. @@ -3968,7 +4003,9 @@ def argsort(self, axis=0, kind="quicksort", order=None) -> Series: res = self._constructor(result, index=self.index, name=self.name, dtype=np.intp) return res.__finalize__(self, method="argsort") - def nlargest(self, n=5, keep="first") -> Series: + def nlargest( + self, n: int = 5, keep: Literal["first", "last", "all"] = "first" + ) -> Series: """ Return the largest `n` elements. @@ -4223,7 +4260,7 @@ def nsmallest(self, n: int = 5, keep: str = "first") -> Series: dtype: object""" ), ) - def swaplevel(self, i=-2, j=-1, copy=True) -> Series: + def swaplevel(self, i: Level = -2, j: Level = -1, copy: bool = True) -> Series: """ Swap levels i and j in a :class:`MultiIndex`. @@ -4248,7 +4285,7 @@ def swaplevel(self, i=-2, j=-1, copy=True) -> Series: self, method="swaplevel" ) - def reorder_levels(self, order) -> Series: + def reorder_levels(self, order: Sequence[Level]) -> Series: """ Rearrange index levels using input order. @@ -4341,7 +4378,7 @@ def explode(self, ignore_index: bool = False) -> Series: return self._constructor(values, index=index, name=self.name) - def unstack(self, level=-1, fill_value=None) -> DataFrame: + def unstack(self, level: IndexLabel = -1, fill_value: Hashable = None) -> DataFrame: """ Unstack, also known as pivot, Series with MultiIndex to produce DataFrame. @@ -4390,7 +4427,11 @@ def unstack(self, level=-1, fill_value=None) -> DataFrame: # ---------------------------------------------------------------------- # function application - def map(self, arg, na_action=None) -> Series: + def map( + self, + arg: Callable | Mapping | Series, + na_action: Literal["ignore"] | None = None, + ) -> Series: """ Map values of Series according to an input mapping or function. @@ -4522,7 +4563,7 @@ def _gotitem(self, key, ndim, subset=None) -> Series: see_also=_agg_see_also_doc, examples=_agg_examples_doc, ) - def aggregate(self, func=None, axis=0, *args, **kwargs): + def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs): # Validate the axis parameter self._get_axis_number(axis) @@ -4776,17 +4817,17 @@ def _needs_reindex_multi(self, axes, method, level) -> bool: ) def align( self, - other, - join="outer", - axis=None, - level=None, - copy=True, - fill_value=None, - method=None, - limit=None, - fill_axis=0, - broadcast_axis=None, - ): + other: Series, + join: Literal["outer", "inner", "left", "right"] = "outer", + axis: Axis | None = None, + level: Level = None, + copy: bool = True, + fill_value: Hashable = None, + method: FillnaOptions | None = None, + limit: int | None = None, + fill_axis: Axis = 0, + broadcast_axis: Axis | None = None, + ) -> Series: return super().align( other, join=join, @@ -5155,129 +5196,53 @@ def drop( # type: ignore[override] @overload def fillna( self, - value=..., + value: Hashable | Mapping | Series | DataFrame = ..., + *, method: FillnaOptions | None = ..., axis: Axis | None = ..., inplace: Literal[False] = ..., - limit=..., - downcast=..., + limit: int | None = ..., + downcast: dict | None = ..., ) -> Series: ... @overload def fillna( self, - value, - method: FillnaOptions | None, - axis: Axis | None, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, + value: Hashable | Mapping | Series | DataFrame = ..., *, + method: FillnaOptions | None = ..., + axis: Axis | None = ..., inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - value, - *, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - *, - method: FillnaOptions | None, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - *, - axis: Axis | None, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - *, - method: FillnaOptions | None, - axis: Axis | None, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - value, - *, - axis: Axis | None, - inplace: Literal[True], - limit=..., - downcast=..., + limit: int | None = ..., + downcast: dict | None = ..., ) -> None: ... @overload def fillna( self, - value, - method: FillnaOptions | None, + value: Hashable | Mapping | Series | DataFrame = ..., *, - inplace: Literal[True], - limit=..., - downcast=..., - ) -> None: - ... - - @overload - def fillna( - self, - value=..., method: FillnaOptions | None = ..., axis: Axis | None = ..., inplace: bool = ..., - limit=..., - downcast=..., + limit: int | None = ..., + downcast: dict | None = ..., ) -> Series | None: ... - # error: Cannot determine type of 'fillna' + # error: Signature of "fillna" incompatible with supertype "NDFrame" @deprecate_nonkeyword_arguments(version=None, allowed_args=["self", "value"]) - @doc(NDFrame.fillna, **_shared_doc_kwargs) # type: ignore[has-type] - def fillna( + @doc(NDFrame.fillna, **_shared_doc_kwargs) + def fillna( # type: ignore[override] self, - value: object | ArrayLike | None = None, + value: Hashable | Mapping | Series | DataFrame = None, method: FillnaOptions | None = None, - axis=None, - inplace=False, - limit=None, - downcast=None, + axis: Axis | None = None, + inplace: bool = False, + limit: int | None = None, + downcast: dict | None = None, ) -> Series | None: return super().fillna( value=value, @@ -5324,7 +5289,7 @@ def replace( *, inplace: Literal[False] = ..., limit: int | None = ..., - regex=..., + regex: bool = ..., method: Literal["pad", "ffill", "bfill"] | lib.NoDefault = ..., ) -> Series: ... @@ -5337,7 +5302,7 @@ def replace( *, inplace: Literal[True], limit: int | None = ..., - regex=..., + regex: bool = ..., method: Literal["pad", "ffill", "bfill"] | lib.NoDefault = ..., ) -> None: ... @@ -5358,7 +5323,7 @@ def replace( # type: ignore[override] value=lib.no_default, inplace: bool = False, limit: int | None = None, - regex=False, + regex: bool = False, method: Literal["pad", "ffill", "bfill"] | lib.NoDefault = lib.no_default, ) -> Series | None: return super().replace( @@ -5410,7 +5375,9 @@ def _replace_single(self, to_replace, method: str, inplace: bool, limit): # error: Cannot determine type of 'shift' @doc(NDFrame.shift, klass=_shared_doc_kwargs["klass"]) # type: ignore[has-type] - def shift(self, periods=1, freq=None, axis=0, fill_value=None) -> Series: + def shift( + self, periods: int = 1, freq=None, axis: Axis = 0, fill_value: Hashable = None + ) -> Series: return super().shift( periods=periods, freq=freq, axis=axis, fill_value=fill_value ) @@ -5547,7 +5514,12 @@ def isin(self, values) -> Series: self, method="isin" ) - def between(self, left, right, inclusive="both") -> Series: + def between( + self, + left, + right, + inclusive: Literal["both", "neither", "left", "right"] = "both", + ) -> Series: """ Return boolean Series equivalent to left <= series <= right. @@ -5615,7 +5587,9 @@ def between(self, left, right, inclusive="both") -> Series: 3 False dtype: bool """ - if inclusive is True or inclusive is False: + # error: Non-overlapping identity check (left operand type: "Literal['both', + # 'neither', 'left', 'right']", right operand type: "Literal[False]") + if inclusive is True or inclusive is False: # type: ignore[comparison-overlap] warnings.warn( "Boolean inputs to the `inclusive` argument are deprecated in " "favour of `both` or `neither`.", @@ -5812,11 +5786,11 @@ def dropna( @doc(NDFrame.asfreq, **_shared_doc_kwargs) # type: ignore[has-type] def asfreq( self, - freq, - method=None, + freq: Frequency, + method: FillnaOptions | None = None, how: str | None = None, normalize: bool = False, - fill_value=None, + fill_value: Hashable = None, ) -> Series: return super().asfreq( freq=freq, @@ -5831,15 +5805,15 @@ def asfreq( def resample( self, rule, - axis=0, + axis: Axis = 0, closed: str | None = None, label: str | None = None, convention: str = "start", kind: str | None = None, loffset=None, base: int | None = None, - on=None, - level=None, + on: Level = None, + level: Level = None, origin: str | TimestampConvertibleTypes = "start_day", offset: TimedeltaConvertibleTypes | None = None, group_keys: bool | lib.NoDefault = no_default, @@ -5860,7 +5834,12 @@ def resample( group_keys=group_keys, ) - def to_timestamp(self, freq=None, how="start", copy=True) -> Series: + def to_timestamp( + self, + freq=None, + how: Literal["s", "e", "start", "end"] = "start", + copy: bool = True, + ) -> Series: """ Cast to DatetimeIndex of Timestamps, at *beginning* of period. @@ -5889,7 +5868,7 @@ def to_timestamp(self, freq=None, how="start", copy=True) -> Series: self, method="to_timestamp" ) - def to_period(self, freq=None, copy=True) -> Series: + def to_period(self, freq: str | None = None, copy: bool = True) -> Series: """ Convert Series from DatetimeIndex to PeriodIndex. @@ -5923,7 +5902,7 @@ def ffill( axis: None | Axis = ..., inplace: Literal[False] = ..., limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> Series: ... @@ -5934,7 +5913,7 @@ def ffill( axis: None | Axis = ..., inplace: Literal[True], limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> None: ... @@ -5945,7 +5924,7 @@ def ffill( axis: None | Axis = ..., inplace: bool = ..., limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> Series | None: ... @@ -5956,7 +5935,7 @@ def ffill( # type: ignore[override] axis: None | Axis = None, inplace: bool = False, limit: None | int = None, - downcast=None, + downcast: dict | None = None, ) -> Series | None: return super().ffill(axis=axis, inplace=inplace, limit=limit, downcast=downcast) @@ -5967,7 +5946,7 @@ def bfill( axis: None | Axis = ..., inplace: Literal[False] = ..., limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> Series: ... @@ -5978,7 +5957,7 @@ def bfill( axis: None | Axis = ..., inplace: Literal[True], limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> None: ... @@ -5989,7 +5968,7 @@ def bfill( axis: None | Axis = ..., inplace: bool = ..., limit: None | int = ..., - downcast=..., + downcast: dict | None = ..., ) -> Series | None: ... @@ -6000,7 +5979,7 @@ def bfill( # type: ignore[override] axis: None | Axis = None, inplace: bool = False, limit: None | int = None, - downcast=None, + downcast: dict | None = None, ) -> Series | None: return super().bfill(axis=axis, inplace=inplace, limit=limit, downcast=downcast) @@ -6048,10 +6027,10 @@ def where( other=..., *, inplace: Literal[False] = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> Series: ... @@ -6062,10 +6041,10 @@ def where( other=..., *, inplace: Literal[True], - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> None: ... @@ -6076,10 +6055,10 @@ def where( other=..., *, inplace: bool = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> Series | None: ... @@ -6093,10 +6072,10 @@ def where( # type: ignore[override] cond, other=lib.no_default, inplace: bool = False, - axis=None, - level=None, + axis: Axis | None = None, + level: Level = None, errors: IgnoreRaise | lib.NoDefault = lib.no_default, - try_cast=lib.no_default, + try_cast: bool | lib.NoDefault = lib.no_default, ) -> Series | None: return super().where( cond, @@ -6114,10 +6093,10 @@ def mask( other=..., *, inplace: Literal[False] = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> Series: ... @@ -6128,10 +6107,10 @@ def mask( other=..., *, inplace: Literal[True], - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> None: ... @@ -6142,10 +6121,10 @@ def mask( other=..., *, inplace: bool = ..., - axis=..., - level=..., + axis: Axis | None = ..., + level: Level = ..., errors: IgnoreRaise | lib.NoDefault = ..., - try_cast=..., + try_cast: bool | lib.NoDefault = ..., ) -> Series | None: ... @@ -6159,10 +6138,10 @@ def mask( # type: ignore[override] cond, other=np.nan, inplace: bool = False, - axis=None, - level=None, + axis: Axis | None = None, + level: Level = None, errors: IgnoreRaise | lib.NoDefault = lib.no_default, - try_cast=lib.no_default, + try_cast: bool | lib.NoDefault = lib.no_default, ) -> Series | None: return super().mask( cond, diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index 2fda6d239d85b..86ff0d569d2a3 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -358,7 +358,7 @@ def read_excel( io, # sheet name is str or int -> DataFrame - sheet_name: str | int, + sheet_name: str | int = ..., header: int | Sequence[int] | None = ..., names: list[str] | None = ..., index_col: int | Sequence[int] | None = ..., diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 98219ef5eea36..27094fff5f812 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -1036,7 +1036,7 @@ def to_latex( multicolumn: bool = False, multicolumn_format: str | None = None, multirow: bool = False, - caption: str | None = None, + caption: str | tuple[str, str] | None = None, label: str | None = None, position: str | None = None, ) -> str | None: