|
27 | 27 | from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
|
28 | 28 | from pandas.core.dtypes.missing import isna
|
29 | 29 |
|
| 30 | +from pandas.core import nanops |
30 | 31 | from pandas.core.algorithms import checked_add_with_arr, take, unique1d
|
31 | 32 | import pandas.core.common as com
|
32 | 33 |
|
@@ -1381,6 +1382,71 @@ def _ensure_localized(self, arg, ambiguous='raise', nonexistent='raise',
|
1381 | 1382 | )
|
1382 | 1383 | return arg
|
1383 | 1384 |
|
| 1385 | + # -------------------------------------------------------------- |
| 1386 | + # Reductions |
| 1387 | + |
| 1388 | + def _reduce(self, name, axis=0, skipna=True, **kwargs): |
| 1389 | + op = getattr(self, name, None) |
| 1390 | + if op: |
| 1391 | + return op(axis=axis, skipna=skipna, **kwargs) |
| 1392 | + else: |
| 1393 | + raise TypeError("cannot perform {name} with type {dtype}" |
| 1394 | + .format(name=name, dtype=self.dtype)) |
| 1395 | + # TODO: use super(DatetimeLikeArrayMixin, self)._reduce |
| 1396 | + # after we subclass ExtensionArray |
| 1397 | + |
| 1398 | + def min(self, axis=None, skipna=True, *args, **kwargs): |
| 1399 | + """ |
| 1400 | + Return the minimum value of the Array or minimum along |
| 1401 | + an axis. |
| 1402 | +
|
| 1403 | + See Also |
| 1404 | + -------- |
| 1405 | + numpy.ndarray.min |
| 1406 | + Index.min : Return the minimum value in an Index. |
| 1407 | + Series.min : Return the minimum value in a Series. |
| 1408 | + """ |
| 1409 | + nv.validate_min(args, kwargs) |
| 1410 | + nv.validate_minmax_axis(axis) |
| 1411 | + |
| 1412 | + result = nanops.nanmin(self.asi8, skipna=skipna, mask=self.isna()) |
| 1413 | + if isna(result): |
| 1414 | + # Period._from_ordinal does not handle np.nan gracefully |
| 1415 | + return NaT |
| 1416 | + return self._box_func(result) |
| 1417 | + |
| 1418 | + def max(self, axis=None, skipna=True, *args, **kwargs): |
| 1419 | + """ |
| 1420 | + Return the maximum value of the Array or maximum along |
| 1421 | + an axis. |
| 1422 | +
|
| 1423 | + See Also |
| 1424 | + -------- |
| 1425 | + numpy.ndarray.max |
| 1426 | + Index.max : Return the maximum value in an Index. |
| 1427 | + Series.max : Return the maximum value in a Series. |
| 1428 | + """ |
| 1429 | + # TODO: skipna is broken with max. |
| 1430 | + # See https://github.com/pandas-dev/pandas/issues/24265 |
| 1431 | + nv.validate_max(args, kwargs) |
| 1432 | + nv.validate_minmax_axis(axis) |
| 1433 | + |
| 1434 | + mask = self.isna() |
| 1435 | + if skipna: |
| 1436 | + values = self[~mask].asi8 |
| 1437 | + elif mask.any(): |
| 1438 | + return NaT |
| 1439 | + else: |
| 1440 | + values = self.asi8 |
| 1441 | + |
| 1442 | + if not len(values): |
| 1443 | + # short-circut for empty max / min |
| 1444 | + return NaT |
| 1445 | + |
| 1446 | + result = nanops.nanmax(values, skipna=skipna) |
| 1447 | + # Don't have to worry about NA `result`, since no NA went in. |
| 1448 | + return self._box_func(result) |
| 1449 | + |
1384 | 1450 |
|
1385 | 1451 | DatetimeLikeArrayMixin._add_comparison_ops()
|
1386 | 1452 |
|
|
0 commit comments