Skip to content

BUG: PeriodIndex.unique results in Int64Index #7843

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
Jul 26, 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
2 changes: 2 additions & 0 deletions doc/source/v0.15.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ Bug Fixes

- Bug in ``is_superperiod`` and ``is_subperiod`` cannot handle higher frequencies than ``S`` (:issue:`7760`, :issue:`7772`, :issue:`7803`)

- Bug in ``PeriodIndex.unique`` returns int64 ``np.ndarray`` (:issue:`7540`)

- Bug in ``DataFrame.reset_index`` which has ``MultiIndex`` contains ``PeriodIndex`` or ``DatetimeIndex`` with tz raises ``ValueError`` (:issue:`7746`, :issue:`7793`)


Expand Down
14 changes: 14 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,17 @@ def __sub__(self, other):

def _add_delta(self, other):
return NotImplemented

def unique(self):
"""
Index.unique with handling for DatetimeIndex/PeriodIndex metadata

Returns
-------
result : DatetimeIndex or PeriodIndex
"""
from pandas.core.index import Int64Index
result = Int64Index.unique(self)
return self._simple_new(result, name=self.name, freq=self.freq,
tz=getattr(self, 'tz', None))

36 changes: 22 additions & 14 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,13 @@ def test_value_counts_unique_nunique(self):
expected_s = Series(range(10, 0, -1), index=values[::-1], dtype='int64')
tm.assert_series_equal(o.value_counts(), expected_s)

if isinstance(o, DatetimeIndex):
# DatetimeIndex.unique returns DatetimeIndex
self.assertTrue(o.unique().equals(klass(values)))
else:
self.assert_numpy_array_equal(o.unique(), values)
result = o.unique()
if isinstance(o, (DatetimeIndex, PeriodIndex)):
self.assertTrue(isinstance(result, o.__class__))
self.assertEqual(result.name, o.name)
self.assertEqual(result.freq, o.freq)

self.assert_numpy_array_equal(result, values)

self.assertEqual(o.nunique(), len(np.unique(o.values)))

Expand All @@ -263,17 +265,13 @@ def test_value_counts_unique_nunique(self):
klass = type(o)
values = o.values

if o.values.dtype == 'int64':
# skips int64 because it doesn't allow to include nan or None
continue

if ((isinstance(o, Int64Index) and not isinstance(o,
(DatetimeIndex, PeriodIndex)))):
# skips int64 because it doesn't allow to include nan or None
continue

# special assign to the numpy array
if o.values.dtype == 'datetime64[ns]':
if o.values.dtype == 'datetime64[ns]' or isinstance(o, PeriodIndex):
values[0:2] = pd.tslib.iNaT
else:
values[0:2] = null_obj
Expand All @@ -294,8 +292,8 @@ def test_value_counts_unique_nunique(self):
result = o.unique()
self.assert_numpy_array_equal(result[1:], values[2:])

if isinstance(o, DatetimeIndex):
self.assertTrue(result[0] is pd.NaT)
if isinstance(o, (DatetimeIndex, PeriodIndex)):
self.assertTrue(result.asi8[0] == pd.tslib.iNaT)
else:
self.assertTrue(pd.isnull(result[0]))

Expand Down Expand Up @@ -706,7 +704,7 @@ def test_sub_isub(self):
rng -= 1
tm.assert_index_equal(rng, expected)

def test_value_counts(self):
def test_value_counts_unique(self):
# GH 7735
for tz in [None, 'UTC', 'Asia/Tokyo', 'US/Eastern']:
idx = pd.date_range('2011-01-01 09:00', freq='H', periods=10)
Expand All @@ -717,6 +715,9 @@ def test_value_counts(self):
expected = Series(range(10, 0, -1), index=exp_idx, dtype='int64')
tm.assert_series_equal(idx.value_counts(), expected)

expected = pd.date_range('2011-01-01 09:00', freq='H', periods=10, tz=tz)
tm.assert_index_equal(idx.unique(), expected)

idx = DatetimeIndex(['2013-01-01 09:00', '2013-01-01 09:00', '2013-01-01 09:00',
'2013-01-01 08:00', '2013-01-01 08:00', pd.NaT], tz=tz)

Expand All @@ -728,6 +729,8 @@ def test_value_counts(self):
expected = Series([3, 2, 1], index=exp_idx)
tm.assert_series_equal(idx.value_counts(dropna=False), expected)

tm.assert_index_equal(idx.unique(), exp_idx)


class TestPeriodIndexOps(Ops):
_allowed = '_allow_period_index_ops'
Expand Down Expand Up @@ -987,7 +990,7 @@ def test_sub_isub(self):
rng -= 1
tm.assert_index_equal(rng, expected)

def test_value_counts(self):
def test_value_counts_unique(self):
# GH 7735
idx = pd.period_range('2011-01-01 09:00', freq='H', periods=10)
# create repeated values, 'n'th element is repeated by n+1 times
Expand All @@ -1000,6 +1003,9 @@ def test_value_counts(self):
expected = Series(range(10, 0, -1), index=exp_idx, dtype='int64')
tm.assert_series_equal(idx.value_counts(), expected)

expected = pd.period_range('2011-01-01 09:00', freq='H', periods=10)
tm.assert_index_equal(idx.unique(), expected)

idx = PeriodIndex(['2013-01-01 09:00', '2013-01-01 09:00', '2013-01-01 09:00',
'2013-01-01 08:00', '2013-01-01 08:00', pd.NaT], freq='H')

Expand All @@ -1011,6 +1017,8 @@ def test_value_counts(self):
expected = Series([3, 2, 1], index=exp_idx)
tm.assert_series_equal(idx.value_counts(dropna=False), expected)

tm.assert_index_equal(idx.unique(), exp_idx)


if __name__ == '__main__':
import nose
Expand Down
12 changes: 0 additions & 12 deletions pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,18 +848,6 @@ def take(self, indices, axis=0):
return self[maybe_slice]
return super(DatetimeIndex, self).take(indices, axis)

def unique(self):
"""
Index.unique with handling for DatetimeIndex metadata

Returns
-------
result : DatetimeIndex
"""
result = Int64Index.unique(self)
return DatetimeIndex._simple_new(result, tz=self.tz,
name=self.name)

def union(self, other):
"""
Specialized union for DatetimeIndex objects. If combine
Expand Down