diff --git a/pandas/_typing.py b/pandas/_typing.py index 12d23786c3387..906b77319406f 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -101,12 +101,6 @@ ] Timezone = Union[str, tzinfo] -# FrameOrSeriesUnion means either a DataFrame or a Series. E.g. -# `def func(a: FrameOrSeriesUnion) -> FrameOrSeriesUnion: ...` means that if a Series -# is passed in, either a Series or DataFrame is returned, and if a DataFrame is passed -# in, either a DataFrame or a Series is returned. -FrameOrSeriesUnion = Union["DataFrame", "Series"] - # FrameOrSeries is stricter and ensures that the same subclass of NDFrame always is # used. E.g. `def func(a: FrameOrSeries) -> FrameOrSeries: ...` means that if a # Series is passed into a function, a Series is always returned and if a DataFrame is diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index f26cf113f7d5e..f727b7b15c8b1 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -29,7 +29,6 @@ AnyArrayLike, ArrayLike, DtypeObj, - FrameOrSeriesUnion, Scalar, ) from pandas.util._decorators import doc @@ -1211,7 +1210,7 @@ def __init__(self, obj, n: int, keep: str): if self.keep not in ("first", "last", "all"): raise ValueError('keep must be either "first", "last" or "all"') - def compute(self, method: str) -> FrameOrSeriesUnion: + def compute(self, method: str) -> DataFrame | Series: raise NotImplementedError def nlargest(self): diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 388c1881afed7..f2e60f43d2a54 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -25,7 +25,6 @@ AggObjType, Axis, FrameOrSeries, - FrameOrSeriesUnion, ) from pandas.util._decorators import cache_readonly @@ -137,10 +136,10 @@ def f(x): self.f: AggFuncType = f @abc.abstractmethod - def apply(self) -> FrameOrSeriesUnion: + def apply(self) -> DataFrame | Series: pass - def agg(self) -> FrameOrSeriesUnion | None: + def agg(self) -> DataFrame | Series | None: """ Provide an implementation for the aggregators. @@ -171,7 +170,7 @@ def agg(self) -> FrameOrSeriesUnion | None: # caller can react return None - def transform(self) -> FrameOrSeriesUnion: + def transform(self) -> DataFrame | Series: """ Transform a DataFrame or Series. @@ -252,7 +251,7 @@ def transform_dict_like(self, func): func = self.normalize_dictlike_arg("transform", obj, func) - results: dict[Hashable, FrameOrSeriesUnion] = {} + results: dict[Hashable, DataFrame | Series] = {} failed_names = [] all_type_errors = True for name, how in func.items(): @@ -283,7 +282,7 @@ def transform_dict_like(self, func): ) return concat(results, axis=1) - def transform_str_or_callable(self, func) -> FrameOrSeriesUnion: + def transform_str_or_callable(self, func) -> DataFrame | Series: """ Compute transform in the case of a string or callable func """ @@ -305,7 +304,7 @@ def transform_str_or_callable(self, func) -> FrameOrSeriesUnion: except Exception: return func(obj, *args, **kwargs) - def agg_list_like(self) -> FrameOrSeriesUnion: + def agg_list_like(self) -> DataFrame | Series: """ Compute aggregation in the case of a list-like argument. @@ -399,7 +398,7 @@ def agg_list_like(self) -> FrameOrSeriesUnion: ) return concatenated.reindex(full_ordered_index, copy=False) - def agg_dict_like(self) -> FrameOrSeriesUnion: + def agg_dict_like(self) -> DataFrame | Series: """ Compute aggregation in the case of a dict-like argument. @@ -467,7 +466,7 @@ def agg_dict_like(self) -> FrameOrSeriesUnion: return result - def apply_str(self) -> FrameOrSeriesUnion: + def apply_str(self) -> DataFrame | Series: """ Compute apply in case of a string. @@ -492,7 +491,7 @@ def apply_str(self) -> FrameOrSeriesUnion: raise ValueError(f"Operation {f} does not support axis=1") return self._try_aggregate_string_function(obj, f, *self.args, **self.kwargs) - def apply_multiple(self) -> FrameOrSeriesUnion: + def apply_multiple(self) -> DataFrame | Series: """ Compute apply in case of a list-like or dict-like. @@ -504,7 +503,7 @@ def apply_multiple(self) -> FrameOrSeriesUnion: return self.obj.aggregate(self.f, self.axis, *self.args, **self.kwargs) def normalize_dictlike_arg( - self, how: str, obj: FrameOrSeriesUnion, func: AggFuncTypeDict + self, how: str, obj: DataFrame | Series, func: AggFuncTypeDict ) -> AggFuncTypeDict: """ Handler for dict-like argument. @@ -617,7 +616,7 @@ def series_generator(self) -> Iterator[Series]: @abc.abstractmethod def wrap_results_for_axis( self, results: ResType, res_index: Index - ) -> FrameOrSeriesUnion: + ) -> DataFrame | Series: pass # --------------------------------------------------------------- @@ -638,7 +637,7 @@ def values(self): def dtypes(self) -> Series: return self.obj.dtypes - def apply(self) -> FrameOrSeriesUnion: + def apply(self) -> DataFrame | Series: """compute the results""" # dispatch to agg if is_list_like(self.f): @@ -812,7 +811,7 @@ def apply_series_generator(self) -> tuple[ResType, Index]: return results, res_index - def wrap_results(self, results: ResType, res_index: Index) -> FrameOrSeriesUnion: + def wrap_results(self, results: ResType, res_index: Index) -> DataFrame | Series: from pandas import Series # see if we can infer the results @@ -835,7 +834,7 @@ def wrap_results(self, results: ResType, res_index: Index) -> FrameOrSeriesUnion return result - def apply_str(self) -> FrameOrSeriesUnion: + def apply_str(self) -> DataFrame | Series: # Caller is responsible for checking isinstance(self.f, str) # TODO: GH#39993 - Avoid special-casing by replacing with lambda if self.f == "size": @@ -866,7 +865,7 @@ def result_columns(self) -> Index: def wrap_results_for_axis( self, results: ResType, res_index: Index - ) -> FrameOrSeriesUnion: + ) -> DataFrame | Series: """return the results for the rows""" if self.result_type == "reduce": @@ -949,9 +948,9 @@ def result_columns(self) -> Index: def wrap_results_for_axis( self, results: ResType, res_index: Index - ) -> FrameOrSeriesUnion: + ) -> DataFrame | Series: """return the results for the columns""" - result: FrameOrSeriesUnion + result: DataFrame | Series # we have requested to expand if self.result_type == "expand": @@ -1005,7 +1004,7 @@ def __init__( kwargs=kwargs, ) - def apply(self) -> FrameOrSeriesUnion: + def apply(self) -> DataFrame | Series: obj = self.obj if len(obj) == 0: @@ -1056,7 +1055,7 @@ def apply_empty_result(self) -> Series: obj, method="apply" ) - def apply_standard(self) -> FrameOrSeriesUnion: + def apply_standard(self) -> DataFrame | Series: f = self.f obj = self.obj diff --git a/pandas/core/describe.py b/pandas/core/describe.py index dfb18b2c40698..0fd7838ab5acc 100644 --- a/pandas/core/describe.py +++ b/pandas/core/describe.py @@ -22,7 +22,6 @@ from pandas._libs.tslibs import Timestamp from pandas._typing import ( FrameOrSeries, - FrameOrSeriesUnion, Hashable, ) from pandas.util._validators import validate_percentile @@ -107,12 +106,12 @@ class NDFrameDescriberAbstract(ABC): Whether to treat datetime dtypes as numeric. """ - def __init__(self, obj: FrameOrSeriesUnion, datetime_is_numeric: bool): + def __init__(self, obj: DataFrame | Series, datetime_is_numeric: bool): self.obj = obj self.datetime_is_numeric = datetime_is_numeric @abstractmethod - def describe(self, percentiles: Sequence[float]) -> FrameOrSeriesUnion: + def describe(self, percentiles: Sequence[float]) -> DataFrame | Series: """Do describe either series or dataframe. Parameters diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 91b9bdd564676..57b2e701fce35 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -58,7 +58,6 @@ FillnaOptions, FloatFormatType, FormattersType, - FrameOrSeriesUnion, Frequency, IndexKeyFunc, IndexLabel, @@ -1362,7 +1361,7 @@ def dot(self, other: Series) -> Series: def dot(self, other: DataFrame | Index | ArrayLike) -> DataFrame: ... - def dot(self, other: AnyArrayLike | FrameOrSeriesUnion) -> FrameOrSeriesUnion: + def dot(self, other: AnyArrayLike | DataFrame | Series) -> DataFrame | Series: """ Compute the matrix multiplication between the DataFrame and other. @@ -1478,13 +1477,13 @@ def __matmul__(self, other: Series) -> Series: @overload def __matmul__( - self, other: AnyArrayLike | FrameOrSeriesUnion - ) -> FrameOrSeriesUnion: + self, other: AnyArrayLike | DataFrame | Series + ) -> DataFrame | Series: ... def __matmul__( - self, other: AnyArrayLike | FrameOrSeriesUnion - ) -> FrameOrSeriesUnion: + self, other: AnyArrayLike | DataFrame | Series + ) -> DataFrame | Series: """ Matrix multiplication using binary `@` operator in Python>=3.5. """ @@ -8401,8 +8400,8 @@ def _gotitem( self, key: IndexLabel, ndim: int, - subset: FrameOrSeriesUnion | None = None, - ) -> FrameOrSeriesUnion: + subset: DataFrame | Series | None = None, + ) -> DataFrame | Series: """ Sub-classes to define. Return a sliced object. @@ -8931,7 +8930,7 @@ def append( def join( self, - other: FrameOrSeriesUnion, + other: DataFrame | Series, on: IndexLabel | None = None, how: str = "left", lsuffix: str = "", @@ -9061,7 +9060,7 @@ def join( def _join_compat( self, - other: FrameOrSeriesUnion, + other: DataFrame | Series, on: IndexLabel | None = None, how: str = "left", lsuffix: str = "", @@ -9131,7 +9130,7 @@ def _join_compat( @Appender(_merge_doc, indents=2) def merge( self, - right: FrameOrSeriesUnion, + right: DataFrame | Series, how: str = "inner", on: IndexLabel | None = None, left_on: IndexLabel | None = None, @@ -10724,7 +10723,7 @@ def _from_nested_dict(data) -> collections.defaultdict: return new_data -def _reindex_for_setitem(value: FrameOrSeriesUnion, index: Index) -> ArrayLike: +def _reindex_for_setitem(value: DataFrame | Series, index: Index) -> ArrayLike: # reindex if necessary if value.index.equals(index) or not len(index): diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 69f992f840c7c..d50e921347f98 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -33,7 +33,6 @@ from pandas._typing import ( ArrayLike, FrameOrSeries, - FrameOrSeriesUnion, Manager2D, ) from pandas.util._decorators import ( @@ -296,7 +295,7 @@ def _aggregate_multiple_funcs(self, arg) -> DataFrame: arg = zip(columns, arg) - results: dict[base.OutputKey, FrameOrSeriesUnion] = {} + results: dict[base.OutputKey, DataFrame | Series] = {} for idx, (name, func) in enumerate(arg): key = base.OutputKey(label=name, position=idx) @@ -422,7 +421,7 @@ def _wrap_applied_output( keys: Index, values: list[Any] | None, not_indexed_same: bool = False, - ) -> FrameOrSeriesUnion: + ) -> DataFrame | Series: """ Wrap the output of SeriesGroupBy.apply into the expected result. @@ -1191,7 +1190,7 @@ def _wrap_applied_output_series( not_indexed_same: bool, first_not_none, key_index, - ) -> FrameOrSeriesUnion: + ) -> DataFrame | Series: # this is to silence a DeprecationWarning # TODO: Remove when default dtype of empty Series is object kwargs = first_not_none._construct_axes_dict() diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index f694dcce809ea..90a641a246eb2 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -45,7 +45,6 @@ class providing the base-class of operations. ArrayLike, F, FrameOrSeries, - FrameOrSeriesUnion, IndexLabel, Scalar, T, @@ -728,7 +727,7 @@ def pipe( plot = property(GroupByPlot) @final - def get_group(self, name, obj=None) -> FrameOrSeriesUnion: + def get_group(self, name, obj=None) -> DataFrame | Series: """ Construct DataFrame from group with provided name. @@ -1267,8 +1266,8 @@ def f(g): @final def _python_apply_general( - self, f: F, data: FrameOrSeriesUnion - ) -> FrameOrSeriesUnion: + self, f: F, data: DataFrame | Series + ) -> DataFrame | Series: """ Apply function f in python space @@ -1786,7 +1785,7 @@ def sem(self, ddof: int = 1): @final @Substitution(name="groupby") @Appender(_common_see_also) - def size(self) -> FrameOrSeriesUnion: + def size(self) -> DataFrame | Series: """ Compute group sizes. diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index ea34bc75b4e31..4952fd5582fdb 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -15,7 +15,6 @@ import numpy as np -from pandas._typing import FrameOrSeriesUnion from pandas.util._decorators import ( cache_readonly, deprecate_nonkeyword_arguments, @@ -83,7 +82,7 @@ def concat( verify_integrity: bool = False, sort: bool = False, copy: bool = True, -) -> FrameOrSeriesUnion: +) -> DataFrame | Series: ... @@ -99,7 +98,7 @@ def concat( verify_integrity: bool = False, sort: bool = False, copy: bool = True, -) -> FrameOrSeriesUnion: +) -> DataFrame | Series: """ Concatenate pandas objects along a particular axis with optional set logic along the other axes. @@ -454,7 +453,7 @@ def __init__( if self._is_frame and axis == 1: name = 0 # mypy needs to know sample is not an NDFrame - sample = cast("FrameOrSeriesUnion", sample) + sample = cast("DataFrame | Series", sample) obj = sample._constructor({name: obj}) self.objs.append(obj) @@ -474,8 +473,8 @@ def __init__( self.new_axes = self._get_new_axes() def get_result(self): - cons: type[FrameOrSeriesUnion] - sample: FrameOrSeriesUnion + cons: type[DataFrame | Series] + sample: DataFrame | Series # series only if self._is_series: diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 51556fda6da04..d96f2628088a5 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -14,7 +14,6 @@ AggFuncType, AggFuncTypeBase, AggFuncTypeDict, - FrameOrSeriesUnion, IndexLabel, ) from pandas.util._decorators import ( @@ -254,7 +253,7 @@ def __internal_pivot_table( def _add_margins( - table: FrameOrSeriesUnion, + table: DataFrame | Series, data, values, rows, diff --git a/pandas/core/series.py b/pandas/core/series.py index 59ea6710ea6cd..5173e98ab8136 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -39,7 +39,6 @@ Dtype, DtypeObj, FillnaOptions, - FrameOrSeriesUnion, IndexKeyFunc, NpDtype, SingleManager, @@ -3022,7 +3021,7 @@ def compare( align_axis: Axis = 1, keep_shape: bool = False, keep_equal: bool = False, - ) -> FrameOrSeriesUnion: + ) -> DataFrame | Series: return super().compare( other=other, align_axis=align_axis, @@ -4178,7 +4177,7 @@ def aggregate(self, func=None, axis=0, *args, **kwargs): ) def transform( self, func: AggFuncType, axis: Axis = 0, *args, **kwargs - ) -> FrameOrSeriesUnion: + ) -> DataFrame | Series: # Validate axis argument self._get_axis_number(axis) result = SeriesApply( @@ -4192,7 +4191,7 @@ def apply( convert_dtype: bool = True, args: tuple[Any, ...] = (), **kwargs, - ) -> FrameOrSeriesUnion: + ) -> DataFrame | Series: """ Invoke function on values of Series. diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index 323cb6bd9fedd..73317cc39c2af 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -13,10 +13,7 @@ import numpy as np import pandas._libs.lib as lib -from pandas._typing import ( - DtypeObj, - FrameOrSeriesUnion, -) +from pandas._typing import DtypeObj from pandas.util._decorators import Appender from pandas.core.dtypes.common import ( @@ -39,7 +36,11 @@ from pandas.core.base import NoNewAttributesMixin if TYPE_CHECKING: - from pandas import Index + from pandas import ( + DataFrame, + Index, + Series, + ) _shared_docs: dict[str, str] = {} _cpython_optimized_encoders = ( @@ -2314,7 +2315,7 @@ def findall(self, pat, flags=0): @forbid_nonstring_types(["bytes"]) def extract( self, pat: str, flags: int = 0, expand: bool = True - ) -> FrameOrSeriesUnion | Index: + ) -> DataFrame | Series | Index: r""" Extract capture groups in the regex `pat` as columns in a DataFrame. diff --git a/pandas/core/util/hashing.py b/pandas/core/util/hashing.py index fb5002648b6a5..03a66225a846f 100644 --- a/pandas/core/util/hashing.py +++ b/pandas/core/util/hashing.py @@ -15,10 +15,7 @@ import numpy as np from pandas._libs.hashing import hash_object_array -from pandas._typing import ( - ArrayLike, - FrameOrSeriesUnion, -) +from pandas._typing import ArrayLike from pandas.core.dtypes.common import ( is_categorical_dtype, @@ -34,6 +31,7 @@ if TYPE_CHECKING: from pandas import ( Categorical, + DataFrame, Index, MultiIndex, Series, @@ -77,7 +75,7 @@ def combine_hash_arrays(arrays: Iterator[np.ndarray], num_items: int) -> np.ndar def hash_pandas_object( - obj: Index | FrameOrSeriesUnion, + obj: Index | DataFrame | Series, index: bool = True, encoding: str = "utf8", hash_key: str | None = _default_hash_key, diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index c1d532d94eb83..664089fa37b83 100644 --- a/pandas/core/window/ewm.py +++ b/pandas/core/window/ewm.py @@ -3,6 +3,7 @@ import datetime from functools import partial from textwrap import dedent +from typing import TYPE_CHECKING import warnings import numpy as np @@ -12,9 +13,12 @@ from pandas._typing import ( Axis, FrameOrSeries, - FrameOrSeriesUnion, TimedeltaConvertibleTypes, ) + +if TYPE_CHECKING: + from pandas import DataFrame, Series + from pandas.compat.numpy import function as nv from pandas.util._decorators import doc @@ -558,7 +562,7 @@ def var_func(values, begin, end, min_periods): ) def cov( self, - other: FrameOrSeriesUnion | None = None, + other: DataFrame | Series | None = None, pairwise: bool | None = None, bias: bool = False, **kwargs, @@ -625,7 +629,7 @@ def cov_func(x, y): ) def corr( self, - other: FrameOrSeriesUnion | None = None, + other: DataFrame | Series | None = None, pairwise: bool | None = None, **kwargs, ): @@ -761,7 +765,7 @@ def std(self, bias: bool = False, *args, **kwargs): def corr( self, - other: FrameOrSeriesUnion | None = None, + other: DataFrame | Series | None = None, pairwise: bool | None = None, **kwargs, ): @@ -769,7 +773,7 @@ def corr( def cov( self, - other: FrameOrSeriesUnion | None = None, + other: DataFrame | Series | None = None, pairwise: bool | None = None, bias: bool = False, **kwargs, diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py index 02cf31cad7b8d..eedb6930bad66 100644 --- a/pandas/core/window/expanding.py +++ b/pandas/core/window/expanding.py @@ -2,6 +2,7 @@ from textwrap import dedent from typing import ( + TYPE_CHECKING, Any, Callable, ) @@ -9,8 +10,11 @@ from pandas._typing import ( Axis, FrameOrSeries, - FrameOrSeriesUnion, ) + +if TYPE_CHECKING: + from pandas import DataFrame, Series + from pandas.compat.numpy import function as nv from pandas.util._decorators import doc @@ -591,7 +595,7 @@ def quantile( ) def cov( self, - other: FrameOrSeriesUnion | None = None, + other: DataFrame | Series | None = None, pairwise: bool | None = None, ddof: int = 1, **kwargs, @@ -656,7 +660,7 @@ def cov( ) def corr( self, - other: FrameOrSeriesUnion | None = None, + other: DataFrame | Series | None = None, pairwise: bool | None = None, ddof: int = 1, **kwargs, diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 2d5f148a6437a..31951b3f29a82 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -28,7 +28,6 @@ ArrayLike, Axis, FrameOrSeries, - FrameOrSeriesUnion, ) from pandas.compat._optional import import_optional_dependency from pandas.compat.numpy import function as nv @@ -408,7 +407,7 @@ def _apply_series( def _apply_blockwise( self, homogeneous_func: Callable[..., ArrayLike], name: str | None = None - ) -> FrameOrSeriesUnion: + ) -> DataFrame | Series: """ Apply the given function to the DataFrame broken down into homogeneous sub-frames. @@ -443,7 +442,7 @@ def hfunc2d(values: ArrayLike) -> ArrayLike: def _apply_tablewise( self, homogeneous_func: Callable[..., ArrayLike], name: str | None = None - ) -> FrameOrSeriesUnion: + ) -> DataFrame | Series: """ Apply the given function to the DataFrame across the entire object """ @@ -460,11 +459,11 @@ def _apply_tablewise( def _apply_pairwise( self, - target: FrameOrSeriesUnion, - other: FrameOrSeriesUnion | None, + target: DataFrame | Series, + other: DataFrame | Series | None, pairwise: bool | None, - func: Callable[[FrameOrSeriesUnion, FrameOrSeriesUnion], FrameOrSeriesUnion], - ) -> FrameOrSeriesUnion: + func: Callable[[DataFrame | Series, DataFrame | Series], DataFrame | Series], + ) -> DataFrame | Series: """ Apply the given pairwise function given 2 pandas objects (DataFrame/Series) """ @@ -639,11 +638,11 @@ def _apply( def _apply_pairwise( self, - target: FrameOrSeriesUnion, - other: FrameOrSeriesUnion | None, + target: DataFrame | Series, + other: DataFrame | Series | None, pairwise: bool | None, - func: Callable[[FrameOrSeriesUnion, FrameOrSeriesUnion], FrameOrSeriesUnion], - ) -> FrameOrSeriesUnion: + func: Callable[[DataFrame | Series, DataFrame | Series], DataFrame | Series], + ) -> DataFrame | Series: """ Apply the given pairwise function given 2 pandas objects (DataFrame/Series) """ @@ -1379,7 +1378,7 @@ def quantile(self, quantile: float, interpolation: str = "linear", **kwargs): def cov( self, - other: FrameOrSeriesUnion | None = None, + other: DataFrame | Series | None = None, pairwise: bool | None = None, ddof: int = 1, **kwargs, @@ -1417,7 +1416,7 @@ def cov_func(x, y): def corr( self, - other: FrameOrSeriesUnion | None = None, + other: DataFrame | Series | None = None, pairwise: bool | None = None, ddof: int = 1, **kwargs, @@ -2159,7 +2158,7 @@ def quantile(self, quantile: float, interpolation: str = "linear", **kwargs): ) def cov( self, - other: FrameOrSeriesUnion | None = None, + other: DataFrame | Series | None = None, pairwise: bool | None = None, ddof: int = 1, **kwargs, @@ -2284,7 +2283,7 @@ def cov( ) def corr( self, - other: FrameOrSeriesUnion | None = None, + other: DataFrame | Series | None = None, pairwise: bool | None = None, ddof: int = 1, **kwargs, diff --git a/pandas/io/formats/info.py b/pandas/io/formats/info.py index e014d7d63a35f..64a59778a54f3 100644 --- a/pandas/io/formats/info.py +++ b/pandas/io/formats/info.py @@ -16,10 +16,7 @@ from pandas._config import get_option -from pandas._typing import ( - Dtype, - FrameOrSeriesUnion, -) +from pandas._typing import Dtype from pandas.core.indexes.api import Index @@ -27,7 +24,10 @@ from pandas.io.formats.printing import pprint_thing if TYPE_CHECKING: - from pandas.core.frame import DataFrame + from pandas.core.frame import ( + DataFrame, + Series, + ) def _put_str(s: str | Dtype, space: int) -> str: @@ -110,7 +110,7 @@ class BaseInfo(ABC): values. """ - data: FrameOrSeriesUnion + data: DataFrame | Series memory_usage: bool | str @property @@ -413,7 +413,7 @@ def get_lines(self) -> list[str]: """Product in a form of list of lines (strings).""" @property - def data(self) -> FrameOrSeriesUnion: + def data(self) -> DataFrame | Series: return self.info.data @property diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 93c3843b36846..6732a4517ec3e 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -23,7 +23,6 @@ Axis, FilePathOrBuffer, FrameOrSeries, - FrameOrSeriesUnion, IndexLabel, Scalar, ) @@ -171,7 +170,7 @@ class Styler(StylerRenderer): def __init__( self, - data: FrameOrSeriesUnion, + data: DataFrame | Series, precision: int | None = None, table_styles: CSSStyles | None = None, uuid: str | None = None, diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 7686d8a340c37..f20c8dcfbca8b 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -20,10 +20,7 @@ from pandas._config import get_option from pandas._libs import lib -from pandas._typing import ( - FrameOrSeriesUnion, - TypedDict, -) +from pandas._typing import TypedDict from pandas.compat._optional import import_optional_dependency from pandas.core.dtypes.generic import ABCSeries @@ -70,7 +67,7 @@ class StylerRenderer: def __init__( self, - data: FrameOrSeriesUnion, + data: DataFrame | Series, uuid: str | None = None, uuid_len: int = 5, table_styles: CSSStyles | None = None, @@ -1159,7 +1156,7 @@ def _pseudo_css(self, uuid: str, name: str, row: int, col: int, text: str): }, ] - def _translate(self, styler_data: FrameOrSeriesUnion, uuid: str, d: dict): + def _translate(self, styler_data: DataFrame | Series, uuid: str, d: dict): """ Mutate the render dictionary to allow for tooltips: diff --git a/pandas/io/json/_json.py b/pandas/io/json/_json.py index 77582c46977c1..fdeda868fdb5e 100644 --- a/pandas/io/json/_json.py +++ b/pandas/io/json/_json.py @@ -21,7 +21,6 @@ from pandas._typing import ( CompressionOptions, DtypeArg, - FrameOrSeriesUnion, IndexLabel, JSONSerializable, StorageOptions, @@ -863,7 +862,7 @@ def __init__( self.convert_dates = convert_dates self.date_unit = date_unit self.keep_default_dates = keep_default_dates - self.obj: FrameOrSeriesUnion | None = None + self.obj: DataFrame | Series | None = None def check_keys_split(self, decoded): """ diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 208b8a008ffe6..3d0a1fe867d21 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -40,7 +40,6 @@ ArrayLike, DtypeArg, FrameOrSeries, - FrameOrSeriesUnion, Shape, ) from pandas.compat._optional import import_optional_dependency @@ -2593,7 +2592,7 @@ class Fixed: pandas_kind: str format_type: str = "fixed" # GH#30962 needed by dask - obj_type: type[FrameOrSeriesUnion] + obj_type: type[DataFrame | Series] ndim: int encoding: str parent: HDFStore @@ -3363,7 +3362,7 @@ def is_multi_index(self) -> bool: return isinstance(self.levels, list) def validate_multiindex( - self, obj: FrameOrSeriesUnion + self, obj: DataFrame | Series ) -> tuple[DataFrame, list[Hashable]]: """ validate that we can store the multi-index; reset and return the @@ -4500,7 +4499,7 @@ class AppendableFrameTable(AppendableTable): pandas_kind = "frame_table" table_type = "appendable_frame" ndim = 2 - obj_type: type[FrameOrSeriesUnion] = DataFrame + obj_type: type[DataFrame | Series] = DataFrame @property def is_transposed(self) -> bool: diff --git a/pandas/plotting/_matplotlib/timeseries.py b/pandas/plotting/_matplotlib/timeseries.py index 3b9c5eae70b42..3cd312b06020d 100644 --- a/pandas/plotting/_matplotlib/timeseries.py +++ b/pandas/plotting/_matplotlib/timeseries.py @@ -16,7 +16,6 @@ to_offset, ) from pandas._libs.tslibs.dtypes import FreqGroup -from pandas._typing import FrameOrSeriesUnion from pandas.core.dtypes.generic import ( ABCDatetimeIndex, @@ -40,6 +39,7 @@ from matplotlib.axes import Axes from pandas import ( + DataFrame, DatetimeIndex, Index, Series, @@ -210,7 +210,7 @@ def _get_freq(ax: Axes, series: Series): return freq, ax_freq -def use_dynamic_x(ax: Axes, data: FrameOrSeriesUnion) -> bool: +def use_dynamic_x(ax: Axes, data: DataFrame | Series) -> bool: freq = _get_index_freq(data.index) ax_freq = _get_ax_freq(ax) diff --git a/pandas/plotting/_matplotlib/tools.py b/pandas/plotting/_matplotlib/tools.py index 9bfa24b6371ab..9d509d02c2e4f 100644 --- a/pandas/plotting/_matplotlib/tools.py +++ b/pandas/plotting/_matplotlib/tools.py @@ -13,8 +13,6 @@ import matplotlib.ticker as ticker import numpy as np -from pandas._typing import FrameOrSeriesUnion - from pandas.core.dtypes.common import is_list_like from pandas.core.dtypes.generic import ( ABCDataFrame, @@ -31,6 +29,11 @@ from matplotlib.lines import Line2D from matplotlib.table import Table + from pandas import ( + DataFrame, + Series, + ) + def do_adjust_figure(fig: Figure): """Whether fig has constrained_layout enabled.""" @@ -55,7 +58,7 @@ def format_date_labels(ax: Axes, rot): def table( - ax, data: FrameOrSeriesUnion, rowLabels=None, colLabels=None, **kwargs + ax, data: DataFrame | Series, rowLabels=None, colLabels=None, **kwargs ) -> Table: if isinstance(data, ABCSeries): data = data.to_frame()