|
1 | 1 | # pylint: disable=E1101,E1103,W0232 |
2 | 2 | from datetime import datetime, timedelta |
3 | | -import operator |
4 | 3 | import warnings |
5 | 4 |
|
6 | 5 | import numpy as np |
|
18 | 17 |
|
19 | 18 | from pandas import compat |
20 | 19 | from pandas.core import common as com |
21 | | -from pandas.core.accessor import PandasDelegate, delegate_names |
| 20 | +from pandas.core.accessor import delegate_names |
22 | 21 | from pandas.core.algorithms import unique1d |
23 | 22 | import pandas.core.arrays.datetimelike as dtl |
| 23 | +from pandas.core.arrays.datetimelike import DatelikeOps |
24 | 24 | from pandas.core.arrays.period import PeriodArray, period_array |
25 | 25 | from pandas.core.base import _shared_docs |
26 | 26 | import pandas.core.indexes.base as ibase |
27 | 27 | from pandas.core.indexes.base import _index_shared_docs, ensure_index |
28 | 28 | from pandas.core.indexes.datetimelike import ( |
29 | | - DatetimeIndexOpsMixin, wrap_arithmetic_op) |
| 29 | + DatetimeIndexOpsMixin, DatetimelikeDelegateMixin, wrap_arithmetic_op) |
30 | 30 | from pandas.core.indexes.datetimes import DatetimeIndex, Index, Int64Index |
31 | 31 | from pandas.core.missing import isna |
32 | 32 | from pandas.core.ops import get_op_result_name |
@@ -54,37 +54,26 @@ def _new_PeriodIndex(cls, **d): |
54 | 54 | return cls(values, **d) |
55 | 55 |
|
56 | 56 |
|
57 | | -class PeriodDelegateMixin(PandasDelegate): |
| 57 | +class PeriodDelegateMixin(DatetimelikeDelegateMixin): |
58 | 58 | """ |
59 | 59 | Delegate from PeriodIndex to PeriodArray. |
60 | 60 | """ |
61 | | - def _delegate_property_get(self, name, *args, **kwargs): |
62 | | - result = getattr(self._data, name) |
63 | | - box_ops = ( |
64 | | - set(PeriodArray._datetimelike_ops) - set(PeriodArray._bool_ops) |
65 | | - ) |
66 | | - if name in box_ops: |
67 | | - result = Index(result, name=self.name) |
68 | | - return result |
69 | | - |
70 | | - def _delegate_property_set(self, name, value, *args, **kwargs): |
71 | | - setattr(self._data, name, value) |
72 | | - |
73 | | - def _delegate_method(self, name, *args, **kwargs): |
74 | | - result = operator.methodcaller(name, *args, **kwargs)(self._data) |
75 | | - return Index(result, name=self.name) |
| 61 | + _delegate_class = PeriodArray |
| 62 | + _delegated_properties = PeriodArray._datetimelike_ops |
| 63 | + _delegated_methods = ( |
| 64 | + set(PeriodArray._datetimelike_methods) | {'_addsub_int_array'} |
| 65 | + ) |
| 66 | + _raw_properties = {'is_leap_year'} |
76 | 67 |
|
77 | 68 |
|
78 | 69 | @delegate_names(PeriodArray, |
79 | | - PeriodArray._datetimelike_ops + ['size', 'asi8', 'shape'], |
| 70 | + PeriodDelegateMixin._delegated_properties, |
80 | 71 | typ='property') |
81 | 72 | @delegate_names(PeriodArray, |
82 | | - [x for x in PeriodArray._datetimelike_methods |
83 | | - if x not in {"asfreq", "to_timestamp"}], |
84 | | - typ="method", |
85 | | - overwrite=True) |
86 | | -class PeriodIndex(DatetimeIndexOpsMixin, |
87 | | - Int64Index, PeriodDelegateMixin): |
| 73 | + PeriodDelegateMixin._delegated_methods, |
| 74 | + typ="method") |
| 75 | +class PeriodIndex(DatelikeOps, DatetimeIndexOpsMixin, Int64Index, |
| 76 | + PeriodDelegateMixin): |
88 | 77 | """ |
89 | 78 | Immutable ndarray holding ordinal values indicating regular periods in |
90 | 79 | time such as particular years, quarters, months, etc. |
@@ -349,21 +338,6 @@ def _maybe_box_as_values(self, values, **attribs): |
349 | 338 | freq = attribs['freq'] |
350 | 339 | return PeriodArray(values, freq=freq) |
351 | 340 |
|
352 | | - # ------------------------------------------------------------------------ |
353 | | - # Dispatch and maybe box. Not done in delegate_names because we box |
354 | | - # different from those (which use Index). |
355 | | - |
356 | | - def asfreq(self, freq=None, how='E'): |
357 | | - result = self._data.asfreq(freq=freq, how=how) |
358 | | - return self._simple_new(result, name=self.name) |
359 | | - |
360 | | - def to_timestamp(self, freq=None, how='start'): |
361 | | - from pandas import DatetimeIndex |
362 | | - result = self._data.to_timestamp(freq=freq, how=how) |
363 | | - return DatetimeIndex._simple_new(result.asi8, |
364 | | - name=self.name, |
365 | | - freq=result.freq) |
366 | | - |
367 | 341 | def _maybe_convert_timedelta(self, other): |
368 | 342 | """ |
369 | 343 | Convert timedelta-like input to an integer multiple of self.freq |
|
0 commit comments