Skip to content

Commit cc78690

Browse files
committed
fixup! API: Accept 'axis' keyword argument for reindex
1 parent ddd7878 commit cc78690

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

doc/source/whatsnew/v0.21.0.txt

+14-7
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ For example:
109109
# the following is now equivalent
110110
df.drop(columns=['B', 'C'])
111111

112-
``rename``, ``reindex`` now also accepts axis keyword
113-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
112+
.. _whatsnew_0210.enhancements.rename_reindex_axis:
113+
114+
``rename``, ``reindex`` now also accept axis keyword
115+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114116

115117
The :meth:`DataFrame.rename` and :meth:`DataFrame.reindex` methods have gained
116118
the ``axis`` keyword to specify the axis to target with the operation. This
@@ -121,18 +123,23 @@ Here's ``rename``:
121123

122124
.. ipython::
123125

124-
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
125-
df.rename(str.lower, axis='columns')
126-
df.rename(id, axis='index')
126+
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
127+
df.rename(str.lower, axis='columns')
128+
df.rename(id, axis='index')
127129

128130
And ``reindex``:
129131

130132
.. ipython::
131133

132-
df.reindex(['A', 'B', 'C'], axis='columns')
133-
df.reindex([0, 1, 3], axis='index')
134+
df.reindex(['A', 'B', 'C'], axis='columns')
135+
df.reindex([0, 1, 3], axis='index')
134136

135137
The ``.rename(index=id, columns=str.lower)`` style continues to work as before.
138+
139+
.. ipython::
140+
141+
df.rename(index=id, columns=str.lower)
142+
136143
We *highly* encourage using named arguments to avoid confusion when using either
137144
style.
138145

