Skip to content

Commit ed2af7e

Browse files
authored
CLN: Replace FrameOrSeriesUnion annotation by DataFrame | Series (#41955)
1 parent 4663377 commit ed2af7e

22 files changed

+112
-124
lines changed

pandas/_typing.py

-6
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@
104104
]
105105
Timezone = Union[str, tzinfo]
106106

107-
# FrameOrSeriesUnion means either a DataFrame or a Series. E.g.
108-
# `def func(a: FrameOrSeriesUnion) -> FrameOrSeriesUnion: ...` means that if a Series
109-
# is passed in, either a Series or DataFrame is returned, and if a DataFrame is passed
110-
# in, either a DataFrame or a Series is returned.
111-
FrameOrSeriesUnion = Union["DataFrame", "Series"]
112-
113107
# FrameOrSeries is stricter and ensures that the same subclass of NDFrame always is
114108
# used. E.g. `def func(a: FrameOrSeries) -> FrameOrSeries: ...` means that if a
115109
# Series is passed into a function, a Series is always returned and if a DataFrame is

pandas/core/algorithms.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
AnyArrayLike,
2626
ArrayLike,
2727
DtypeObj,
28-
FrameOrSeriesUnion,
2928
Scalar,
3029
)
3130
from pandas.util._decorators import doc
@@ -1211,7 +1210,7 @@ def __init__(self, obj, n: int, keep: str):
12111210
if self.keep not in ("first", "last", "all"):
12121211
raise ValueError('keep must be either "first", "last" or "all"')
12131212

1214-
def compute(self, method: str) -> FrameOrSeriesUnion:
1213+
def compute(self, method: str) -> DataFrame | Series:
12151214
raise NotImplementedError
12161215

12171216
def nlargest(self):

pandas/core/apply.py

+19-20
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
AggObjType,
2626
Axis,
2727
FrameOrSeries,
28-
FrameOrSeriesUnion,
2928
)
3029
from pandas.util._decorators import cache_readonly
3130

@@ -137,10 +136,10 @@ def f(x):
137136
self.f: AggFuncType = f
138137

139138
@abc.abstractmethod
140-
def apply(self) -> FrameOrSeriesUnion:
139+
def apply(self) -> DataFrame | Series:
141140
pass
142141

143-
def agg(self) -> FrameOrSeriesUnion | None:
142+
def agg(self) -> DataFrame | Series | None:
144143
"""
145144
Provide an implementation for the aggregators.
146145
@@ -171,7 +170,7 @@ def agg(self) -> FrameOrSeriesUnion | None:
171170
# caller can react
172171
return None
173172

174-
def transform(self) -> FrameOrSeriesUnion:
173+
def transform(self) -> DataFrame | Series:
175174
"""
176175
Transform a DataFrame or Series.
177176
@@ -252,7 +251,7 @@ def transform_dict_like(self, func):
252251

253252
func = self.normalize_dictlike_arg("transform", obj, func)
254253

255-
results: dict[Hashable, FrameOrSeriesUnion] = {}
254+
results: dict[Hashable, DataFrame | Series] = {}
256255
failed_names = []
257256
all_type_errors = True
258257
for name, how in func.items():
@@ -283,7 +282,7 @@ def transform_dict_like(self, func):
283282
)
284283
return concat(results, axis=1)
285284

286-
def transform_str_or_callable(self, func) -> FrameOrSeriesUnion:
285+
def transform_str_or_callable(self, func) -> DataFrame | Series:
287286
"""
288287
Compute transform in the case of a string or callable func
289288
"""
@@ -305,7 +304,7 @@ def transform_str_or_callable(self, func) -> FrameOrSeriesUnion:
305304
except Exception:
306305
return func(obj, *args, **kwargs)
307306

