|
8 | 8 | from pandas.util.testing import assertRaisesRegexp, assert_isinstance
|
9 | 9 | from pandas import Series, Index, Int64Index, DatetimeIndex, PeriodIndex
|
10 | 10 | from pandas import _np_version_under1p7
|
| 11 | +import pandas.tslib as tslib |
11 | 12 | import nose
|
12 | 13 |
|
13 | 14 | import pandas.util.testing as tm
|
@@ -202,7 +203,10 @@ def test_ops(self):
|
202 | 203 | for op in ['max','min']:
|
203 | 204 | for o in self.objs:
|
204 | 205 | result = getattr(o,op)()
|
205 |
| - expected = getattr(o.values,op)() |
| 206 | + if not isinstance(o, PeriodIndex): |
| 207 | + expected = getattr(o.values, op)() |
| 208 | + else: |
| 209 | + expected = pd.Period(ordinal=getattr(o.values, op)(), freq=o.freq) |
206 | 210 | try:
|
207 | 211 | self.assertEqual(result, expected)
|
208 | 212 | except ValueError:
|
@@ -232,17 +236,6 @@ def test_nanops(self):
|
232 | 236 | # check DatetimeIndex non-monotonic path
|
233 | 237 | self.assertEqual(getattr(obj, op)(), datetime(2011, 11, 1))
|
234 | 238 |
|
235 |
| - # explicitly create DatetimeIndex |
236 |
| - obj = DatetimeIndex([]) |
237 |
| - self.assertTrue(pd.isnull(getattr(obj, op)())) |
238 |
| - |
239 |
| - obj = DatetimeIndex([pd.NaT]) |
240 |
| - self.assertTrue(pd.isnull(getattr(obj, op)())) |
241 |
| - |
242 |
| - obj = DatetimeIndex([pd.NaT, pd.NaT, pd.NaT]) |
243 |
| - self.assertTrue(pd.isnull(getattr(obj, op)())) |
244 |
| - |
245 |
| - |
246 | 239 | def test_value_counts_unique_nunique(self):
|
247 | 240 | for o in self.objs:
|
248 | 241 | klass = type(o)
|
@@ -552,6 +545,33 @@ def test_asobject_tolist(self):
|
552 | 545 | self.assertEqual(result.name, expected.name)
|
553 | 546 | self.assertEqual(idx.tolist(), expected_list)
|
554 | 547 |
|
| 548 | + def test_minmax(self): |
| 549 | + for tz in [None, 'Asia/Tokyo', 'US/Eastern']: |
| 550 | + # monotonic |
| 551 | + idx1 = pd.DatetimeIndex([pd.NaT, '2011-01-01', '2011-01-02', |
| 552 | + '2011-01-03'], tz=tz) |
| 553 | + self.assertTrue(idx1.is_monotonic) |
| 554 | + |
| 555 | + # non-monotonic |
| 556 | + idx2 = pd.DatetimeIndex(['2011-01-01', pd.NaT, '2011-01-03', |
| 557 | + '2011-01-02', pd.NaT], tz=tz) |
| 558 | + self.assertFalse(idx2.is_monotonic) |
| 559 | + |
| 560 | + for idx in [idx1, idx2]: |
| 561 | + self.assertEqual(idx.min(), pd.Timestamp('2011-01-01', tz=tz)) |
| 562 | + self.assertEqual(idx.max(), pd.Timestamp('2011-01-03', tz=tz)) |
| 563 | + |
| 564 | + for op in ['min', 'max']: |
| 565 | + # Return NaT |
| 566 | + obj = DatetimeIndex([]) |
| 567 | + self.assertTrue(pd.isnull(getattr(obj, op)())) |
| 568 | + |
| 569 | + obj = DatetimeIndex([pd.NaT]) |
| 570 | + self.assertTrue(pd.isnull(getattr(obj, op)())) |
| 571 | + |
| 572 | + obj = DatetimeIndex([pd.NaT, pd.NaT, pd.NaT]) |
| 573 | + self.assertTrue(pd.isnull(getattr(obj, op)())) |
| 574 | + |
555 | 575 |
|
556 | 576 | class TestPeriodIndexOps(Ops):
|
557 | 577 | _allowed = '_allow_period_index_ops'
|
@@ -597,6 +617,39 @@ def test_asobject_tolist(self):
|
597 | 617 | self.assertTrue(result_list[2].ordinal, pd.tslib.iNaT)
|
598 | 618 | self.assertTrue(result_list[2].freq, 'D')
|
599 | 619 |
|
| 620 | + def test_minmax(self): |
| 621 | + |
| 622 | + # monotonic |
| 623 | + idx1 = pd.PeriodIndex([pd.NaT, '2011-01-01', '2011-01-02', |
| 624 | + '2011-01-03'], freq='D') |
| 625 | + self.assertTrue(idx1.is_monotonic) |
| 626 | + |
| 627 | + # non-monotonic |
| 628 | + idx2 = pd.PeriodIndex(['2011-01-01', pd.NaT, '2011-01-03', |
| 629 | + '2011-01-02', pd.NaT], freq='D') |
| 630 | + self.assertFalse(idx2.is_monotonic) |
| 631 | + |
| 632 | + for idx in [idx1, idx2]: |
| 633 | + self.assertEqual(idx.min(), pd.Period('2011-01-01', freq='D')) |
| 634 | + self.assertEqual(idx.max(), pd.Period('2011-01-03', freq='D')) |
| 635 | + |
| 636 | + for op in ['min', 'max']: |
| 637 | + # Return NaT |
| 638 | + obj = PeriodIndex([], freq='M') |
| 639 | + result = getattr(obj, op)() |
| 640 | + self.assertEqual(result.ordinal, tslib.iNaT) |
| 641 | + self.assertEqual(result.freq, 'M') |
| 642 | + |
| 643 | + obj = PeriodIndex([pd.NaT], freq='M') |
| 644 | + result = getattr(obj, op)() |
| 645 | + self.assertEqual(result.ordinal, tslib.iNaT) |
| 646 | + self.assertEqual(result.freq, 'M') |
| 647 | + |
| 648 | + obj = PeriodIndex([pd.NaT, pd.NaT, pd.NaT], freq='M') |
| 649 | + result = getattr(obj, op)() |
| 650 | + self.assertEqual(result.ordinal, tslib.iNaT) |
| 651 | + self.assertEqual(result.freq, 'M') |
| 652 | + |
600 | 653 |
|
601 | 654 | if __name__ == '__main__':
|
602 | 655 | import nose
|
|
0 commit comments