-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
ENH: Allow rename_axis to specify index and columns arguments #20046
Conversation
"""Alter the name of the index or columns. | ||
def rename_axis(self, mapper=None, **kwargs): | ||
"""Alter the name of the index or name of index backing the | ||
columns. |
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.
Let's keep this to be one line.
pandas/core/generic.py
Outdated
@@ -749,6 +749,20 @@ def swaplevel(self, i=-2, j=-1, axis=0): | |||
# ---------------------------------------------------------------------- | |||
# Rename | |||
|
|||
# renamer function if passed a dict |
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 know you just moved the function, but let's take this opportunity to write a brief docstring.
Codecov Report
@@ Coverage Diff @@
## master #20046 +/- ##
=========================================
Coverage ? 92.18%
=========================================
Files ? 161
Lines ? 51184
Branches ? 0
=========================================
Hits ? 47185
Misses ? 3999
Partials ? 0
Continue to review full report at Codecov.
|
this implementation needs to follow the coding standards that are present in you have text saying that this has 2 calling conventions. This alone is making me -1 on this whole idea. |
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.
see comments
@jreback I'm not sure which coding standards that you are referring to in Secondly, with respect to the 2 calling conventions, that is true for |
@jreback See above. Awaiting your feedback or maybe @jorisvandenbossche |
@Dr-Irv if you can rebase can have a look |
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.
cc @jreback
@Dr-Irv can you rebase |
Hello @Dr-Irv! Thanks for updating the PR.
|
@jreback Merged with latest master. Current failure in travis was due to a network error. |
@TomAugspurger @jorisvandenbossche if you'd have a look. |
doc/source/whatsnew/v0.24.0.txt
Outdated
@@ -181,7 +181,8 @@ Other Enhancements | |||
- :func:`read_html` copies cell data across ``colspan`` and ``rowspan``, and it treats all-``th`` table rows as headers if ``header`` kwarg is not given and there is no ``thead`` (:issue:`17054`) | |||
- :meth:`Series.nlargest`, :meth:`Series.nsmallest`, :meth:`DataFrame.nlargest`, and :meth:`DataFrame.nsmallest` now accept the value ``"all"`` for the ``keep`` argument. This keeps all ties for the nth largest/smallest value (:issue:`16818`) | |||
- :class:`IntervalIndex` has gained the :meth:`~IntervalIndex.set_closed` method to change the existing ``closed`` value (:issue:`21670`) | |||
- :func:`~DataFrame.to_csv`, :func:`~Series.to_csv`, :func:`~DataFrame.to_json`, and :func:`~Series.to_json` now support ``compression='infer'`` to infer compression based on filename extension (:issue:`15008`). | |||
- :func:`~DataFrame.to_csv` and :func:`~DataFrame.to_json` now support ``compression='infer'`` to infer compression based on filename (:issue:`15008`) |
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.
actually can you move this note to a sub-section and show the previous and new behavior. I now this is not a super common method, but its a fairly meaty change.
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.
@jreback latest commit puts rename_axis()
change in a subsection with an example. Also, I had messed up the merge of whatsnew in previous commit, so that is now fixed.
can you rebase |
@jreback merged in latest master. All green |
doc/source/basics.rst
Outdated
@@ -1464,10 +1464,25 @@ for altering the ``Series.name`` attribute. | |||
|
|||
s.rename("scalar-name") | |||
|
|||
The Panel class has a related :meth:`~Panel.rename` class which can rename |
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.
this is not needed, we are not advertising Panel any longer
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.
OK, I deleted that, but Panel
appears a lot in that document!
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 know, but no need to add at this point
pandas/core/generic.py
Outdated
def _get_rename_function(self, mapper): | ||
""" | ||
Returns a function that will map names/labels, dependent if mapper | ||
is a dict, Series or just a function. |
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.
this doesn't need to be here (in the class). is it used in more than 1 place?
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.
Yes, it is used in both rename()
and rename_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.
right, but it doesn't need self right? can you move to pandas/core/common.py i think is ok for this
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 have moved it and removed leading underscore
|
||
Use either ``mapper`` and ``axis`` to | ||
specify the axis to target with ``mapper``, or ``index`` | ||
and/or ``columns``. |
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.
add a versionchanged
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.
done
@jreback Changes made, and all green. |
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.
Haven't looked at the tests yet, but a few comments first.
pandas/core/generic.py
Outdated
""" | ||
Alter the name of the index or columns. | ||
Alter the name of the index or name of index backing the columns. |
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 don't understand this change, is it a typo?
Oh, I think I saying "alter the name of the Index object that is the columns"?
If so, I think that index
needs to be capitalized. But I think the older version was fine.
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 will use "alter the name of the Index object that is the columns."
pandas/core/generic.py
Outdated
* ``(mapper, axis={'index', 'columns'}, ...)`` | ||
|
||
The first calling convention will only modify the names of | ||
the index and/or the names of the index backing the columns. |
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.
same: index -> Index, or just remove.
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.
changed to "names of the Index object that is the columns."
pandas/core/common.py
Outdated
Returns a function that will map names/labels, dependent if mapper | ||
is a dict, Series or just a function. | ||
""" | ||
if isinstance(mapper, (dict, ABCSeries)): |
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.
using compat.Mapping
instead of dict.
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.
done
Could also maybe use a short docnote in |
I've added a section on renaming to |
@TomAugspurger I did the changes. See remark at #20046 (comment) . I didn't make a change there. There was a failure in the CI due to a timeout on travis. Rather than resubmit, I figured you could review what's here and see if any other changes are needed. |
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.
lgtm. small comments. nice docs!
doc/source/basics.rst
Outdated
|
||
.. ipython:: python | ||
|
||
df = pd.DataFrame({'x': [1,2,3,4,5,6], 'y': [10,20,30,40,50,60]}, |
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.
can you make this pep-y, iow spaces after commas (you can also line up the x and y columns on separate lines)
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.
done
pandas/core/common.py
Outdated
@@ -454,3 +454,21 @@ def _pipe(obj, func, *args, **kwargs): | |||
return func(*args, **kwargs) | |||
else: | |||
return func(obj, *args, **kwargs) | |||
|
|||
|
|||
def get_rename_function(mapper): |
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.
can you make this private (e.g. _get_rename_function)
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.
done
mi = MultiIndex.from_product([['a', 'b', 'c'], [1, 2]], | ||
names=['ll', 'nn']) | ||
df = DataFrame({'x': [i for i in range(len(mi))], | ||
'y': [i * 10 for i in range(len(mi))]}, |
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.
can you sprinkle some comments to delineate the cases you are testing
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.
done
@jreback @TomAugspurger all green |
pandas/core/generic.py
Outdated
# and not a list or scalar, then call rename | ||
msg = ("Using 'rename_axis' to alter labels is deprecated. " | ||
"Use '.rename' instead") | ||
warnings.warn(msg, FutureWarning, stacklevel=2) |
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 think if you increase stack level by 1, you can revert your changes to the tests below. The stack level increased by 1 since you're using the decorator to fix the function signature.
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 tip. It worked.
@@ -522,22 +522,72 @@ def test_rename_axis_inplace(self, float_frame): | |||
def test_rename_axis_warns(self): | |||
# https://github.com/pandas-dev/pandas/issues/17833 | |||
df = DataFrame({"A": [1, 2], "B": [1, 2]}) | |||
with tm.assert_produces_warning(FutureWarning) as w: | |||
with tm.assert_produces_warning(FutureWarning, | |||
check_stacklevel=False) as w: |
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.
So these check_stacklevel
changes can hopefully all be reverted.
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.
done
@jreback @TomAugspurger Almost all Green, but the failure was due to something on Azure "before install". Let me know if there are more changes, and if there is some way to get that job to run again. |
@Dr-Irv ok lgtm. can you rebase, lots of isort things going on. ping on green. |
@jreback merged with latest and all green |
Thanks! |
…y_tests * repo_org/master: (52 commits) ENH: Allow rename_axis to specify index and columns arguments (pandas-dev#20046) STY: proposed isort settings [ci skip] [skip ci] [ciskip] [skipci] (pandas-dev#23366) MAINT: Remove extraneous test.parquet file CLN: Follow-up comments to pandas-devgh-23392 (pandas-dev#23401) BUG GH23282 calling min on series of NaT returns NaT (pandas-dev#23289) unpin openpyxl (pandas-dev#23361) REF: collect ops dispatch functions in one place, try to de-duplicate SparseDataFrame methods (pandas-dev#23060) CLN: Remove pandas.tools module (pandas-dev#23376) CLN: Remove some dtype methods from API (pandas-dev#23390) CLN: Cleanup toplevel namespace shims (pandas-dev#23386) DOC: fixup whatsnew note for GH21394 (pandas-dev#23355) Fix import format at pandas/tests/extension directory (pandas-dev#23365) DOC: Remove Series.sortlevel from api.rst (pandas-dev#23395) API: Disallow dtypes w/o frequency when casting (pandas-dev#23392) BUG/TST/REF: Datetimelike Arithmetic Methods (pandas-dev#23215) STYLE: lint add np.nan* funcs to cython_table (pandas-dev#22109) Run Isort on tests/util single PR (pandas-dev#23347) BUG: Fix date_range overflow (pandas-dev#23345) Run Isort on tests/arrays single PR (pandas-dev#23346) ...
test_rename_axis_mapper
forDataFrame
andSeries
git diff upstream/master -u -- "*.py" | flake8 --diff