Skip to content

validate min/max axis #23206

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

Merged
merged 3 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions pandas/compat/numpy/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,22 @@ 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