Skip to content

Commit 7e0bda3

Browse files
committed
CLN: Replace FrameOrSeriesUnion annotation by DataFrame | Series
1 parent 3653ddd commit 7e0bda3

File tree

22 files changed

+84
-112
lines changed

22 files changed

+84
-112
lines changed

pandas/_typing.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,6 @@
101101
]
102102
Timezone = Union[str, tzinfo]
103103

104-
# FrameOrSeriesUnion means either a DataFrame or a Series. E.g.
105-
# `def func(a: FrameOrSeriesUnion) -> FrameOrSeriesUnion: ...` means that if a Series
106-
# is passed in, either a Series or DataFrame is returned, and if a DataFrame is passed
107-
# in, either a DataFrame or a Series is returned.
108-
FrameOrSeriesUnion = Union["DataFrame", "Series"]
109-
110104
# FrameOrSeries is stricter and ensures that the same subclass of NDFrame always is
111105
# used. E.g. `def func(a: FrameOrSeries) -> FrameOrSeries: ...` means that if a
112106
# Series is passed into a function, a Series is always returned and if a DataFrame is

pandas/core/algorithms.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
AnyArrayLike,
3030
ArrayLike,
3131
DtypeObj,
32-
FrameOrSeriesUnion,
3332
Scalar,
3433
)
3534
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

Lines changed: 19 additions & 20 deletions
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
@@ -399,7 +398,7 @@ def agg_list_like(self) -> FrameOrSeriesUnion:
399398
)
400399
return concatenated.reindex(full_ordered_index, copy=False)
401400

402-
def agg_dict_like(self) -> FrameOrSeriesUnion:
401+
def agg_dict_like(self) -> DataFrame | Series:
403402
"""
404403
Compute aggregation in the case of a dict-like argument.
405404
@@ -467,7 +466,7 @@ def agg_dict_like(self) -> FrameOrSeriesUnion:
467466

468467
return result
469468

470-
def apply_str(self) -> FrameOrSeriesUnion:
469+
def apply_str(self) -> DataFrame | Series:
471470
"""
472471
Compute apply in case of a string.
473472
@@ -492,7 +491,7 @@ def apply_str(self) -> FrameOrSeriesUnion:
492491
raise ValueError(f"Operation {f} does not support axis=1")
493492
return self._try_aggregate_string_function(obj, f, *self.args, **self.kwargs)
494493

495-
def apply_multiple(self) -> FrameOrSeriesUnion:
494+
def apply_multiple(self) -> DataFrame | Series:
496495
"""
497496
Compute apply in case of a list-like or dict-like.
498497
@@ -504,7 +503,7 @@ def apply_multiple(self) -> FrameOrSeriesUnion:
504503
return self.obj.aggregate(self.f, self.axis, *self.args, **self.kwargs)
505504

506505
def normalize_dictlike_arg(
507-
self, how: str, obj: FrameOrSeriesUnion, func: AggFuncTypeDict
506+
self, how: str, obj: DataFrame | Series, func: AggFuncTypeDict
508507
) -> AggFuncTypeDict:
509508
"""
510509
Handler for dict-like argument.
@@ -617,7 +616,7 @@ def series_generator(self) -> Iterator[Series]:
617616
@abc.abstractmethod
618617
def wrap_results_for_axis(
619618
self, results: ResType, res_index: Index
620-
) -> FrameOrSeriesUnion:
619+
) -> DataFrame | Series:
621620
pass
622621

623622
# ---------------------------------------------------------------
@@ -638,7 +637,7 @@ def values(self):
638637
def dtypes(self) -> Series:
639638
return self.obj.dtypes
640639

641-
def apply(self) -> FrameOrSeriesUnion:
640+
def apply(self) -> DataFrame | Series:
642641
"""compute the results"""
643642
# dispatch to agg
644643
if is_list_like(self.f):
@@ -812,7 +811,7 @@ def apply_series_generator(self) -> tuple[ResType, Index]:
812811

813812
return results, res_index
814813

815-
def wrap_results(self, results: ResType, res_index: Index) -> FrameOrSeriesUnion:
814+
def wrap_results(self, results: ResType, res_index: Index) -> DataFrame | Series:
816815
from pandas import Series
817816

818817
# see if we can infer the results
@@ -835,7 +834,7 @@ def wrap_results(self, results: ResType, res_index: Index) -> FrameOrSeriesUnion
835834

836835
return result
837836

838-
def apply_str(self) -> FrameOrSeriesUnion:
837+
def apply_str(self) -> DataFrame | Series:
839838
# Caller is responsible for checking isinstance(self.f, str)
840839
# TODO: GH#39993 - Avoid special-casing by replacing with lambda
841840
if self.f == "size":
@@ -866,7 +865,7 @@ def result_columns(self) -> Index:
866865

867866
def wrap_results_for_axis(
868867
self, results: ResType, res_index: Index
869-
) -> FrameOrSeriesUnion:
868+
) -> DataFrame | Series:
870869
"""return the results for the rows"""
871870