308-
def agg_list_like(self) -> FrameOrSeriesUnion:
307+
def agg_list_like(self) -> DataFrame | Series:
309308
"""
310309
Compute aggregation in the case of a list-like argument.
311310
@@ -402,7 +401,7 @@ def agg_list_like(self) -> FrameOrSeriesUnion:
402401
)
403402
return concatenated.reindex(full_ordered_index, copy=False)
404403

405-
def agg_dict_like(self) -> FrameOrSeriesUnion:
404+
def agg_dict_like(self) -> DataFrame | Series:
406405
"""
407406
Compute aggregation in the case of a dict-like argument.
408407
@@ -481,7 +480,7 @@ def agg_dict_like(self) -> FrameOrSeriesUnion:
481480

482481
return result
483482

484-
def apply_str(self) -> FrameOrSeriesUnion:
483+
def apply_str(self) -> DataFrame | Series:
485484
"""
486485
Compute apply in case of a string.
487486
@@ -506,7 +505,7 @@ def apply_str(self) -> FrameOrSeriesUnion:
506505
raise ValueError(f"Operation {f} does not support axis=1")
507506
return self._try_aggregate_string_function(obj, f, *self.args, **self.kwargs)
508507

509-
def apply_multiple(self) -> FrameOrSeriesUnion:
508+
def apply_multiple(self) -> DataFrame | Series:
510509
"""
511510
Compute apply in case of a list-like or dict-like.
512511
@@ -518,7 +517,7 @@ def apply_multiple(self) -> FrameOrSeriesUnion:
518517
return self.obj.aggregate(self.f, self.axis, *self.args, **self.kwargs)
519518

520519
def normalize_dictlike_arg(
521-
self, how: str, obj: FrameOrSeriesUnion, func: AggFuncTypeDict
520+
self, how: str, obj: DataFrame | Series, func: AggFuncTypeDict
522521
) -> AggFuncTypeDict:
523522
"""
524523
Handler for dict-like argument.
@@ -631,7 +630,7 @@ def series_generator(self) -> Iterator[Series]:
631630
@abc.abstractmethod
632631
def wrap_results_for_axis(
633632
self, results: ResType, res_index: Index
634-
) -> FrameOrSeriesUnion:
633+
) -> DataFrame | Series:
635634
pass
636635

637636
# ---------------------------------------------------------------
@@ -652,7 +651,7 @@ def values(self):
652651
def dtypes(self) -> Series:
653652
return self.obj.dtypes
654653

655-
def apply(self) -> FrameOrSeriesUnion:
654+
def apply(self) -> DataFrame | Series:
656655
"""compute the results"""
657656
# dispatch to agg
658657
if is_list_like(self.f):
@@ -826,7 +825,7 @@ def apply_series_generator(self) -> tuple[ResType, Index]:
826825

827826
return results, res_index
828827

829-
def wrap_results(self, results: ResType, res_index: Index) -> FrameOrSeriesUnion:
828+
def wrap_results(self, results: ResType, res_index: Index) -> DataFrame | Series:
830829
from pandas import Series
831830

832831
# see if we can infer the results
@@ -849,7 +848,7 @@ def wrap_results(self, results: ResType, res_index: Index) -> FrameOrSeriesUnion
849848

850849
return result
851850

852-
def apply_str(self) -> FrameOrSeriesUnion:
851+
def apply_str(self) -> DataFrame | Series:
853852
# Caller is responsible for checking isinstance(self.f, str)
854853
# TODO: GH#39993 - Avoid special-casing by replacing with lambda
855854
if self.f == "size":
@@ -880,7 +879,7 @@ def result_columns(self) -> Index:
880879

881880
def wrap_results_for_axis(
882881
self, results: ResType, res_index: Index
883-
) -> FrameOrSeriesUnion:
882+
) -> DataFrame | Series:
884883
"""return the results for the rows"""
885884

886885
if self.result_type == "reduce":
@@ -963,9 +962,9 @@ def result_columns(self) -> Index:
963962

964963
def wrap_results_for_axis(
965964
self, results: ResType, res_index: Index
966-
) -> FrameOrSeriesUnion:
965+
) -> DataFrame | Series:
967966
"""return the results for the columns"""
968-
result: FrameOrSeriesUnion
967+
result: DataFrame | Series
969968

970969
# we have requested to expand
971970
if self.result_type == "expand":
@@ -1019,7 +1018,7 @@ def __init__(
10191018
kwargs=kwargs,
10201019
)
10211020

1022-
def apply(self) -> FrameOrSeriesUnion:
1021+
def apply(self) -> DataFrame | Series:
10231022
obj = self.obj
10241023

10251024
if len(obj) == 0:
@@ -1070,7 +1069,7 @@ def apply_empty_result(self) -> Series:
10701069
obj, method="apply"
10711070
)
10721071

1073-
def apply_standard(self) -> FrameOrSeriesUnion:
1072+
def apply_standard(self) -> DataFrame | Series:
10741073
f = self.f
10751074
obj = self.obj
10761075

pandas/core/describe.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from pandas._libs.tslibs import Timestamp
2323
from pandas._typing import (
2424
FrameOrSeries,
25-
FrameOrSeriesUnion,
2625
Hashable,
2726
)
2827
from pandas.util._validators import validate_percentile
@@ -107,12 +106,12 @@ class NDFrameDescriberAbstract(ABC):
107106
Whether to treat datetime dtypes as numeric.
108107
"""
109108

