Skip to content

DOC:Remove hard-coded examples from _flex_doc_SERIES (#24589) #25524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

266 changes: 210 additions & 56 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,57 +384,252 @@ def _get_op_name(op, special):
# -----------------------------------------------------------------------------
# Docstring Generation and Templates

_add_example_SERIES = """
Examples
--------
>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
>>> a
a 1.0
b 1.0
c 1.0
d NaN
dtype: float64
>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
>>> b
a 1.0
b NaN
d 1.0
e NaN
dtype: float64
>>> a.add(b, fill_value=0)
a 2.0
b 1.0
c 1.0
d 1.0
e NaN
dtype: float64
"""

_sub_example_SERIES = """
Examples
--------
>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
>>> a
a 1.0
b 1.0
c 1.0
d NaN
dtype: float64
>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
>>> b
a 1.0
b NaN
d 1.0
e NaN
dtype: float64
>>> a.subtract(b, fill_value=0)
a 0.0
b 1.0
c 1.0
d -1.0
e NaN
dtype: float64
"""

_mul_example_SERIES = """
Examples
--------
>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
>>> a
a 1.0
b 1.0
c 1.0
d NaN
dtype: float64
>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
>>> b
a 1.0
b NaN
d 1.0
e NaN
dtype: float64
>>> a.multiply(b, fill_value=0)
a 1.0
b 0.0
c 0.0
d 0.0
e NaN
dtype: float64
"""

_div_example_SERIES = """
Examples
--------
>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
>>> a
a 1.0
b 1.0
c 1.0
d NaN
dtype: float64
>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
>>> b
a 1.0
b NaN
d 1.0
e NaN
dtype: float64
>>> a.divide(b, fill_value=0)
a 1.0
b inf
c inf
d 0.0
e NaN
dtype: float64
"""

_floordiv_example_SERIES = """
Examples
--------
>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
>>> a
a 1.0
b 1.0
c 1.0
d NaN
dtype: float64
>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
>>> b
a 1.0
b NaN
d 1.0
e NaN
dtype: float64
>>> a.floordiv(b, fill_value=0)
a 1.0
b NaN
c NaN
d 0.0
e NaN
dtype: float64
"""

_mod_example_SERIES = """
Examples
--------
>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
>>> a
a 1.0
b 1.0
c 1.0
d NaN
dtype: float64
>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
>>> b
a 1.0
b NaN
d 1.0
e NaN
dtype: float64
>>> a.mod(b, fill_value=0)
a 0.0
b NaN
c NaN
d 0.0
e NaN
dtype: float64
"""
_pow_example_SERIES = """
Examples
--------
>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
>>> a
a 1.0
b 1.0
c 1.0
d NaN
dtype: float64
>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
>>> b
a 1.0
b NaN
d 1.0
e NaN
dtype: float64
>>> a.pow(b, fill_value=0)
a 1.0
b 1.0
c 1.0
d 0.0
e NaN
dtype: float64
"""

_op_descriptions = {
# Arithmetic Operators
'add': {'op': '+',
'desc': 'Addition',
'reverse': 'radd'},
'reverse': 'radd',
'series_examples': _add_example_SERIES},
'sub': {'op': '-',
'desc': 'Subtraction',
'reverse': 'rsub'},
'reverse': 'rsub',
'series_examples': _sub_example_SERIES},
'mul': {'op': '*',
'desc': 'Multiplication',
'reverse': 'rmul',
'series_examples': _mul_example_SERIES,
'df_examples': None},
'mod': {'op': '%',
'desc': 'Modulo',
'reverse': 'rmod'},
'reverse': 'rmod',
'series_examples': _mod_example_SERIES},
'pow': {'op': '**',
'desc': 'Exponential power',
'reverse': 'rpow',
'series_examples': _pow_example_SERIES,
'df_examples': None},
'truediv': {'op': '/',
'desc': 'Floating division',
'reverse': 'rtruediv',
'series_examples': _div_example_SERIES,
'df_examples': None},
'floordiv': {'op': '//',
'desc': 'Integer division',
'reverse': 'rfloordiv',
'series_examples': _floordiv_example_SERIES,
'df_examples': None},
'divmod': {'op': 'divmod',
'desc': 'Integer division and modulo',
'reverse': 'rdivmod',
'series_examples': None,
'df_examples': None},

# Comparison Operators
'eq': {'op': '==',
'desc': 'Equal to',
'reverse': None},
'reverse': None,
'series_examples': None},
'ne': {'op': '!=',
'desc': 'Not equal to',
'reverse': None},
'reverse': None,
'series_examples': None},
'lt': {'op': '<',
'desc': 'Less than',
'reverse': None},
'reverse': None,
'series_examples': None},
'le': {'op': '<=',
'desc': 'Less than or equal to',
'reverse': None},
'reverse': None,
'series_examples': None},
'gt': {'op': '>',
'desc': 'Greater than',
'reverse': None},
'reverse': None,
'series_examples': None},
'ge': {'op': '>=',
'desc': 'Greater than or equal to',
'reverse': None}
'reverse': None,
'series_examples': None}
}

_op_names = list(_op_descriptions.keys())
Expand Down Expand Up @@ -472,51 +667,6 @@ def _get_op_name(op, special):
See Also
--------
Series.{reverse}

Examples
--------
>>> a = pd.Series([1, 1, 1, np.nan], index=['a', 'b', 'c', 'd'])
>>> a
a 1.0
b 1.0
c 1.0
d NaN
dtype: float64
>>> b = pd.Series([1, np.nan, 1, np.nan], index=['a', 'b', 'd', 'e'])
>>> b
a 1.0
b NaN
d 1.0
e NaN
dtype: float64
>>> a.add(b, fill_value=0)
a 2.0
b 1.0
c 1.0
d 1.0
e NaN
dtype: float64
>>> a.subtract(b, fill_value=0)
a 0.0
b 1.0
c 1.0
d -1.0
e NaN
dtype: float64
>>> a.multiply(b)
a 1.0
b NaN
c NaN
d NaN
e NaN
dtype: float64
>>> a.divide(b, fill_value=0)
a 1.0
b inf
c inf
d 0.0
e NaN
dtype: float64
"""

_arith_doc_FRAME = """
Expand Down Expand Up @@ -906,8 +1056,12 @@ def _make_flex_doc(op_name, typ):

if typ == 'series':
base_doc = _flex_doc_SERIES
doc = base_doc.format(desc=op_desc['desc'], op_name=op_name,
equiv=equiv, reverse=op_desc['reverse'])
doc_no_examples = base_doc.format(desc=op_desc['desc'],
op_name=op_name, equiv=equiv, reverse=op_desc['reverse'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is what failed the build. Should be aligned with the open paren on the previous line, or move desc=... down here.

if op_desc['series_examples']:
doc = doc_no_examples + op_desc['series_examples']
else:
doc = doc_no_examples
elif typ == 'dataframe':
base_doc = _flex_doc_FRAME
doc = base_doc.format(desc=op_desc['desc'], op_name=op_name,
Expand Down