-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: Updating operators docstrings #20415
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
Changes from 10 commits
acab08e
1818aeb
86cfd56
b68b61f
13fed5f
8bdbc14
4668c5f
e6eb9b9
db143c4
33ff1e4
e138d92
50e9d98
aa016fd
c2cc037
644273b
240a502
bbcdcbe
a33f003
70950c0
6bcb9b9
e7da1e9
20cbec1
722ae81
4580f7a
49c7b82
1e4e450
ec71a04
25129ff
d344688
eaaee0d
e777e87
6879e89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -436,27 +436,33 @@ def _get_op_name(op, special): | |
'eq': {'op': '==', | ||
'desc': 'Equal to', | ||
'reverse': None, | ||
'df_examples': None}, | ||
'df_examples': None, | ||
'others': None}, | ||
'ne': {'op': '!=', | ||
'desc': 'Not equal to', | ||
'reverse': None, | ||
'df_examples': None}, | ||
'df_examples': None, | ||
'others': None}, | ||
'lt': {'op': '<', | ||
'desc': 'Less than', | ||
'reverse': None, | ||
'df_examples': None}, | ||
'df_examples': None, | ||
'others': None}, | ||
'le': {'op': '<=', | ||
'desc': 'Less than or equal to', | ||
'reverse': None, | ||
'df_examples': None}, | ||
'reverse': 'gt', | ||
'df_examples': None, | ||
'others': None}, | ||
'gt': {'op': '>', | ||
'desc': 'Greater than', | ||
'reverse': None, | ||
'df_examples': None}, | ||
'df_examples': None, | ||
'others': None}, | ||
'ge': {'op': '>=', | ||
'desc': 'Greater than or equal to', | ||
'reverse': None, | ||
'df_examples': None}} | ||
'df_examples': None, | ||
'others': None}} | ||
|
||
_op_names = list(_op_descriptions.keys()) | ||
for key in _op_names: | ||
|
@@ -582,6 +588,150 @@ def _get_op_name(op, special): | |
DataFrame.{reverse} | ||
""" | ||
|
||
_flex_comp_doc_FRAME = """ | ||
Flexible wrappers to comparison operators (`eq`, `ne`, `le`, `lt`, `ge`, `gt`). | ||
|
||
Equivalent to `==`, `=!`, `<=`, `<`, `>=`, `>` with support to choose | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused... In the previous dictionaries, you define There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be consistent with examples that run through several of these sibling methods, the intro lists all of them. It seems counterintuitive to have an intro that describes one method with examples from several methods. |
||
axis (rows or columns) and level for comparison. | ||
|
||
Parameters | ||
---------- | ||
other : constant, list, tuple, ndarray, Series, or DataFrame | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
Any single or multiple element data structure, or list-like object. | ||
axis : int or str, optional | ||
Axis to target. Can be either the axis name ('index', 'rows', | ||
'columns') or number (0, 1). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be consistent, I'd document the axis as we usually do. See this docstring: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop.html |
||
level : int or name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
Broadcast across a level, matching Index values on the passed | ||
MultiIndex level. | ||
|
||
Returns | ||
------- | ||
result : DataFrame of bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
Result of the comparison. | ||
|
||
See also | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see no current docs page that has |
||
-------- | ||
DataFrame.eq : Compare DataFrames for equality elementwise | ||
DataFrame.ne : Compare DataFrames for inequality elementwise | ||
DataFrame.le : Compare DataFrames for less than inequality | ||
or equality elementwise | ||
DataFrame.lt : Compare DataFrames for strictly less than | ||
inequality elementwise | ||
DataFrame.ge : Compare DataFrames for greater than inequality | ||
or equality elementwise | ||
DataFrame.gt : Compare DataFrames for strictly greater than | ||
inequality elementwise | ||
|
||
Notes | ||
-------- | ||
Mismatched indices will be unioned together. | ||
datapythonista marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Examples | ||
-------- | ||
>>> df1 = pd.DataFrame({'company': ['A', 'B', 'C'], | ||
... 'cost': [250, 150, 100], | ||
... 'revenue': [100, 250, 300]}) | ||
>>> df1 | ||
company cost revenue | ||
0 A 250 100 | ||
1 B 150 250 | ||
2 C 100 300 | ||
>>> df2 = pd.DataFrame({'company': ['A', 'B', 'C', 'D'], | ||
... 'revenue': [300, 250, 100, 150]}) | ||
>>> df2 | ||
company revenue | ||
0 A 300 | ||
1 B 250 | ||
2 C 100 | ||
3 D 150 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead if defining all the DataFrames first, I'd define the first, show the examples that don't need the others, and define the others immediately before they are used. For the naming, I'd name I also think that would make things easier for the user having smaller DataFrames (like 2x2). These big There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood on names. As for lengths, df1 and df2 have different number of rows and columns to show results of across different lengths. And it is just one more row and one more column. Surely not that big. I can add a note to mention results of different lengths. |
||
>>> df3 = pd.DataFrame({'cost': [250, 150, 100, 150, 300, 220], | ||
... 'revenue': [100, 250, 300, 200, 175, 225]}, | ||
... index = [['Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2'], | ||
... ['A', 'B', 'C', 'A', 'B' ,'C']]) | ||
>>> df3 | ||
cost revenue | ||
Q1 A 250 100 | ||
B 150 250 | ||
C 100 300 | ||
Q2 A 150 200 | ||
B 300 175 | ||
C 220 225 | ||
|
||
Compare to a constant and operator version | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain that both are equivalent in this case? And may be finish the sentence with a period? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
|
||
>>> df1 == 100 | ||
company cost revenue | ||
0 False False True | ||
1 False False False | ||
2 False True False | ||
>>> df1.eq(100) | ||
company cost revenue | ||
0 False False True | ||
1 False False False | ||
2 False True False | ||
|
||
Compare to a Series by axis and operator version | ||
|
||
>>> df1 != [100, 250, 300] | ||
company cost revenue | ||
0 True True False | ||
1 True True False | ||
2 True True False | ||
>>> df1.ne([100, 250, 300], axis=0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
company cost revenue | ||
0 True True False | ||
1 True True False | ||
2 True True False | ||
>>> df1 != pd.Series([100, 250, 300]) | ||
company cost revenue 0 1 2 | ||
0 True True True True True True | ||
1 True True True True True True | ||
2 True True True True True True | ||
>>> df1.ne(pd.Series([100, 250, 300]), axis=1) | ||
company cost revenue 0 1 2 | ||
0 True True True True True True | ||
1 True True True True True True | ||
2 True True True True True True | ||
|
||
Compare to a DataFrame by axis and operator version | ||
|
||
>>> df1.reindex(['company', 'revenue'], axis='columns') > df2.iloc[:-1] | ||
company revenue | ||
0 False False | ||
1 False False | ||
2 False True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not obvious to me what we're showing here. |
||
>>> df1.gt(df2, axis=0) | ||
company cost revenue | ||
0 False False False | ||
1 False False False | ||
2 False False True | ||
3 False False False | ||
>>> df1.gt(df2, axis=1) | ||
company cost revenue | ||
0 False False False | ||
1 False False False | ||
2 False False True | ||
3 False False False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd use |
||
|
||
Compare to a MultiIndex by level and operator version | ||
|
||
>>> df1.set_index('company') <= df3.loc['Q1'] | ||
cost revenue | ||
company | ||
A True True | ||
B True True | ||
C True True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think here we're comparing two |
||
>>> df1.set_index('company').le(df3, level=1) | ||
cost revenue | ||
Q1 A True True | ||
B True True | ||
C True True | ||
Q2 A False True | ||
B True False | ||
C True False | ||
""" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to explain what's going on here. And I'd probably set The blank line after the docstring is not required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has this been resolved? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add this explanation? So users can understand in an easier way what we are showing here. |
||
_flex_doc_PANEL = """ | ||
{desc} of series and other, element-wise (binary operator `{op_name}`). | ||
Equivalent to ``{equiv}``. | ||
|
@@ -1546,8 +1696,12 @@ def na_op(x, y): | |
result = mask_cmp_op(x, y, op, (np.ndarray, ABCSeries)) | ||
return result | ||
|
||
@Appender('Wrapper for flexible comparison methods {name}' | ||
.format(name=op_name)) | ||
if op_name in _op_descriptions: | ||
doc = _flex_comp_doc_FRAME | ||
else: | ||
doc = "Flexible wrappers to comparison methods" | ||
|
||
@Appender(doc) | ||
datapythonista marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def f(self, other, axis=default_axis, level=None): | ||
|
||
other = _align_method_FRAME(self, other, axis) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't use
reverse
,df_examples
andothers
, can we get rid of them instead of having all them toNone
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
df_examples
was kept from original docs and not removed for code consistency if used elsewhere. Can remove and test docstrings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reverse
is needed for compatibility with arithmetic operators as they are included in same dictionary,_op_descriptions