Skip to content

Commit 9632f2e

Browse files
authored
TYP: Remove NDFrame._add_series_or_dataframe_operations (#35957)
1 parent 5368397 commit 9632f2e

File tree

6 files changed

+323
-343
lines changed

6 files changed

+323
-343
lines changed

pandas/core/frame.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9306,7 +9306,6 @@ def _AXIS_NAMES(self) -> Dict[int, str]:
93069306

93079307

93089308
DataFrame._add_numeric_operations()
9309-
DataFrame._add_series_or_dataframe_operations()
93109309

93119310
ops.add_flex_arithmetic_methods(DataFrame)
93129311
ops.add_special_arithmetic_methods(DataFrame)

pandas/core/generic.py

Lines changed: 68 additions & 227 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import operator
77
import pickle
88
import re
9-
from textwrap import dedent
109
from typing import (
1110
TYPE_CHECKING,
1211
Any,
@@ -101,17 +100,22 @@
101100
from pandas.core.missing import find_valid_index
102101
from pandas.core.ops import _align_method_FRAME
103102
from pandas.core.shared_docs import _shared_docs
103+
from pandas.core.window import Expanding, ExponentialMovingWindow, Rolling, Window
104104

105105
from pandas.io.formats import format as fmt
106106
from pandas.io.formats.format import DataFrameFormatter, format_percentiles
107107
from pandas.io.formats.printing import pprint_thing
108108

109109
if TYPE_CHECKING:
110+
from pandas._libs.tslibs import BaseOffset
111+
110112
from pandas.core.resample import Resampler
111113
from pandas.core.series import Series # noqa: F401
114+
from pandas.core.window.indexers import BaseIndexer
112115

113116
# goal is to be able to define the docs close to function, while still being
114117
# able to share
118+
_shared_docs = {**_shared_docs}
115119
_shared_doc_kwargs = dict(
116120
axes="keywords for axes",
117121
klass="Series/DataFrame",
@@ -5128,51 +5132,6 @@ def pipe(self, func, *args, **kwargs):
51285132
"""
51295133
return com.pipe(self, func, *args, **kwargs)
51305134

5131-
_shared_docs["aggregate"] = dedent(
5132-
"""
5133-
Aggregate using one or more operations over the specified axis.
5134-
{versionadded}
5135-
Parameters
5136-
----------
5137-
func : function, str, list or dict
5138-
Function to use for aggregating the data. If a function, must either
5139-
work when passed a {klass} or when passed to {klass}.apply.
5140-
5141-
Accepted combinations are:
5142-
5143-
- function
5144-
- string function name
5145-
- list of functions and/or function names, e.g. ``[np.sum, 'mean']``
5146-
- dict of axis labels -> functions, function names or list of such.
5147-
{axis}
5148-
*args
5149-
Positional arguments to pass to `func`.
5150-
**kwargs
5151-
Keyword arguments to pass to `func`.
5152-
5153-
Returns
5154-
-------
5155-
scalar, Series or DataFrame
5156-
5157-
The return can be:
5158-
5159-
* scalar : when Series.agg is called with single function
5160-
* Series : when DataFrame.agg is called with a single function
5161-
* DataFrame : when DataFrame.agg is called with several functions
5162-
5163-
Return scalar, Series or DataFrame.
5164-
{see_also}
5165-
Notes
5166-
-----
5167-
`agg` is an alias for `aggregate`. Use the alias.
5168-
5169-
In pandas, agg, as most operations just ignores the missing values,
5170-
and returns the operation only considering the values that are present.
5171-
5172-
A passed user-defined-function will be passed a Series for evaluation.
5173-
{examples}"""
5174-
)
5175-
51765135
# ----------------------------------------------------------------------
51775136
# Attribute access
51785137

@@ -7452,77 +7411,6 @@ def clip(
74527411

74537412
return result
74547413

7455-
_shared_docs[
7456-
"groupby"
7457-
] = """
7458-
Group %(klass)s using a mapper or by a Series of columns.
7459-
7460-
A groupby operation involves some combination of splitting the
7461-
object, applying a function, and combining the results. This can be
7462-
used to group large amounts of data and compute operations on these
7463-
groups.
7464-
7465-
Parameters
7466-
----------
7467-
by : mapping, function, label, or list of labels
7468-
Used to determine the groups for the groupby.
7469-
If ``by`` is a function, it's called on each value of the object's
7470-
index. If a dict or Series is passed, the Series or dict VALUES
7471-
will be used to determine the groups (the Series' values are first
7472-
aligned; see ``.align()`` method). If an ndarray is passed, the
7473-
values are used as-is determine the groups. A label or list of
7474-
labels may be passed to group by the columns in ``self``. Notice
7475-
that a tuple is interpreted as a (single) key.
7476-
axis : {0 or 'index', 1 or 'columns'}, default 0
7477-
Split along rows (0) or columns (1).
7478-
level : int, level name, or sequence of such, default None
7479-
If the axis is a MultiIndex (hierarchical), group by a particular
7480-
level or levels.
7481-
as_index : bool, default True
7482-
For aggregated output, return object with group labels as the
7483-
index. Only relevant for DataFrame input. as_index=False is
7484-
effectively "SQL-style" grouped output.
7485-
sort : bool, default True
7486-
Sort group keys. Get better performance by turning this off.
7487-
Note this does not influence the order of observations within each
7488-
group. Groupby preserves the order of rows within each group.
7489-
group_keys : bool, default True
7490-
When calling apply, add group keys to index to identify pieces.
7491-
squeeze : bool, default False
7492-
Reduce the dimensionality of the return type if possible,
7493-
otherwise return a consistent type.
7494-
7495-
.. deprecated:: 1.1.0
7496-
7497-
observed : bool, default False
7498-
This only applies if any of the groupers are Categoricals.
7499-
If True: only show observed values for categorical groupers.
7500-
If False: show all values for categorical groupers.
7501-
7502-
.. versionadded:: 0.23.0
7503-
dropna : bool, default True
7504-
If True, and if group keys contain NA values, NA values together
7505-
with row/column will be dropped.
7506-
If False, NA values will also be treated as the key in groups
7507-
7508-
.. versionadded:: 1.1.0
7509-
7510-
Returns
7511-
-------
7512-
%(klass)sGroupBy
7513-
Returns a groupby object that contains information about the groups.
7514-
7515-
See Also
7516-
--------
7517-
resample : Convenience method for frequency conversion and resampling
7518-
of time series.
7519-
7520-
Notes
7521-
-----
7522-
See the `user guide
7523-
<https://pandas.pydata.org/pandas-docs/stable/groupby.html>`_ for more.
7524-
"""
7525-
75267414
def asfreq(
75277415
self: FrameOrSeries,
75287416
freq,
@@ -8431,35 +8319,6 @@ def ranker(data):
84318319

84328320
return ranker(data)
84338321

8434-
_shared_docs[
8435-
"compare"
8436-
] = """
8437-
Compare to another %(klass)s and show the differences.
8438-
8439-
.. versionadded:: 1.1.0
8440-
8441-
Parameters
8442-
----------
8443-
other : %(klass)s
8444-
Object to compare with.
8445-
8446-
align_axis : {0 or 'index', 1 or 'columns'}, default 1
8447-
Determine which axis to align the comparison on.
8448-
8449-
* 0, or 'index' : Resulting differences are stacked vertically
8450-
with rows drawn alternately from self and other.
8451-
* 1, or 'columns' : Resulting differences are aligned horizontally
8452-
with columns drawn alternately from self and other.
8453-
8454-
keep_shape : bool, default False
8455-
If true, all rows and columns are kept.
8456-
Otherwise, only the ones with different values are kept.
8457-
8458-
keep_equal : bool, default False
8459-
If true, the result keeps values that are equal.
8460-
Otherwise, equal values are shown as NaNs.
8461-
"""
8462-
84638322
@Appender(_shared_docs["compare"] % _shared_doc_kwargs)
84648323
def compare(
84658324
self,
@@ -10589,45 +10448,21 @@ def mad(self, axis=None, skipna=None, level=None):
1058910448
examples=_min_examples,
1059010449
)
1059110450

10592-
@classmethod
10593-
def _add_series_or_dataframe_operations(cls):
10594-
"""
10595-
Add the series or dataframe only operations to the cls; evaluate
10596-
the doc strings again.
10597-
"""
10598-
from pandas.core.window import (
10599-
Expanding,
10600-
ExponentialMovingWindow,
10601-
Rolling,
10602-
Window,
10603-
)
10604-
10605-
@doc(Rolling)
10606-
def rolling(
10607-
self,
10608-
window,
10609-
min_periods=None,
10610-
center=False,
10611-
win_type=None,
10612-
on=None,
10613-
axis=0,
10614-
closed=None,
10615-
):
10616-
axis = self._get_axis_number(axis)
10617-
10618-
if win_type is not None:
10619-
return Window(
10620-
self,
10621-
window=window,
10622-
min_periods=min_periods,
10623-
center=center,
10624-
win_type=win_type,
10625-
on=on,
10626-
axis=axis,
10627-
closed=closed,
10628-
)
10451+
@doc(Rolling)
10452+
def rolling(
10453+
self,
10454+
window: "Union[int, timedelta, BaseOffset, BaseIndexer]",
10455+
min_periods: Optional[int] = None,
10456+
center: bool_t = False,
10457+
win_type: Optional[str] = None,
10458+
on: Optional[str] = None,
10459+
axis: Axis = 0,
10460+
closed: Optional[str] = None,
10461+
):
10462+
axis = self._get_axis_number(axis)
1062910463

10630-
return Rolling(
10464+
if win_type is not None:
10465+
return Window(
1063110466
self,
1063210467
window=window,
1063310468
min_periods=min_periods,
@@ -10638,53 +10473,59 @@ def rolling(
1063810473
closed=closed,
1063910474
)
1064010475

10641-
cls.rolling = rolling
10642-
10643-
@doc(Expanding)
10644-
def expanding(self, min_periods=1, center=None, axis=0):
10645-
axis = self._get_axis_number(axis)
10646-
if center is not None:
10647-
warnings.warn(
10648-
"The `center` argument on `expanding` "
10649-
"will be removed in the future",
10650-
FutureWarning,
10651-
stacklevel=2,
10652-
)
10653-
else:
10654-
center = False
10476+
return Rolling(
10477+
self,
10478+
window=window,
10479+
min_periods=min_periods,
10480+
center=center,
10481+
win_type=win_type,
10482+
on=on,
10483+
axis=axis,
10484+
closed=closed,
10485+
)
1065510486

10656-
return Expanding(self, min_periods=min_periods, center=center, axis=axis)
10487+
@doc(Expanding)
10488+
def expanding(
10489+
self, min_periods: int = 1, center: Optional[bool_t] = None, axis: Axis = 0
10490+
) -> Expanding:
10491+
axis = self._get_axis_number(axis)
10492+
if center is not None:
10493+
warnings.warn(
10494+
"The `center` argument on `expanding` will be removed in the future",
10495+
FutureWarning,
10496+
stacklevel=2,
10497+
)
10498+
else:
10499+
center = False
1065710500

10658-
cls.expanding = expanding
10501+
return Expanding(self, min_periods=min_periods, center=center, axis=axis)
1065910502

10660-
@doc(ExponentialMovingWindow)
10661-
def ewm(
10503+
@doc(ExponentialMovingWindow)
10504+
def ewm(
10505+
self,
10506+
com: Optional[float] = None,
10507+
span: Optional[float] = None,
10508+
halflife: Optional[Union[float, TimedeltaConvertibleTypes]] = None,
10509+
alpha: Optional[float] = None,
10510+
min_periods: int = 0,
10511+
adjust: bool_t = True,
10512+
ignore_na: bool_t = False,
10513+
axis: Axis = 0,
10514+
times: Optional[Union[str, np.ndarray, FrameOrSeries]] = None,
10515+
) -> ExponentialMovingWindow:
10516+
axis = self._get_axis_number(axis)
10517+
return ExponentialMovingWindow(
1066210518
self,
10663-
com=None,
10664-
span=None,
10665-
halflife=None,
10666-
alpha=None,
10667-
min_periods=0,
10668-
adjust=True,
10669-
ignore_na=False,
10670-
axis=0,
10671-
times=None,
10672-
):
10673-
axis = self._get_axis_number(axis)
10674-
return ExponentialMovingWindow(
10675-
self,
10676-
com=com,
10677-
span=span,
10678-
halflife=halflife,
10679-
alpha=alpha,
10680-
min_periods=min_periods,
10681-
adjust=adjust,
10682-
ignore_na=ignore_na,
10683-
axis=axis,
10684-
times=times,
10685-
)
10686-
10687-
cls.ewm = ewm
10519+
com=com,
10520+
span=span,
10521+
halflife=halflife,
10522+
alpha=alpha,
10523+
min_periods=min_periods,
10524+
adjust=adjust,
10525+
ignore_na=ignore_na,
10526+
axis=axis,
10527+
times=times,
10528+
)
1068810529

1068910530
@doc(klass=_shared_doc_kwargs["klass"], axis="")
1069010531
def transform(self, func, *args, **kwargs):

pandas/core/series.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5000,7 +5000,6 @@ def to_period(self, freq=None, copy=True) -> "Series":
50005000

50015001

50025002
Series._add_numeric_operations()
5003-
Series._add_series_or_dataframe_operations()
50045003

50055004
# Add arithmetic!
50065005
ops.add_flex_arithmetic_methods(Series)

0 commit comments

Comments
 (0)