Skip to content

Commit ddd7878

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

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

pandas/core/frame.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
_values_from_object,
6666
_maybe_box_datetimelike,
6767
_dict_compat,
68+
_all_not_none,
6869
standardize_mapping)
6970
from pandas.core.generic import NDFrame, _shared_docs
7071
from pandas.core.index import (Index, MultiIndex, _ensure_index,
@@ -2792,15 +2793,15 @@ def _validate_axis_style_args(self, arg, arg_name, index, columns,
27922793
elif axis == 'columns':
27932794
columns = arg
27942795

2795-
elif all(x is not None for x in (arg, index, columns)):
2796+
elif _all_not_none(arg, index, columns):
27962797
msg = (
27972798
"Cannot specify all of '{arg_name}', 'index', and 'columns'. "
27982799
"Specify either {arg_name} and 'axis', or 'index' and "
27992800
"'columns'."
28002801
).format(arg_name=arg_name)
28012802
raise TypeError(msg)
28022803

2803-
elif axis is None and (arg is not None and index is not None):
2804+
elif _all_not_none(arg, index):
28042805
# This is the "ambiguous" case, so emit a warning
28052806
msg = (
28062807
"Interpreting call to '.{method_name}(a, b)' as "
@@ -2809,7 +2810,7 @@ def _validate_axis_style_args(self, arg, arg_name, index, columns,
28092810
).format(method_name=method_name)
28102811
warnings.warn(msg, stacklevel=3)
28112812
index, columns = arg, index
2812-
elif index is None and columns is None:
2813+
elif index is None:
28132814
index = arg
28142815
return index, columns
28152816

pandas/tests/frame/test_alter_axes.py

+6
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,12 @@ def test_rename_mapper_multi(self):
874874
expected = df.rename(index=str.upper)
875875
assert_frame_equal(result, expected)
876876

877+
def test_rename_positional_named(self):
878+
df = pd.DataFrame({"a": [1, 2], "b": [1, 2]}, index=['X', 'Y'])
879+
result = df.rename(str.lower, columns=str.upper)
880+
expected = pd.DataFrame({"A": [1, 2], "B": [1, 2]}, index=['x', 'y'])
881+
assert_frame_equal(result, expected)
882+
877883
def test_rename_raises(self):
878884
df = pd.DataFrame({"A": [1, 2], "B": [1, 2]}, index=['0', '1'])
879885

pandas/tests/frame/test_axis_select_reindex.py

+6
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,12 @@ def test_reindex_axis_style_raises(self):
492492
with tm.assert_raises_regex(TypeError, ''):
493493
df.reindex([0, 1], [0], ['A'])
494494

495+
def test_reindex_single_named_indexer(self):
496+
df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]})
497+
result = df.reindex([0, 1], columns=['A'])
498+
expected = pd.DataFrame({"A": [1, 2]})
499+
assert_frame_equal(result, expected)
500+
495501
def test_reindex_api_equivalence(self):
496502
# equivalence of the labels/axis and index/columns API's
497503
df = DataFrame([[1, 2, 3], [3, 4, 5], [5, 6, 7]],

0 commit comments

Comments
 (0)