Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pandas/compat/numpy/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,24 @@ def validate_resampler_func(method, args, kwargs):
"{func}() instead".format(func=method)))
else:
raise TypeError("too many arguments passed in")


def validate_minmax_axis(axis):
"""
Ensure that the axis argument passed to min, max, argmin, or argmax is
zero or None, as otherwise it will be incorrectly ignored.

Parameters
----------
axis : int or None

Raises
------
ValueError
"""
ndim = 1 # hard-coded for Index
if axis is None:
return
if axis >= ndim or (axis < 0 and ndim + axis < 0):
raise ValueError("`axis` must be fewer than the number of "
"dimensions ({ndim})".format(ndim=ndim))
4 changes: 4 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ def min(self, axis=None, *args, **kwargs):
numpy.ndarray.min
"""
nv.validate_min(args, kwargs)
nv.validate_minmax_axis(axis)

try:
i8 = self.asi8
Expand Down Expand Up @@ -459,6 +460,7 @@ def argmin(self, axis=None, *args, **kwargs):
numpy.ndarray.argmin
"""
nv.validate_argmin(args, kwargs)
nv.validate_minmax_axis(axis)

i8 = self.asi8
if self.hasnans:
Expand All @@ -479,6 +481,7 @@ def max(self, axis=None, *args, **kwargs):
numpy.ndarray.max
"""
nv.validate_max(args, kwargs)
nv.validate_minmax_axis(axis)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these need to be inside these functions

the is so smelly to have to call 2 functions here
pls fix


try:
i8 = self.asi8
Expand Down Expand Up @@ -507,6 +510,7 @@ def argmax(self, axis=None, *args, **kwargs):
numpy.ndarray.argmax
"""
nv.validate_argmax(args, kwargs)
nv.validate_minmax_axis(axis)

i8 = self.asi8
if self.hasnans:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@

class DatetimeLike(Base):

def test_argmax_axis_invalid(self):
# GH#23081
rng = self.create_index()
with pytest.raises(ValueError):
rng.argmax(axis=1)
with pytest.raises(ValueError):
rng.argmin(axis=2)
with pytest.raises(ValueError):
rng.min(axis=-2)
with pytest.raises(ValueError):
rng.max(axis=-3)

def test_can_hold_identifiers(self):
idx = self.create_index()
key = idx[0]
Expand Down