110-
def __init__(self, obj: FrameOrSeriesUnion, datetime_is_numeric: bool):
109+
def __init__(self, obj: DataFrame | Series, datetime_is_numeric: bool):
111110
self.obj = obj
112111
self.datetime_is_numeric = datetime_is_numeric
113112

114113
@abstractmethod
115-
def describe(self, percentiles: Sequence[float]) -> FrameOrSeriesUnion:
114+
def describe(self, percentiles: Sequence[float]) -> DataFrame | Series:
116115
"""Do describe either series or dataframe.
117116
118117
Parameters

pandas/core/frame.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
FillnaOptions,
5959
FloatFormatType,
6060
FormattersType,
61-
FrameOrSeriesUnion,
6261
Frequency,
6362
IndexKeyFunc,
6463
IndexLabel,
@@ -1362,7 +1361,7 @@ def dot(self, other: Series) -> Series:
13621361
def dot(self, other: DataFrame | Index | ArrayLike) -> DataFrame:
13631362
...
13641363

1365-
def dot(self, other: AnyArrayLike | FrameOrSeriesUnion) -> FrameOrSeriesUnion:
1364+
def dot(self, other: AnyArrayLike | DataFrame | Series) -> DataFrame | Series:
13661365
"""
13671366
Compute the matrix multiplication between the DataFrame and other.
13681367
@@ -1478,13 +1477,13 @@ def __matmul__(self, other: Series) -> Series:
14781477

14791478
@overload
14801479
def __matmul__(
1481-
self, other: AnyArrayLike | FrameOrSeriesUnion
1482-
) -> FrameOrSeriesUnion:
1480+
self, other: AnyArrayLike | DataFrame | Series
1481+
) -> DataFrame | Series:
14831482
...
14841483

