-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
standardize signature for Index reductions, implement nanmean for datetime64 dtypes #24293
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
Changes from all commits
f38ffe1
04cf1f7
3efab79
a652439
ce760d3
2380af6
4157f0b
50642ae
0baedf3
6e0e69f
e2c301b
fce67ac
4b4979f
82b8cdf
ffe6ada
7f7693f
07c3102
6c93410
4620c56
4777b75
aa4028a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -973,10 +973,16 @@ def _ndarray_values(self): | |
def empty(self): | ||
return not self.size | ||
|
||
def max(self): | ||
def max(self, axis=None, skipna=True): | ||
""" | ||
Return the maximum value of the Index. | ||
|
||
Parameters | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also we have similar things in pandas/core/base, these should change as well. |
||
---------- | ||
axis : int, optional | ||
For compatibility with NumPy. Only 0 or None are allowed. | ||
skipna : bool, default True | ||
|
||
Returns | ||
------- | ||
scalar | ||
|
@@ -1004,22 +1010,36 @@ def max(self): | |
>>> idx.max() | ||
('b', 2) | ||
""" | ||
return nanops.nanmax(self.values) | ||
nv.validate_minmax_axis(axis) | ||
return nanops.nanmax(self._values, skipna=skipna) | ||
|
||
def argmax(self, axis=None): | ||
def argmax(self, axis=None, skipna=True): | ||
""" | ||
Return a ndarray of the maximum argument indexer. | ||
|
||
Parameters | ||
---------- | ||
axis : {None} | ||
Dummy argument for consistency with Series | ||
skipna : bool, default True | ||
|
||
See Also | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be consisten about the See Also, e.g. make sure in all, and add a refernce to the Series.min function as well (as appropriate) |
||
-------- | ||
numpy.ndarray.argmax | ||
""" | ||
return nanops.nanargmax(self.values) | ||
nv.validate_minmax_axis(axis) | ||
return nanops.nanargmax(self._values, skipna=skipna) | ||
|
||
def min(self): | ||
def min(self, axis=None, skipna=True): | ||
""" | ||
Return the minimum value of the Index. | ||
|
||
Parameters | ||
---------- | ||
axis : {None} | ||
Dummy argument for consistency with Series | ||
skipna : bool, default True | ||
|
||
Returns | ||
------- | ||
scalar | ||
|
@@ -1047,17 +1067,25 @@ def min(self): | |
>>> idx.min() | ||
('a', 1) | ||
""" | ||
return nanops.nanmin(self.values) | ||
nv.validate_minmax_axis(axis) | ||
return nanops.nanmin(self._values, skipna=skipna) | ||
|
||
def argmin(self, axis=None): | ||
def argmin(self, axis=None, skipna=True): | ||
""" | ||
Return a ndarray of the minimum argument indexer. | ||
|
||
Parameters | ||
---------- | ||
axis : {None} | ||
Dummy argument for consistency with Series | ||
skipna : bool, default True | ||
|
||
See Also | ||
-------- | ||
numpy.ndarray.argmin | ||
""" | ||
return nanops.nanargmin(self.values) | ||
nv.validate_minmax_axis(axis) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nanops.nanargmin(self._values, skipna=skipna) | ||
|
||
def tolist(self): | ||
""" | ||
|
@@ -1110,7 +1138,7 @@ def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None, | |
if func is None: | ||
raise TypeError("{klass} cannot perform the operation {op}".format( | ||
klass=self.__class__.__name__, op=name)) | ||
return func(**kwds) | ||
return func(skipna=skipna, **kwds) | ||
|
||
def _map_values(self, mapper, na_action=None): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -266,35 +266,41 @@ def tolist(self): | |
""" | ||
return list(self.astype(object)) | ||
|
||
def min(self, axis=None, *args, **kwargs): | ||
def min(self, axis=None, skipna=True, *args, **kwargs): | ||
""" | ||
Return the minimum value of the Index or minimum along | ||
an axis. | ||
|
||
See Also | ||
-------- | ||
numpy.ndarray.min | ||
Series.min : Return the minimum value in a Series. | ||
""" | ||
nv.validate_min(args, kwargs) | ||
nv.validate_minmax_axis(axis) | ||
|
||
try: | ||
i8 = self.asi8 | ||
if not len(self): | ||
return self._na_value | ||
|
||
i8 = self.asi8 | ||
try: | ||
# quick check | ||
if len(i8) and self.is_monotonic: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these quick checks make sense for Index as they are immutable, but may not make much sense here (but i guess can evaluate later) |
||
if i8[0] != iNaT: | ||
return self._box_func(i8[0]) | ||
|
||
if self.hasnans: | ||
min_stamp = self[~self._isnan].asi8.min() | ||
if skipna: | ||
min_stamp = self[~self._isnan].asi8.min() | ||
else: | ||
return self._na_value | ||
else: | ||
min_stamp = i8.min() | ||
return self._box_func(min_stamp) | ||
except ValueError: | ||
return self._na_value | ||
|
||
def argmin(self, axis=None, *args, **kwargs): | ||
def argmin(self, axis=None, skipna=True, *args, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can these inherit the doc-string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll take a look. Might also get rid of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm the docstrings here and for the corresponding methods in base.IndexOpsMixin are kind of clunky. This may merit a separate look, @datapythonista ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know well what's the class hierarchy and whether makes sense to inherit. But we'll have to add |
||
""" | ||
Returns the indices of the minimum values along an axis. | ||
|
||
|
@@ -311,41 +317,47 @@ def argmin(self, axis=None, *args, **kwargs): | |
i8 = self.asi8 | ||
if self.hasnans: | ||
mask = self._isnan | ||
if mask.all(): | ||
if mask.all() or not skipna: | ||
return -1 | ||
i8 = i8.copy() | ||
i8[mask] = np.iinfo('int64').max | ||
return i8.argmin() | ||
|
||
def max(self, axis=None, *args, **kwargs): | ||
def max(self, axis=None, skipna=True, *args, **kwargs): | ||
""" | ||
Return the maximum value of the Index or maximum along | ||
an axis. | ||
|
||
See Also | ||
-------- | ||
numpy.ndarray.max | ||
Series.max : Return the maximum value in a Series. | ||
""" | ||
nv.validate_max(args, kwargs) | ||
nv.validate_minmax_axis(axis) | ||
|
||
try: | ||
i8 = self.asi8 | ||
if not len(self): | ||
return self._na_value | ||
|
||
i8 = self.asi8 | ||
try: | ||
# quick check | ||
if len(i8) and self.is_monotonic: | ||
if i8[-1] != iNaT: | ||
return self._box_func(i8[-1]) | ||
|
||
if self.hasnans: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
max_stamp = self[~self._isnan].asi8.max() | ||
if skipna: | ||
max_stamp = self[~self._isnan].asi8.max() | ||
else: | ||
return self._na_value | ||
else: | ||
max_stamp = i8.max() | ||
return self._box_func(max_stamp) | ||
except ValueError: | ||
return self._na_value | ||
|
||
def argmax(self, axis=None, *args, **kwargs): | ||
def argmax(self, axis=None, skipna=True, *args, **kwargs): | ||
""" | ||
Returns the indices of the maximum values along an axis. | ||
|
||
|
@@ -362,7 +374,7 @@ def argmax(self, axis=None, *args, **kwargs): | |
i8 = self.asi8 | ||
if self.hasnans: | ||
mask = self._isnan | ||
if mask.all(): | ||
if mask.all() or not skipna: | ||
return -1 | ||
i8 = i8.copy() | ||
i8[mask] = 0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -297,12 +297,14 @@ def _minmax(self, meth): | |
|
||
return self._start + self._step * no_steps | ||
|
||
def min(self): | ||
def min(self, axis=None, skipna=True): | ||
"""The minimum value of the RangeIndex""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same. why don't these inherit the doc-string |
||
nv.validate_minmax_axis(axis) | ||
return self._minmax('min') | ||
|
||
def max(self): | ||
def max(self, axis=None, skipna=True): | ||
"""The maximum value of the RangeIndex""" | ||
nv.validate_minmax_axis(axis) | ||
return self._minmax('max') | ||
|
||
def argsort(self, *args, **kwargs): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,10 @@ | |
|
||
from pandas.core.dtypes.common import ( | ||
_is_unorderable_exception, ensure_platform_int, is_bool, | ||
is_categorical_dtype, is_datetime64tz_dtype, is_datetimelike, is_dict_like, | ||
is_extension_array_dtype, is_extension_type, is_hashable, is_integer, | ||
is_iterator, is_list_like, is_scalar, is_string_like, is_timedelta64_dtype) | ||
is_categorical_dtype, is_datetime64_dtype, is_datetime64tz_dtype, | ||
is_datetimelike, is_dict_like, is_extension_array_dtype, is_extension_type, | ||
is_hashable, is_integer, is_iterator, is_list_like, is_scalar, | ||
is_string_like, is_timedelta64_dtype) | ||
from pandas.core.dtypes.generic import ( | ||
ABCDataFrame, ABCDatetimeIndex, ABCSeries, ABCSparseArray, ABCSparseSeries) | ||
from pandas.core.dtypes.missing import ( | ||
|
@@ -3537,6 +3538,9 @@ def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None, | |
# dispatch to ExtensionArray interface | ||
if isinstance(delegate, ExtensionArray): | ||
return delegate._reduce(name, skipna=skipna, **kwds) | ||
elif is_datetime64_dtype(delegate): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be first? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it isn't clear to me that it would make a difference |
||
# use DatetimeIndex implementation to handle skipna correctly | ||
delegate = DatetimeIndex(delegate) | ||
|
||
# dispatch to numpy arrays | ||
elif isinstance(delegate, np.ndarray): | ||
|
Uh oh!
There was an error while loading. Please reload this page.