pandas/core/frame.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@
115115
versionadded_to_excel='',
116116
optional_mapper="""mapper : dict-like or function
117117
Applied to the axis specified by `axis`""",
118-
optional_labels="""labels : array-like
118+
optional_labels="""labels : array-like, optional
119119
New labels / index to conform the axis specified by 'axis' to.""",
120120
optional_axis="""axis : int or str, optional
121-
Axis to target. Can be either the axis name ('rows', 'columns')
121+
Axis to target. Can be either the axis name ('index', 'columns')
122122
or number (0, 1).""",
123123
)
124124

@@ -2776,7 +2776,7 @@ def reindexer(value):
27762776

27772777
def _validate_axis_style_args(self, arg, arg_name, index, columns,
27782778
axis, method_name):
2779-
if axis is not None:
2779+
if axis != 0:
27802780
# Using "axis" style, along with a positional arg
27812781
# Both index and columns should be None then
27822782
axis = self._get_axis_name(axis)
@@ -2940,7 +2940,7 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
29402940
broadcast_axis=broadcast_axis)
29412941

29422942
@Appender(_shared_docs['reindex'] % _shared_doc_kwargs)
2943-
def reindex(self, labels=None, index=None, columns=None, axis=None,
2943+
def reindex(self, labels=None, index=None, columns=None, axis=0,
29442944
**kwargs):
29452945
index, columns = self._validate_axis_style_args(labels, 'labels',
29462946
index, columns,
@@ -2957,7 +2957,7 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,
29572957
limit=limit, fill_value=fill_value)
29582958

29592959
@Appender(_shared_docs['rename'] % _shared_doc_kwargs)
2960-
def rename(self, mapper=None, index=None, columns=None, axis=None,
2960+
def rename(self, mapper=None, index=None, columns=None, axis=0,
29612961
**kwargs):
29622962
index, columns = self._validate_axis_style_args(mapper, 'mapper',
29632963
index, columns,

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2796,8 +2796,8 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
27962796
27972797
Note that ``.reindex`` can be called by specifying either
27982798
2799-
* One or both of ``index`` and ``columns``
2800-
* ``labels`` and ``axis``
2799+
* One or both of ``index`` and ``columns``
2800+
* ``labels`` and ``axis``
28012801
28022802
Create a dataframe with some fictional data.
28032803

pandas/tests/frame/test_alter_axes.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,8 @@ def test_rename_objects(self):
837837
assert 'FOO' in renamed
838838
assert 'foo' not in renamed
839839

840-
def test_rename_columns(self):
840+
def test_rename_axis_style(self):
841+
# https://github.com/pandas-dev/pandas/issues/12392
841842
df = pd.DataFrame({"A": [1, 2], "B": [1, 2]}, index=['X', 'Y'])
842843
expected = pd.DataFrame({"a": [1, 2], "b": [1, 2]}, index=['X', 'Y'])
843844

@@ -875,12 +876,14 @@ def test_rename_mapper_multi(self):
875876
assert_frame_equal(result, expected)
876877

877878
def test_rename_positional_named(self):
879+
# https://github.com/pandas-dev/pandas/issues/12392
878880
df = pd.DataFrame({"a": [1, 2], "b": [1, 2]}, index=['X', 'Y'])
879881
result = df.rename(str.lower, columns=str.upper)
880882
expected = pd.DataFrame({"A": [1, 2], "B": [1, 2]}, index=['x', 'y'])
881883
assert_frame_equal(result, expected)
882884

883-
def test_rename_raises(self):
885+
def test_rename_axis_style_raises(self):
886+
# https://github.com/pandas-dev/pandas/issues/12392
884887
df = pd.DataFrame({"A": [1, 2], "B": [1, 2]}, index=['0', '1'])
885888

886889
# Named target and axis
@@ -908,6 +911,7 @@ def test_rename_raises(self):
908911
df.rename(str.lower, str.lower, str.lower)
909912

910913
def test_drop_api_equivalence(self):
914+
# https://github.com/pandas-dev/pandas/issues/12392
911915
# equivalence of the labels/axis and index/columns API's
912916
df = DataFrame([[1, 2, 3], [3, 4, 5], [5, 6, 7]],
913917
index=['a', 'b', 'c'],

pandas/tests/frame/test_axis_select_reindex.py

+5
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ def test_reindex_dups(self):
448448
pytest.raises(ValueError, df.reindex, index=list(range(len(df))))
449449

450450
def test_reindex_axis_style(self):
451+
# https://github.com/pandas-dev/pandas/issues/12392
451452
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
452453
expected = pd.DataFrame({"A": [1, 2, np.nan], "B": [4, 5, np.nan]},
453454
index=[0, 1, 3])
@@ -461,6 +462,7 @@ def test_reindex_axis_style(self):
461462
assert_frame_equal(result, expected)
462463

463464
def test_reindex_positional_warns(self):
465+
# https://github.com/pandas-dev/pandas/issues/12392
464466
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
465467
expected = pd.DataFrame({"A": [1., 2], 'B': [4., 5],
466468
"C": [np.nan, np.nan]})
@@ -470,6 +472,7 @@ def test_reindex_positional_warns(self):
470472
assert_frame_equal(result, expected)
471473

472474
def test_reindex_axis_style_raises(self):
475+
# https://github.com/pandas-dev/pandas/issues/12392
473476
df = pd.DataFrame({"A": [1, 2, 3], 'B': [4, 5, 6]})
474477
with tm.assert_raises_regex(TypeError, ''):
475478
df.reindex([0, 1], ['A'], axis=1)
@@ -493,12 +496,14 @@ def test_reindex_axis_style_raises(self):
493496
df.reindex([0, 1], [0], ['A'])
494497

495498
def test_reindex_single_named_indexer(self):
499+
# https://github.com/pandas-dev/pandas/issues/12392
496500
df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]})
497501
result = df.reindex([0, 1], columns=['A'])
498502
expected = pd.DataFrame({"A": [1, 2]})
499503
assert_frame_equal(result, expected)
500504

501505
def test_reindex_api_equivalence(self):
506+
# https://github.com/pandas-dev/pandas/issues/12392
502507
# equivalence of the labels/axis and index/columns API's
503508
df = DataFrame([[1, 2, 3], [3, 4, 5], [5, 6, 7]],
504509
index=['a', 'b', 'c'],

0 commit comments

Comments
 (0)