14851484
def __matmul__(
1486-
self, other: AnyArrayLike | FrameOrSeriesUnion
1487-
) -> FrameOrSeriesUnion:
1485+
self, other: AnyArrayLike | DataFrame | Series
1486+
) -> DataFrame | Series:
14881487
"""
14891488
Matrix multiplication using binary `@` operator in Python>=3.5.
14901489
"""
@@ -8432,8 +8431,8 @@ def _gotitem(
84328431
self,
84338432
key: IndexLabel,
84348433
ndim: int,
8435-
subset: FrameOrSeriesUnion | None = None,
8436-
) -> FrameOrSeriesUnion:
8434+
subset: DataFrame | Series | None = None,
8435+
) -> DataFrame | Series:
84378436
"""
84388437
Sub-classes to define. Return a sliced object.
84398438
@@ -8962,7 +8961,7 @@ def append(
89628961

89638962
def join(
89648963
self,
8965-
other: FrameOrSeriesUnion,
8964+
other: DataFrame | Series,
89668965
on: IndexLabel | None = None,
89678966
how: str = "left",
89688967
lsuffix: str = "",
@@ -9092,7 +9091,7 @@ def join(
90929091

90939092
def _join_compat(
90949093
self,
9095-
other: FrameOrSeriesUnion,
9094+
other: DataFrame | Series,
90969095
on: IndexLabel | None = None,
90979096
how: str = "left",
90989097
lsuffix: str = "",
@@ -9162,7 +9161,7 @@ def _join_compat(
91629161
@Appender(_merge_doc, indents=2)
91639162
def merge(
91649163
self,
9165-
right: FrameOrSeriesUnion,
9164+
right: DataFrame | Series,
91669165
how: str = "inner",
91679166
on: IndexLabel | None = None,
91689167
left_on: IndexLabel | None = None,
@@ -10759,7 +10758,7 @@ def _from_nested_dict(data) -> collections.defaultdict:
1075910758
return new_data
1076010759

1076110760

10762-
def _reindex_for_setitem(value: FrameOrSeriesUnion, index: Index) -> ArrayLike:
10761+
def _reindex_for_setitem(value: DataFrame | Series, index: Index) -> ArrayLike:
1076310762
# reindex if necessary
1076410763

1076510764
if value.index.equals(index) or not len(index):

pandas/core/groupby/generic.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from pandas._typing import (
3434
ArrayLike,
3535
FrameOrSeries,
36-
FrameOrSeriesUnion,
3736
Manager2D,
3837
)
3938
from pandas.util._decorators import (
@@ -296,7 +295,7 @@ def _aggregate_multiple_funcs(self, arg) -> DataFrame:
296295

297296
arg = zip(columns, arg)
298297

299-
results: dict[base.OutputKey, FrameOrSeriesUnion] = {}
298+
results: dict[base.OutputKey, DataFrame | Series] = {}
300299
for idx, (name, func) in enumerate(arg):
301300

302301
key = base.OutputKey(label=name, position=idx)
@@ -422,7 +421,7 @@ def _wrap_applied_output(
422421
keys: Index,
423422
values: list[Any] | None,
424423
not_indexed_same: bool = False,
425-
) -> FrameOrSeriesUnion:
424+
) -> DataFrame | Series:
426425
"""
427426
Wrap the output of SeriesGroupBy.apply into the expected result.
428427
@@ -1193,7 +1192,7 @@ def _wrap_applied_output_series(
11931192
not_indexed_same: bool,
11941193
first_not_none,
11951194
key_index,
1196-
) -> FrameOrSeriesUnion:
1195+
) -> DataFrame | Series:
11971196
# this is to silence a DeprecationWarning
11981197
# TODO: Remove when default dtype of empty Series is object
11991198
kwargs = first_not_none._construct_axes_dict()

pandas/core/groupby/groupby.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class providing the base-class of operations.
4545
ArrayLike,
4646
F,
4747
FrameOrSeries,
48-
FrameOrSeriesUnion,
4948
IndexLabel,
5049
RandomState,
5150
Scalar,
@@ -730,7 +729,7 @@ def pipe(
730729
plot = property(GroupByPlot)
731730

732731
@final
733-
def get_group(self, name, obj=None) -> FrameOrSeriesUnion:
732+
def get_group(self, name, obj=None) -> DataFrame | Series:
734733
"""
735734
Construct DataFrame from group with provided name.
736735
@@ -1269,8 +1268,8 @@ def f(g):
12691268

12701269
@final
12711270
def _python_apply_general(
1272-
self, f: F, data: FrameOrSeriesUnion
1273-
) -> FrameOrSeriesUnion:
1271+
self, f: F, data: DataFrame | Series
1272+
) -> DataFrame | Series:
12741273
"""
12751274
Apply function f in python space
12761275
@@ -1792,7 +1791,7 @@ def sem(self, ddof: int = 1):
17921791
@final
17931792
@Substitution(name="groupby")
17941793
@Appender(_common_see_also)
1795-
def size(self) -> FrameOrSeriesUnion:
1794+
def size(self) -> DataFrame | Series:
17961795
"""
17971796
Compute group sizes.
17981797

0 commit comments

Comments
 (0)