Skip to content

Commit 1b10e14

Browse files
committed
fixup! API: Accept 'axis' keyword argument for reindex
1 parent 342445a commit 1b10e14

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

doc/source/basics.rst

+22-2
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,15 @@ following can be done:
12171217
This means that the reindexed Series's index is the same Python object as the
12181218
DataFrame's index.
12191219

1220+
.. versionadded:: 0.21.0
1221+
1222+
:meth:`DataFrame.reindex` also supports an "axis-style" calling convention,
1223+
where you specify a single ``labels`` argument and the ``axis`` it applies to.
1224+
1225+
.. ipython:: python
1226+
1227+
df.reindex(['c', 'f', 'b'], axis='index')
1228+
df.reindex(['three', 'two', 'one'], axis='columns')
12201229
12211230
.. seealso::
12221231

@@ -1413,12 +1422,23 @@ Series can also be used:
14131422

14141423
.. ipython:: python
14151424
1416-
df.rename(columns={'one' : 'foo', 'two' : 'bar'},
1417-
index={'a' : 'apple', 'b' : 'banana', 'd' : 'durian'})
1425+
df.rename(columns={'one': 'foo', 'two': 'bar'},
1426+
index={'a': 'apple', 'b': 'banana', 'd': 'durian'})
14181427
14191428
If the mapping doesn't include a column/index label, it isn't renamed. Also
14201429
extra labels in the mapping don't throw an error.
14211430

1431+
.. versionadded:: 0.21.0
1432+
1433+
:meth:`DataFrame.rename` also supports an "axis-style" calling convention, where
1434+
you specify a single ``mapper`` and the ``axis`` to apply that mapping to.
1435+
1436+
.. ipython:: python
1437+
1438+
df.rename({'one': 'foo', 'two': 'bar'}, axis='columns'})
1439+
df.rename({'a': 'apple', 'b': 'banana', 'd': 'durian'}, axis='columns'})
1440+
1441+
14221442
The :meth:`~DataFrame.rename` method also provides an ``inplace`` named
14231443
parameter that is by default ``False`` and copies the underlying data. Pass
14241444
``inplace=True`` to rename the data in place.

doc/source/whatsnew/v0.21.0.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,22 @@ library (:issue:`12392`).
121121

122122
Here's ``rename``:
123123

124-
.. ipython::
124+
.. ipython:: python
125125

126126
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
127127
df.rename(str.lower, axis='columns')
128128
df.rename(id, axis='index')
129129

130130
And ``reindex``:
131131

132-
.. ipython::
132+
.. ipython:: python
133133

134134
df.reindex(['A', 'B', 'C'], axis='columns')
135135
df.reindex([0, 1, 3], axis='index')
136136

137137
The ``.rename(index=id, columns=str.lower)`` style continues to work as before.
138138

139-
.. ipython::
139+
.. ipython:: python
140140

141141
df.rename(index=id, columns=str.lower)
142142

pandas/core/generic.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ def swaplevel(self, i=-2, j=-1, axis=0):
768768
769769
Examples
770770
--------
771+
771772
>>> s = pd.Series([1, 2, 3])
772773
>>> s
773774
0 1
@@ -790,16 +791,29 @@ def swaplevel(self, i=-2, j=-1, axis=0):
790791
5 3
791792
dtype: int64
792793
794+
Since ``DataFrame`` doesn't have a ``.name`` attribute,
795+
only mapping-type arguments are allowed.
796+
793797
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
794798
>>> df.rename(2)
795799
Traceback (most recent call last):
796800
...
797801
TypeError: 'int' object is not callable
802+
803+
``DataFrame.rename`` supports two calling conventions
804+
805+
* ``(index=index_mapper, columns=columns_mapper, ...)
806+
* ``(mapper, axis={'index', 'columns'}, ...)
807+
808+
We *highly* recommend using keyword arguments to clarify your
809+
intent.
810+
798811
>>> df.rename(index=str, columns={"A": "a", "B": "c"})
799812
a c
800813
0 1 4
801814
1 2 5
802815
2 3 6
816+
803817
>>> df.rename(index=str, columns={"A": "a", "C": "c"})
804818
a B
805819
0 1 4
@@ -819,6 +833,8 @@ def swaplevel(self, i=-2, j=-1, axis=0):
819833
0 1 4
820834
2 2 5
821835
4 3 6
836+
837+
See the :ref:`user guide <basics.rename>` for more.
822838
"""
823839

824840
@Appender(_shared_docs['rename'] % dict(axes='axes keywords for this'
@@ -904,6 +920,7 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False):
904920
905921
Examples
906922
--------
923+
907924
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
908925
>>> df.rename_axis("foo") # scalar, alters df.index.name
909926
A B
@@ -2794,10 +2811,13 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
27942811
Examples
27952812
--------
27962813
2797-
Note that ``.reindex`` can be called by specifying either
2814+
``DataFrame.reindex`` supports two calling conventions
27982815
2799-
* One or both of ``index`` and ``columns``
2800-
* ``labels`` and ``axis``
2816+
* ``(index=index_labels, columns=column_labels, ...)
2817+
* ``(labels, axis={'index', 'columns'}, ...)
2818+
2819+
We *highly* recommend using keyword arguments to clarify your
2820+
intent.
28012821
28022822
Create a dataframe with some fictional data.
28032823
@@ -2931,6 +2951,8 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
29312951
desired indexes. If you do want to fill in the ``NaN`` values present
29322952
in the original dataframe, use the ``fillna()`` method.
29332953
2954+
See the :ref:`user guide <basics.reindexing>` for more.
2955+
29342956
Returns
29352957
-------
29362958
reindexed : %(klass)s

0 commit comments

Comments
 (0)