Skip to content

BUG: Index.min and max doesnt handle nan and NaT properly #7279

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 1 commit into from
May 30, 2014
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
1 change: 1 addition & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ There are no experimental changes in 0.14.1
Bug Fixes
~~~~~~~~~

- Bug in ``Index.min`` and ``max`` doesn't handle ``nan`` and ``NaT`` properly (:issue:`7261`)
6 changes: 4 additions & 2 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,13 @@ def _wrap_access_object(self, obj):

def max(self):
""" The maximum value of the object """
return self.values.max()
import pandas.core.nanops
return pandas.core.nanops.nanmax(self.values)

def min(self):
""" The minimum value of the object """
return self.values.min()
import pandas.core.nanops
return pandas.core.nanops.nanmin(self.values)

def value_counts(self, normalize=False, sort=True, ascending=False,
bins=None):
Expand Down
35 changes: 34 additions & 1 deletion pandas/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from datetime import timedelta
from datetime import datetime, timedelta
import numpy as np
import pandas.compat as compat
import pandas as pd
Expand Down Expand Up @@ -210,6 +210,39 @@ def test_ops(self):
expected = expected.astype('M8[ns]').astype('int64')
self.assertEqual(result.value, expected)

def test_nanops(self):
# GH 7261
for op in ['max','min']:
for klass in [Index, Series]:

obj = klass([np.nan, 2.0])
self.assertEqual(getattr(obj, op)(), 2.0)

obj = klass([np.nan])
self.assertTrue(pd.isnull(getattr(obj, op)()))

obj = klass([])
self.assertTrue(pd.isnull(getattr(obj, op)()))

obj = klass([pd.NaT, datetime(2011, 11, 1)])
# check DatetimeIndex monotonic path
self.assertEqual(getattr(obj, op)(), datetime(2011, 11, 1))

obj = klass([pd.NaT, datetime(2011, 11, 1), pd.NaT])
# check DatetimeIndex non-monotonic path
self.assertEqual(getattr(obj, op)(), datetime(2011, 11, 1))

# explicitly create DatetimeIndex
obj = DatetimeIndex([])
self.assertTrue(pd.isnull(getattr(obj, op)()))

obj = DatetimeIndex([pd.NaT])
self.assertTrue(pd.isnull(getattr(obj, op)()))

obj = DatetimeIndex([pd.NaT, pd.NaT, pd.NaT])
self.assertTrue(pd.isnull(getattr(obj, op)()))


def test_value_counts_unique_nunique(self):
for o in self.objs:
klass = type(o)
Expand Down
20 changes: 14 additions & 6 deletions pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1758,20 +1758,28 @@ def min(self, axis=None):
"""
Overridden ndarray.min to return a Timestamp
"""
if self.is_monotonic:
return self[0]
mask = self.asi8 == tslib.iNaT
masked = self[~mask]
if len(masked) == 0:
return tslib.NaT
elif self.is_monotonic:
return masked[0]
else:
min_stamp = self.asi8.min()
min_stamp = masked.asi8.min()
return Timestamp(min_stamp, tz=self.tz)

def max(self, axis=None):
"""
Overridden ndarray.max to return a Timestamp
"""
if self.is_monotonic:
return self[-1]
mask = self.asi8 == tslib.iNaT
masked = self[~mask]
if len(masked) == 0:
return tslib.NaT
elif self.is_monotonic:
return masked[-1]
else:
max_stamp = self.asi8.max()
max_stamp = masked.asi8.max()
return Timestamp(max_stamp, tz=self.tz)

def to_julian_date(self):
Expand Down