diff --git a/pandas/_libs/tslibs/dtypes.pyi b/pandas/_libs/tslibs/dtypes.pyi index f6a8d7887ced1..9dbf9d082d8cc 100644 --- a/pandas/_libs/tslibs/dtypes.pyi +++ b/pandas/_libs/tslibs/dtypes.pyi @@ -9,7 +9,7 @@ class PeriodDtypeBase: _dtype_code: int # PeriodDtypeCode # actually __cinit__ - def __new__(self, code: int): ... + def __new__(cls, code: int): ... def freq_group_code(self) -> int: ... def date_offset(self) -> BaseOffset: ... @classmethod diff --git a/pandas/_testing/_io.py b/pandas/_testing/_io.py index 32224cdd4d04e..c7113e663789b 100644 --- a/pandas/_testing/_io.py +++ b/pandas/_testing/_io.py @@ -4,15 +4,13 @@ from functools import wraps import gzip from typing import ( + TYPE_CHECKING, Any, Callable, ) import zipfile -from pandas._typing import ( - FilePathOrBuffer, - FrameOrSeries, -) +from pandas._typing import FilePathOrBuffer from pandas.compat import ( get_lzma_file, import_lzma, @@ -24,6 +22,12 @@ from pandas.io.common import urlopen +if TYPE_CHECKING: + from pandas import ( + DataFrame, + Series, + ) + _RAISE_NETWORK_ERROR_DEFAULT = False lzma = import_lzma() @@ -272,7 +276,9 @@ def can_connect(url, error_classes=None): # File-IO -def round_trip_pickle(obj: Any, path: FilePathOrBuffer | None = None) -> FrameOrSeries: +def round_trip_pickle( + obj: Any, path: FilePathOrBuffer | None = None +) -> DataFrame | Series: """ Pickle an object and then read it again. diff --git a/pandas/core/apply.py b/pandas/core/apply.py index dafcc71ae0b5a..813ba019e09b6 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -1337,7 +1337,7 @@ def _make_unique_kwarg_list( def relabel_result( - result: FrameOrSeries, + result: DataFrame | Series, func: dict[str, list[Callable | str]], columns: Iterable[Hashable], order: Iterable[int], diff --git a/pandas/core/arrays/_mixins.py b/pandas/core/arrays/_mixins.py index 4c7ccc2f16477..dbd1001e0ce07 100644 --- a/pandas/core/arrays/_mixins.py +++ b/pandas/core/arrays/_mixins.py @@ -323,7 +323,7 @@ def __repr__(self) -> str: # ------------------------------------------------------------------------ # __array_function__ methods - def putmask(self: NDArrayBackedExtensionArrayT, mask: np.ndarray, value) -> None: + def putmask(self, mask: np.ndarray, value) -> None: """ Analogue to np.putmask(self, mask, value) diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 732bdb112b8c3..68365613c8c77 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -934,9 +934,7 @@ def copy(self: IntervalArrayT) -> IntervalArrayT: def isna(self) -> np.ndarray: return isna(self._left) - def shift( - self: IntervalArrayT, periods: int = 1, fill_value: object = None - ) -> IntervalArray: + def shift(self, periods: int = 1, fill_value: object = None) -> IntervalArray: if not len(self) or periods == 0: return self.copy() diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 4321610521f97..26c0b7426727c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -709,13 +709,11 @@ def set_axis( ... @overload - def set_axis( - self: FrameOrSeries, labels, axis: Axis, inplace: Literal[True] - ) -> None: + def set_axis(self, labels, axis: Axis, inplace: Literal[True]) -> None: ... @overload - def set_axis(self: FrameOrSeries, labels, *, inplace: Literal[True]) -> None: + def set_axis(self, labels, *, inplace: Literal[True]) -> None: ... @overload @@ -2703,10 +2701,12 @@ def to_hdf( """ from pandas.io import pytables + # Argument 3 to "to_hdf" has incompatible type "NDFrame"; expected + # "Union[DataFrame, Series]" [arg-type] pytables.to_hdf( path_or_buf, key, - self, + self, # type: ignore[arg-type] mode=mode, complevel=complevel, complib=complib, @@ -3850,7 +3850,7 @@ def _slice(self: FrameOrSeries, slobj: slice, axis=0) -> FrameOrSeries: return result @final - def _set_is_copy(self, ref: FrameOrSeries, copy: bool_t = True) -> None: + def _set_is_copy(self, ref: NDFrame, copy: bool_t = True) -> None: if not copy: self._is_copy = None else: @@ -11019,13 +11019,11 @@ def ewm( adjust: bool_t = True, ignore_na: bool_t = False, axis: Axis = 0, - times: str | np.ndarray | FrameOrSeries | None = None, + times: str | np.ndarray | DataFrame | Series | None = None, method: str = "single", ) -> ExponentialMovingWindow: axis = self._get_axis_number(axis) - # error: Value of type variable "FrameOrSeries" of "ExponentialMovingWindow" - # cannot be "object" - return ExponentialMovingWindow( # type: ignore[type-var] + return ExponentialMovingWindow( self, com=com, span=span, diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index f0408db7f4ef8..fd7a10d0c1b0b 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -1523,7 +1523,7 @@ def _wrap_agged_manager(self, mgr: Manager2D) -> DataFrame: # values converted return self._reindex_output(result)._convert(datetime=True) - def _iterate_column_groupbys(self, obj: FrameOrSeries): + def _iterate_column_groupbys(self, obj: DataFrame | Series): for i, colname in enumerate(obj.columns): yield colname, SeriesGroupBy( obj.iloc[:, i], @@ -1532,7 +1532,7 @@ def _iterate_column_groupbys(self, obj: FrameOrSeries): exclusions=self.exclusions, ) - def _apply_to_column_groupbys(self, func, obj: FrameOrSeries) -> DataFrame: + def _apply_to_column_groupbys(self, func, obj: DataFrame | Series) -> DataFrame: from pandas.core.reshape.concat import concat columns = obj.columns diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index b41935902b9cf..dfc3e464a7952 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -44,7 +44,6 @@ class providing the base-class of operations. import pandas._libs.groupby as libgroupby from pandas._typing import ( ArrayLike, - F, FrameOrSeries, IndexLabel, RandomState, @@ -1373,7 +1372,10 @@ def f(g): @final def _python_apply_general( - self, f: F, data: DataFrame | Series, not_indexed_same: bool | None = None + self, + f: Callable, + data: DataFrame | Series, + not_indexed_same: bool | None = None, ) -> DataFrame | Series: """ Apply function f in python space diff --git a/pandas/core/groupby/grouper.py b/pandas/core/groupby/grouper.py index d02df6a65d359..22270fdca4b20 100644 --- a/pandas/core/groupby/grouper.py +++ b/pandas/core/groupby/grouper.py @@ -5,6 +5,8 @@ from __future__ import annotations from typing import ( + TYPE_CHECKING, + Any, Hashable, final, ) @@ -47,6 +49,9 @@ from pandas.io.formats.printing import pprint_thing +if TYPE_CHECKING: + from pandas.core.generic import NDFrame + class Grouper: """ @@ -299,7 +304,9 @@ def ax(self) -> Index: raise ValueError("_set_grouper must be called before ax is accessed") return index - def _get_grouper(self, obj: FrameOrSeries, validate: bool = True): + def _get_grouper( + self, obj: FrameOrSeries, validate: bool = True + ) -> tuple[Any, ops.BaseGrouper, FrameOrSeries]: """ Parameters ---------- @@ -326,10 +333,12 @@ def _get_grouper(self, obj: FrameOrSeries, validate: bool = True): dropna=self.dropna, ) - return self.binner, self.grouper, self.obj + # error: Incompatible return value type (got "Tuple[None, None, None]", + # expected "Tuple[Any, BaseGrouper, FrameOrSeries]") + return self.binner, self.grouper, self.obj # type: ignore[return-value] @final - def _set_grouper(self, obj: FrameOrSeries, sort: bool = False): + def _set_grouper(self, obj: NDFrame, sort: bool = False): """ given an object and the specifications, setup the internal grouper for this particular specification @@ -459,7 +468,7 @@ def __init__( self, index: Index, grouper=None, - obj: FrameOrSeries | None = None, + obj: NDFrame | None = None, level=None, sort: bool = True, observed: bool = False, @@ -501,11 +510,9 @@ def __init__( # what key/level refer to exactly, don't need to # check again as we have by this point converted these # to an actual value (rather than a pd.Grouper) + assert self.obj is not None # for mypy _, newgrouper, newobj = self.grouping_vector._get_grouper( - # error: Value of type variable "FrameOrSeries" of "_get_grouper" - # of "Grouper" cannot be "Optional[FrameOrSeries]" - self.obj, # type: ignore[type-var] - validate=False, + self.obj, validate=False ) self.obj = newobj diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 278e8123001d1..a7ac2c7a1dba6 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -10,6 +10,7 @@ import collections import functools from typing import ( + Callable, Generic, Hashable, Iterator, @@ -29,7 +30,6 @@ from pandas._typing import ( ArrayLike, DtypeObj, - F, FrameOrSeries, Shape, npt, @@ -700,7 +700,7 @@ def get_iterator( yield key, group.__finalize__(data, method="groupby") @final - def _get_splitter(self, data: FrameOrSeries, axis: int = 0) -> DataSplitter: + def _get_splitter(self, data: NDFrame, axis: int = 0) -> DataSplitter: """ Returns ------- @@ -732,7 +732,9 @@ def group_keys_seq(self): return get_flattened_list(ids, ngroups, self.levels, self.codes) @final - def apply(self, f: F, data: FrameOrSeries, axis: int = 0) -> tuple[list, bool]: + def apply( + self, f: Callable, data: DataFrame | Series, axis: int = 0 + ) -> tuple[list, bool]: mutated = self.mutated splitter = self._get_splitter(data, axis=axis) group_keys = self.group_keys_seq @@ -918,7 +920,7 @@ def _cython_operation( @final def agg_series( - self, obj: Series, func: F, preserve_dtype: bool = False + self, obj: Series, func: Callable, preserve_dtype: bool = False ) -> ArrayLike: """ Parameters @@ -960,7 +962,7 @@ def agg_series( @final def _aggregate_series_pure_python( - self, obj: Series, func: F + self, obj: Series, func: Callable ) -> npt.NDArray[np.object_]: ids, _, ngroups = self.group_info @@ -1061,7 +1063,7 @@ def _get_grouper(self): """ return self - def get_iterator(self, data: FrameOrSeries, axis: int = 0): + def get_iterator(self, data: NDFrame, axis: int = 0): """ Groupby iterator @@ -1142,7 +1144,7 @@ def groupings(self) -> list[grouper.Grouping]: ping = grouper.Grouping(lev, lev, in_axis=False, level=None) return [ping] - def _aggregate_series_fast(self, obj: Series, func: F) -> np.ndarray: + def _aggregate_series_fast(self, obj: Series, func: Callable) -> np.ndarray: # -> np.ndarray[object] raise NotImplementedError( "This should not be reached; use _aggregate_series_pure_python" @@ -1241,7 +1243,7 @@ def _chop(self, sdata: DataFrame, slice_obj: slice) -> DataFrame: def get_splitter( - data: FrameOrSeries, labels: np.ndarray, ngroups: int, axis: int = 0 + data: NDFrame, labels: np.ndarray, ngroups: int, axis: int = 0 ) -> DataSplitter: if isinstance(data, Series): klass: type[DataSplitter] = SeriesSplitter diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 303d0494db803..11ed7cb47ea3e 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -4,6 +4,7 @@ from datetime import timedelta from textwrap import dedent from typing import ( + TYPE_CHECKING, Callable, Hashable, Literal, @@ -90,6 +91,12 @@ Tick, ) +if TYPE_CHECKING: + from pandas import ( + DataFrame, + Series, + ) + _shared_docs_kwargs: dict[str, str] = {} @@ -135,7 +142,7 @@ class Resampler(BaseGroupBy, PandasObject): def __init__( self, - obj: FrameOrSeries, + obj: DataFrame | Series, groupby: TimeGrouper, axis: int = 0, kind=None, diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 6dc95a19d8d53..d810bbecc412f 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -26,7 +26,6 @@ from pandas._typing import ( ArrayLike, DtypeObj, - FrameOrSeries, IndexLabel, Suffixes, npt, @@ -2259,7 +2258,7 @@ def _any(x) -> bool: return x is not None and com.any_not_none(*x) -def _validate_operand(obj: FrameOrSeries) -> DataFrame: +def _validate_operand(obj: DataFrame | Series) -> DataFrame: if isinstance(obj, ABCDataFrame): return obj elif isinstance(obj, ABCSeries): diff --git a/pandas/core/sample.py b/pandas/core/sample.py index 63b8789f3f551..16fca2d0ff1b4 100644 --- a/pandas/core/sample.py +++ b/pandas/core/sample.py @@ -3,18 +3,22 @@ """ from __future__ import annotations +from typing import TYPE_CHECKING + import numpy as np from pandas._libs import lib -from pandas._typing import FrameOrSeries from pandas.core.dtypes.generic import ( ABCDataFrame, ABCSeries, ) +if TYPE_CHECKING: + from pandas.core.generic import NDFrame + -def preprocess_weights(obj: FrameOrSeries, weights, axis: int) -> np.ndarray: +def preprocess_weights(obj: NDFrame, weights, axis: int) -> np.ndarray: """ Process and validate the `weights` argument to `NDFrame.sample` and `.GroupBy.sample`. diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index 7b58af87fb1d8..fd53481778001 100644 --- a/pandas/core/window/ewm.py +++ b/pandas/core/window/ewm.py @@ -18,6 +18,7 @@ if TYPE_CHECKING: from pandas import DataFrame, Series + from pandas.core.generic import NDFrame from pandas.compat.numpy import function as nv from pandas.util._decorators import doc @@ -93,7 +94,7 @@ def get_center_of_mass( def _calculate_deltas( - times: str | np.ndarray | FrameOrSeries | None, + times: str | np.ndarray | NDFrame | None, halflife: float | TimedeltaConvertibleTypes | None, ) -> np.ndarray: """ diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py index 2f460267bfec4..03f16259c66a2 100644 --- a/pandas/core/window/expanding.py +++ b/pandas/core/window/expanding.py @@ -9,12 +9,12 @@ from pandas._typing import ( Axis, - FrameOrSeries, WindowingRankType, ) if TYPE_CHECKING: from pandas import DataFrame, Series + from pandas.core.generic import NDFrame from pandas.compat.numpy import function as nv from pandas.util._decorators import doc @@ -101,7 +101,7 @@ class Expanding(RollingAndExpandingMixin): def __init__( self, - obj: FrameOrSeries, + obj: NDFrame, min_periods: int = 1, center=None, axis: Axis = 0, diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 2060f2d701276..94a5a9797d43d 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -104,6 +104,7 @@ DataFrame, Series, ) + from pandas.core.generic import NDFrame from pandas.core.groupby.ops import BaseGrouper from pandas.core.internals import Block # noqa:F401 @@ -117,7 +118,7 @@ class BaseWindow(SelectionMixin): def __init__( self, - obj: FrameOrSeries, + obj: NDFrame, window=None, min_periods: int | None = None, center: bool = False, @@ -635,7 +636,7 @@ class BaseWindowGroupby(BaseWindow): def __init__( self, - obj: FrameOrSeries, + obj: DataFrame | Series, *args, _grouper: BaseGrouper, _as_index: bool = True, @@ -660,7 +661,7 @@ def _apply( numba_cache_key: tuple[Callable, str] | None = None, numba_args: tuple[Any, ...] = (), **kwargs, - ) -> FrameOrSeries: + ) -> DataFrame | Series: result = super()._apply( func, name, diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index a454980138369..67f92c4a67e14 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -3250,7 +3250,7 @@ def _background_gradient( text_color_threshold: float = 0.408, vmin: float | None = None, vmax: float | None = None, - gmap: Sequence | np.ndarray | FrameOrSeries | None = None, + gmap: Sequence | np.ndarray | DataFrame | Series | None = None, text_only: bool = False, ): """ @@ -3357,7 +3357,7 @@ def _highlight_between( return np.where(g_left & l_right, props, "") -def _highlight_value(data: FrameOrSeries, op: str, props: str) -> np.ndarray: +def _highlight_value(data: DataFrame | Series, op: str, props: str) -> np.ndarray: """ Return an array of css strings based on the condition of values matching an op. """ @@ -3368,7 +3368,7 @@ def _highlight_value(data: FrameOrSeries, op: str, props: str) -> np.ndarray: def _bar( - data: FrameOrSeries, + data: NDFrame, align: str | float | int | Callable, colors: list[str], width: float, diff --git a/pandas/io/json/_table_schema.py b/pandas/io/json/_table_schema.py index e7a24fcee2b34..75fd950cd6076 100644 --- a/pandas/io/json/_table_schema.py +++ b/pandas/io/json/_table_schema.py @@ -15,7 +15,6 @@ import pandas._libs.json as json from pandas._typing import ( DtypeObj, - FrameOrSeries, JSONSerializable, ) @@ -36,6 +35,7 @@ import pandas.core.common as com if TYPE_CHECKING: + from pandas import Series from pandas.core.indexes.multi import MultiIndex loads = json.loads @@ -206,7 +206,7 @@ def convert_json_field_to_pandas_type(field): def build_table_schema( - data: FrameOrSeries, + data: DataFrame | Series, index: bool = True, primary_key: bool | None = None, version: bool = True, diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index d7a90511e3c73..f85128ea0ca4a 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -39,7 +39,6 @@ from pandas._typing import ( ArrayLike, DtypeArg, - FrameOrSeries, Shape, ) from pandas.compat._optional import import_optional_dependency @@ -262,7 +261,7 @@ def _tables(): def to_hdf( path_or_buf, key: str, - value: FrameOrSeries, + value: DataFrame | Series, mode: str = "a", complevel: int | None = None, complib: str | None = None, @@ -275,7 +274,7 @@ def to_hdf( data_columns: bool | list[str] | None = None, errors: str = "strict", encoding: str = "UTF-8", -): +) -> None: """store this object, close it if we opened it""" if append: f = lambda store: store.append( @@ -1070,7 +1069,7 @@ def func(_start, _stop, _where): def put( self, key: str, - value: FrameOrSeries, + value: DataFrame | Series, format=None, index=True, append=False, @@ -1195,7 +1194,7 @@ def remove(self, key: str, where=None, start=None, stop=None): def append( self, key: str, - value: FrameOrSeries, + value: DataFrame | Series, format=None, axes=None, index=True, @@ -1637,7 +1636,7 @@ def _create_storer( self, group, format=None, - value: FrameOrSeries | None = None, + value: DataFrame | Series | None = None, encoding: str = "UTF-8", errors: str = "strict", ) -> GenericFixed | Table: @@ -1728,7 +1727,7 @@ def error(t): def _write_to_group( self, key: str, - value: FrameOrSeries, + value: DataFrame | Series, format, axes=None, index=True, @@ -1745,7 +1744,7 @@ def _write_to_group( encoding=None, errors: str = "strict", track_times: bool = True, - ): + ) -> None: # we don't want to store a table node at all if our object is 0-len # as there are not dtypes if getattr(value, "empty", None) and (format == "table" or append): @@ -3016,7 +3015,9 @@ def write_array_empty(self, key: str, value: ArrayLike): node._v_attrs.value_type = str(value.dtype) node._v_attrs.shape = value.shape - def write_array(self, key: str, obj: FrameOrSeries, items: Index | None = None): + def write_array( + self, key: str, obj: DataFrame | Series, items: Index | None = None + ) -> None: # TODO: we only have a few tests that get here, the only EA # that gets passed is DatetimeArray, and we never have # both self._filters and EA @@ -3469,14 +3470,9 @@ def write_metadata(self, key: str, values: np.ndarray): key : str values : ndarray """ - # error: Incompatible types in assignment (expression has type - # "Series", variable has type "ndarray") - values = Series(values) # type: ignore[assignment] - # error: Value of type variable "FrameOrSeries" of "put" of "HDFStore" - # cannot be "ndarray" - self.parent.put( # type: ignore[type-var] + self.parent.put( self._get_metadata_path(key), - values, + Series(values), format="table", encoding=self.encoding, errors=self.errors, diff --git a/pyproject.toml b/pyproject.toml index 65f49dafb5136..a99aeec17791e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -129,7 +129,6 @@ reportImportCycles = false reportIncompatibleMethodOverride = false reportIncompatibleVariableOverride = false reportInvalidStubStatement = false -reportInvalidTypeVarUse = false reportMissingImports = false reportMissingModuleSource = false reportMissingTypeArgument = false @@ -143,7 +142,6 @@ reportOverlappingOverload = false reportPrivateImportUsage = false reportPrivateUsage = false reportPropertyTypeMismatch = false -reportSelfClsParameterName = false reportUnboundVariable = false reportUnknownArgumentType = false reportUnknownLambdaType = false