-
- Notifications
You must be signed in to change notification settings - Fork 19.2k
DOC: Generalize "rename" documentation #25577
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
DOC: Generalize "rename" documentation #25577
Conversation
In this commit, the documentation of the rename function of the NDFrame, is generalized over all its children using the function: DataFrame, Series and Panel. These three classes now use the Substitute decorator to add the generic part of the documentation to their own, using the same decorator to replace class specific parameters into the generic documentation.
Codecov Report
@@ Coverage Diff @@ ## master #25577 +/- ## ========================================== + Coverage 91.26% 91.26% +<.01% ========================================== Files 173 173 Lines 52966 52970 +4 ========================================== + Hits 48337 48342 +5 + Misses 4629 4628 -1
Continue to review full report at Codecov.
|
1 similar comment
Codecov Report
@@ Coverage Diff @@ ## master #25577 +/- ## ========================================== + Coverage 91.26% 91.26% +<.01% ========================================== Files 173 173 Lines 52966 52970 +4 ========================================== + Hits 48337 48342 +5 + Misses 4629 4628 -1
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@ ## master #25577 +/- ## ========================================== + Coverage 91.26% 91.29% +0.03% ========================================== Files 173 173 Lines 52968 52965 -3 ========================================== + Hits 48340 48354 +14 + Misses 4628 4611 -17
Continue to review full report at Codecov.
|
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.
Thanks for the PR! Per our conversation on Gitter I think you can actually move a lot more into the generic docstring and reduce the amount of substitution going on here. I think this is confused by the current Series docstring which may not be properly documenting all of the allowed parameters.
If you don't mind looking a little further and seeing if we can simplify by moving more into the generic docstring would be greatly appreciated
| From our conversation yesterday and from the code, you would think that indeed the Upon further inspection, I did notice that they are indeed not disallowed. However, they do not seem to have the same behavior as the DataFrame counterparts: s = pd.Series([1,2,3,4]) df = pd.DataFrame([1,2,3,4]) s 0 1 1 2 2 3 3 4 dtype: int64 df 0 0 1 1 2 2 3 3 4 s.rename(mapper={0:'x'}, axis=0) 0 1 1 2 2 3 3 4 dtype: int64 df.rename(mapper={0:'x'}, axis=0) 0 x 1 1 2 2 3 3 4 s.rename(columns={"NONEXISTENT": 'a'}, errors="raise") 0 1 1 2 2 3 3 4 dtype: int64 df.rename(colums={"NONEXISTENT": 'a'}, errors="raise") Traceback (most recent call last): ... KeyError: "['NONEXISTENT'] not found in axis"From this, I do not think it is a good decision to generalize the documentation over these arguments, as they do not provide the same functionality for Series and DataFrame. Generalizing the Series documentation to essentially the one of DataFrame, leads to a misleading documentation for Series. |
| Can you take a look at reindex? I think that has a similar issue from an API perspective but handles the docstring substitution in a way that requires less duplication |
| @WillAyd I have now tried to use the
I have not found a way to effectively generalize the
Note that in the new commit, |
| @WillAyd Any ideas? |
| It sounds like there are potentially two issues here:
I would isolate number 2 first. For example Seems buggy. I think providing non-N�one I think once we agree on the correct behavior it'll be easier to document and determine whether or not the docstrings should be shared. |
| I see what you mean. I do not think I am the one to decide on the behavior of
Even None columns would not make sense though, in the context of Series, since Series does not have columns. To be honest, I would say to either force the API of Series.rename (i.e. throw on any non-documented argument) or make sure all possible arguments of NDFrame.rename make sense to Series as well, and provide behaviour. |
| Thanks for the input @TomAugspurger. Mentioned above but we have a similar handling of the reindex method. I think we can just mimic that substitution behavior for this PR. Agreed we can also change up some things with implementation but I don't think that's a blocker for this and could be subsequent PR |
| ('inplace', False), | ||
| ('level', None), | ||
| ('errors', 'ignore')]) | ||
| @Substitution(**dict(_shared_doc_kwargs, |
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.
I find this rather difficult to read - can you just make multiple Substitution decorations instead?
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.
I do think that that would be better; however, when splitting this into multiple Substitution decorations, the "top" Substitution is not substituted into the NDFrame.rename documentation.
For example; if we use the following Substitution decorations:
@Substitution(**_shared_doc_kwargs) @Substitution(altered='axes input function or functions', alternative_use='', axes_alt_types='') @Substitution(generic_documentation=NDFrame.rename.__doc__)I get the following error when building the documentation:
File "make.py", line 341, in <module> sys.exit(main()) File "make.py", line 329, in main globals()['pandas'] = importlib.import_module('pandas') File "/opt/anaconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 728, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/max/git/pandas/pandas/__init__.py", line 42, in <module> from pandas.core.api import * File "/home/max/git/pandas/pandas/core/api.py", line 40, in <module> from pandas.core.panel import Panel File "/home/max/git/pandas/pandas/core/panel.py", line 109, in <module> class Panel(NDFrame): File "/home/max/git/pandas/pandas/core/panel.py", line 1257, in Panel def rename(self, items=None, major_axis=None, minor_axis=None, **kwargs): File "/home/max/git/pandas/pandas/util/_decorators.py", line 259, in __call__ func.__doc__ = func.__doc__ and func.__doc__ % self.params KeyError: 'axes' This is what I encountered before, and I found out the pushed code fixes this issue.
| | ||
| @Substitution(**_shared_doc_kwargs) | ||
| @Appender(NDFrame.rename.__doc__) | ||
| @Substitution(**dict(_shared_doc_kwargs, |
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.
As mentioned don't worry about Panel - we'll be removing this anyway
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.
I understand; however, it still had to be made compliant with the NDFrame.generic substitutions in order for the documentation to be built (without errors).
| good idea, but closing as stale |
In this pull request, the documentation of the rename function of NDFrame is generalized over all its children using the function: DataFrame, Series and Panel. These three classes now use the Substitute decorator to add the generic part of the documentation to their own, using the same decorator to replace class specific parameters into the generic documentation.
I have tried my best to create as little changes to the documentation of each individual function. Although I have tried to get rid of all style errors, I do still receive some errors from the
validate_docstrings.py:Help is appreciated!