|
10 | 10 | import pandas.tseries.frequencies as _freq_mod
|
11 | 11 |
|
12 | 12 | import pandas.core.common as com
|
13 |
| -from pandas.util import py3compat |
14 | 13 |
|
15 | 14 | from pandas._tseries import Timestamp
|
16 | 15 | import pandas._tseries as lib
|
@@ -470,6 +469,30 @@ def dt64arr_to_periodarr(data, freq):
|
470 | 469 |
|
471 | 470 | # --- Period index sketch
|
472 | 471 |
|
| 472 | + |
| 473 | +def _period_index_cmp(opname): |
| 474 | + """ |
| 475 | + Wrap comparison operations to convert datetime-like to datetime64 |
| 476 | + """ |
| 477 | + def wrapper(self, other): |
| 478 | + if isinstance(other, Period): |
| 479 | + func = getattr(self.values, opname) |
| 480 | + assert(other.freq == self.freq) |
| 481 | + result = func(other.ordinal) |
| 482 | + elif isinstance(other, PeriodIndex): |
| 483 | + assert(other.freq == self.freq) |
| 484 | + return getattr(self.values, opname)(other.values) |
| 485 | + else: |
| 486 | + other = Period(other, freq=self.freq) |
| 487 | + func = getattr(self.values, opname) |
| 488 | + result = func(other.ordinal) |
| 489 | + try: |
| 490 | + return result.view(np.ndarray) |
| 491 | + except: |
| 492 | + return result |
| 493 | + return wrapper |
| 494 | + |
| 495 | + |
473 | 496 | class PeriodIndex(Int64Index):
|
474 | 497 | """
|
475 | 498 | Immutable ndarray holding ordinal values indicating regular periods in
|
@@ -507,6 +530,13 @@ class PeriodIndex(Int64Index):
|
507 | 530 | """
|
508 | 531 | _box_scalars = True
|
509 | 532 |
|
| 533 | + __eq__ = _period_index_cmp('__eq__') |
| 534 | + __ne__ = _period_index_cmp('__ne__') |
| 535 | + __lt__ = _period_index_cmp('__lt__') |
| 536 | + __gt__ = _period_index_cmp('__gt__') |
| 537 | + __le__ = _period_index_cmp('__le__') |
| 538 | + __ge__ = _period_index_cmp('__ge__') |
| 539 | + |
510 | 540 | def __new__(cls, data=None,
|
511 | 541 | freq=None, start=None, end=None, periods=None,
|
512 | 542 | copy=False, name=None):
|
|
0 commit comments