|
7 | 7 | from pandas import (DatetimeIndex, Float64Index, Index, Int64Index,
|
8 | 8 | NaT, Period, PeriodIndex, Series, Timedelta,
|
9 | 9 | TimedeltaIndex, date_range, period_range,
|
10 |
| - timedelta_range) |
| 10 | + timedelta_range, notnull) |
11 | 11 |
|
12 | 12 | import pandas.util.testing as tm
|
13 | 13 |
|
@@ -449,6 +449,38 @@ def test_astype_raises(self):
|
449 | 449 | self.assertRaises(ValueError, idx.astype, 'datetime64')
|
450 | 450 | self.assertRaises(ValueError, idx.astype, 'datetime64[D]')
|
451 | 451 |
|
| 452 | + def test_where_other(self): |
| 453 | + |
| 454 | + # other is ndarray or Index |
| 455 | + i = pd.date_range('20130101', periods=3, tz='US/Eastern') |
| 456 | + |
| 457 | + for arr in [np.nan, pd.NaT]: |
| 458 | + result = i.where(notnull(i), other=np.nan) |
| 459 | + expected = i |
| 460 | + tm.assert_index_equal(result, expected) |
| 461 | + |
| 462 | + i2 = i.copy() |
| 463 | + i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist()) |
| 464 | + result = i.where(notnull(i2), i2) |
| 465 | + tm.assert_index_equal(result, i2) |
| 466 | + |
| 467 | + i2 = i.copy() |
| 468 | + i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist()) |
| 469 | + result = i.where(notnull(i2), i2.values) |
| 470 | + tm.assert_index_equal(result, i2) |
| 471 | + |
| 472 | + def test_where_tz(self): |
| 473 | + i = pd.date_range('20130101', periods=3, tz='US/Eastern') |
| 474 | + result = i.where(notnull(i)) |
| 475 | + expected = i |
| 476 | + tm.assert_index_equal(result, expected) |
| 477 | + |
| 478 | + i2 = i.copy() |
| 479 | + i2 = Index([pd.NaT, pd.NaT] + i[2:].tolist()) |
| 480 | + result = i.where(notnull(i2)) |
| 481 | + expected = i2 |
| 482 | + tm.assert_index_equal(result, expected) |
| 483 | + |
452 | 484 | def test_get_loc(self):
|
453 | 485 | idx = pd.date_range('2000-01-01', periods=3)
|
454 | 486 |
|
@@ -776,6 +808,39 @@ def test_get_loc(self):
|
776 | 808 | with tm.assertRaises(KeyError):
|
777 | 809 | idx.get_loc('2000-01-10', method='nearest', tolerance='1 day')
|
778 | 810 |
|
| 811 | + def test_where(self): |
| 812 | + i = self.create_index() |
| 813 | + result = i.where(notnull(i)) |
| 814 | + expected = i |
| 815 | + tm.assert_index_equal(result, expected) |
| 816 | + |
| 817 | + i2 = i.copy() |
| 818 | + i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(), |
| 819 | + freq='D') |
| 820 | + result = i.where(notnull(i2)) |
| 821 | + expected = i2 |
| 822 | + tm.assert_index_equal(result, expected) |
| 823 | + |
| 824 | + def test_where_other(self): |
| 825 | + |
| 826 | + i = self.create_index() |
| 827 | + for arr in [np.nan, pd.NaT]: |
| 828 | + result = i.where(notnull(i), other=np.nan) |
| 829 | + expected = i |
| 830 | + tm.assert_index_equal(result, expected) |
| 831 | + |
| 832 | + i2 = i.copy() |
| 833 | + i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(), |
| 834 | + freq='D') |
| 835 | + result = i.where(notnull(i2), i2) |
| 836 | + tm.assert_index_equal(result, i2) |
| 837 | + |
| 838 | + i2 = i.copy() |
| 839 | + i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(), |
| 840 | + freq='D') |
| 841 | + result = i.where(notnull(i2), i2.values) |
| 842 | + tm.assert_index_equal(result, i2) |
| 843 | + |
779 | 844 | def test_get_indexer(self):
|
780 | 845 | idx = pd.period_range('2000-01-01', periods=3).asfreq('H', how='start')
|
781 | 846 | tm.assert_numpy_array_equal(idx.get_indexer(idx), [0, 1, 2])
|
|
0 commit comments