872871
if self.result_type == "reduce":
@@ -949,9 +948,9 @@ def result_columns(self) -> Index:
949948

950949
def wrap_results_for_axis(
951950
self, results: ResType, res_index: Index
952-
) -> FrameOrSeriesUnion:
951+
) -> DataFrame | Series:
953952
"""return the results for the columns"""
954-
result: FrameOrSeriesUnion
953+
result: DataFrame | Series
955954

956955
# we have requested to expand
957956
if self.result_type == "expand":
@@ -1005,7 +1004,7 @@ def __init__(
10051004
kwargs=kwargs,
10061005
)
10071006

1008-
def apply(self) -> FrameOrSeriesUnion:
1007+
def apply(self) -> DataFrame | Series:
10091008
obj = self.obj
10101009

10111010
if len(obj) == 0:
@@ -1056,7 +1055,7 @@ def apply_empty_result(self) -> Series:
10561055
obj, method="apply"
10571056
)
10581057

1059-
def apply_standard(self) -> FrameOrSeriesUnion:
1058+
def apply_standard(self) -> DataFrame | Series:
10601059
f = self.f
10611060
obj = self.obj
10621061

pandas/core/describe.py

Lines changed: 2 additions & 3 deletions
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

Lines changed: 11 additions & 12 deletions
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
"""
@@ -8401,8 +8400,8 @@ def _gotitem(
84018400
self,
84028401
key: IndexLabel,
84038402
ndim: int,
8404-
subset: FrameOrSeriesUnion | None = None,
8405-
) -> FrameOrSeriesUnion:
8403+
subset: DataFrame | Series | None = None,
8404+
) -> DataFrame | Series:
84068405
"""
84078406
Sub-classes to define. Return a sliced object.
84088407
@@ -8931,7 +8930,7 @@ def append(
89318930

89328931
def join(
89338932
self,
8934-
other: FrameOrSeriesUnion,
8933+
other: DataFrame | Series,
89358934
on: IndexLabel | None = None,
89368935
how: str = "left",
89378936
lsuffix: str = "",
@@ -9061,7 +9060,7 @@ def join(
90619060

90629061
def _join_compat(
90639062
self,
9064-
other: FrameOrSeriesUnion,
9063+
other: DataFrame | Series,
90659064
on: IndexLabel | None = None,
90669065
how: str = "left",
90679066
lsuffix: str = "",
@@ -9131,7 +9130,7 @@ def _join_compat(
91319130
@Appender(_merge_doc, indents=2)
91329131
def merge(
91339132
self,
9134-
right: FrameOrSeriesUnion,
9133+
right: DataFrame | Series,
91359134
how: str = "inner",
91369135
on: IndexLabel | None = None,
91379136
left_on: IndexLabel | None = None,
@@ -10724,7 +10723,7 @@ def _from_nested_dict(data) -> collections.defaultdict:
1072410723
return new_data
1072510724

1072610725

10727-
def _reindex_for_setitem(value: FrameOrSeriesUnion, index: Index) -> ArrayLike:
10726+
def _reindex_for_setitem(value: DataFrame | Series, index: Index) -> ArrayLike:
1072810727
# reindex if necessary
1072910728

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

pandas/core/groupby/generic.py

Lines changed: 3 additions & 4 deletions
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
@@ -1191,7 +1190,7 @@ def _wrap_applied_output_series(
11911190
not_indexed_same: bool,
11921191
first_not_none,
11931192
key_index,
1194-
) -> FrameOrSeriesUnion:
1193+
) -> DataFrame | Series:
11951194
# this is to silence a DeprecationWarning
11961195
# TODO: Remove when default dtype of empty Series is object
11971196
kwargs = first_not_none._construct_axes_dict()

pandas/core/groupby/groupby.py

Lines changed: 4 additions & 5 deletions
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
Scalar,
5150
T,
@@ -728,7 +727,7 @@ def pipe(
728727
plot = property(GroupByPlot)
729728

730729
@final
731-
def get_group(self, name, obj=None) -> FrameOrSeriesUnion:
730+
def get_group(self, name, obj=None) -> DataFrame | Series:
732731
"""
733732
Construct DataFrame from group with provided name.
734733
@@ -1267,8 +1266,8 @@ def f(g):
12671266

12681267
@final
12691268
def _python_apply_general(
1270-
self, f: F, data: FrameOrSeriesUnion
1271-
) -> FrameOrSeriesUnion:
1269+
self, f: F, data: DataFrame | Series
1270+
) -> DataFrame | Series:
12721271
"""
12731272
Apply function f in python space
12741273
@@ -1786,7 +1785,7 @@ def sem(self, ddof: int = 1):
17861785
@final
17871786
@Substitution(name="groupby")
17881787
@Appender(_common_see_also)
1789-
def size(self) -> FrameOrSeriesUnion:
1788+
def size(self) -> DataFrame | Series:
17901789
"""
17911790
Compute group sizes.
17921791

0 commit comments

Comments
 (0)