|
20 | 20 | import pandas.core.dtypes.concat as _concat |
21 | 21 | from pandas.core.dtypes.missing import isna |
22 | 22 |
|
| 23 | +from pandas.core.accessor import delegate_names |
23 | 24 | from pandas.core.arrays.datetimes import ( |
24 | 25 | DatetimeArrayMixin as DatetimeArray, _to_m8) |
25 | 26 | from pandas.core.base import _shared_docs |
26 | 27 | import pandas.core.common as com |
27 | 28 | from pandas.core.indexes.base import Index, _index_shared_docs |
28 | 29 | from pandas.core.indexes.datetimelike import ( |
29 | | - DatetimeIndexOpsMixin, wrap_array_method, wrap_field_accessor) |
| 30 | + DatetimeIndexOpsMixin, DatetimelikeDelegateMixin) |
30 | 31 | from pandas.core.indexes.numeric import Int64Index |
31 | 32 | from pandas.core.ops import get_op_result_name |
32 | 33 | import pandas.core.tools.datetimes as tools |
@@ -61,7 +62,54 @@ def _new_DatetimeIndex(cls, d): |
61 | 62 | return result |
62 | 63 |
|
63 | 64 |
|
64 | | -class DatetimeIndex(DatetimeArray, DatetimeIndexOpsMixin, Int64Index): |
| 65 | +class DatetimeDelegateMixin(DatetimelikeDelegateMixin): |
| 66 | + # Most attrs are dispatched via datetimelike_{ops,methods} |
| 67 | + # Some are "raw" methods, the result is not not re-boxed in an Index |
| 68 | + # We also have a few "extra" attrs, which may or may not be raw, |
| 69 | + # which we we dont' want to expose in the .dt accessor. |
| 70 | + _extra_methods = [ |
| 71 | + 'to_period', |
| 72 | + 'to_perioddelta', |
| 73 | + 'to_julian_date', |
| 74 | + ] |
| 75 | + _extra_raw_methods = [ |
| 76 | + 'to_pydatetime', |
| 77 | + '_local_timestamps', |
| 78 | + '_has_same_tz', |
| 79 | + ] |
| 80 | + _extra_raw_properties = [ |
| 81 | + '_box_func', |
| 82 | + 'tz', 'tzinfo', |
| 83 | + ] |
| 84 | + _delegated_properties = ( |
| 85 | + DatetimeArray._datetimelike_ops + _extra_raw_properties |
| 86 | + ) |
| 87 | + _delegated_methods = ( |
| 88 | + DatetimeArray._datetimelike_methods + _extra_methods + |
| 89 | + _extra_raw_methods |
| 90 | + ) |
| 91 | + _raw_properties = { |
| 92 | + 'date', |
| 93 | + 'time', |
| 94 | + 'timetz', |
| 95 | + } | set(DatetimeArray._bool_ops) | set(_extra_raw_properties) |
| 96 | + _raw_methods = set(_extra_raw_methods) |
| 97 | + _delegate_class = DatetimeArray |
| 98 | + |
| 99 | + |
| 100 | +@delegate_names(DatetimeArray, ["to_period", "tz_localize", "tz_convert", |
| 101 | + "day_name", "month_name"], |
| 102 | + typ="method", overwrite=True) |
| 103 | +@delegate_names(DatetimeArray, |
| 104 | + DatetimeArray._field_ops, typ="property", overwrite=True) |
| 105 | +@delegate_names(DatetimeArray, |
| 106 | + DatetimeDelegateMixin._delegated_properties, |
| 107 | + typ="property") |
| 108 | +@delegate_names(DatetimeArray, |
| 109 | + DatetimeDelegateMixin._delegated_methods, |
| 110 | + typ="method", overwrite=False) |
| 111 | +class DatetimeIndex(DatetimeArray, DatetimeIndexOpsMixin, Int64Index, |
| 112 | + DatetimeDelegateMixin): |
65 | 113 | """ |
66 | 114 | Immutable ndarray of datetime64 data, represented internally as int64, and |
67 | 115 | which can be boxed to Timestamp objects that are subclasses of datetime and |
@@ -1094,44 +1142,6 @@ def _eadata(self): |
1094 | 1142 | is_normalized = cache_readonly(DatetimeArray.is_normalized.fget) |
1095 | 1143 | _resolution = cache_readonly(DatetimeArray._resolution.fget) |
1096 | 1144 |
|
1097 | | - year = wrap_field_accessor(DatetimeArray.year) |
1098 | | - month = wrap_field_accessor(DatetimeArray.month) |
1099 | | - day = wrap_field_accessor(DatetimeArray.day) |
1100 | | - hour = wrap_field_accessor(DatetimeArray.hour) |
1101 | | - minute = wrap_field_accessor(DatetimeArray.minute) |
1102 | | - second = wrap_field_accessor(DatetimeArray.second) |
1103 | | - microsecond = wrap_field_accessor(DatetimeArray.microsecond) |
1104 | | - nanosecond = wrap_field_accessor(DatetimeArray.nanosecond) |
1105 | | - weekofyear = wrap_field_accessor(DatetimeArray.weekofyear) |
1106 | | - week = weekofyear |
1107 | | - dayofweek = wrap_field_accessor(DatetimeArray.dayofweek) |
1108 | | - weekday = dayofweek |
1109 | | - |
1110 | | - weekday_name = wrap_field_accessor(DatetimeArray.weekday_name) |
1111 | | - |
1112 | | - dayofyear = wrap_field_accessor(DatetimeArray.dayofyear) |
1113 | | - quarter = wrap_field_accessor(DatetimeArray.quarter) |
1114 | | - days_in_month = wrap_field_accessor(DatetimeArray.days_in_month) |
1115 | | - daysinmonth = days_in_month |
1116 | | - is_month_start = wrap_field_accessor(DatetimeArray.is_month_start) |
1117 | | - is_month_end = wrap_field_accessor(DatetimeArray.is_month_end) |
1118 | | - is_quarter_start = wrap_field_accessor(DatetimeArray.is_quarter_start) |
1119 | | - is_quarter_end = wrap_field_accessor(DatetimeArray.is_quarter_end) |
1120 | | - is_year_start = wrap_field_accessor(DatetimeArray.is_year_start) |
1121 | | - is_year_end = wrap_field_accessor(DatetimeArray.is_year_end) |
1122 | | - is_leap_year = wrap_field_accessor(DatetimeArray.is_leap_year) |
1123 | | - |
1124 | | - tz_localize = wrap_array_method(DatetimeArray.tz_localize, True) |
1125 | | - tz_convert = wrap_array_method(DatetimeArray.tz_convert, True) |
1126 | | - to_perioddelta = wrap_array_method(DatetimeArray.to_perioddelta, |
1127 | | - False) |
1128 | | - to_period = wrap_array_method(DatetimeArray.to_period, True) |
1129 | | - normalize = wrap_array_method(DatetimeArray.normalize, True) |
1130 | | - to_julian_date = wrap_array_method(DatetimeArray.to_julian_date, |
1131 | | - False) |
1132 | | - month_name = wrap_array_method(DatetimeArray.month_name, True) |
1133 | | - day_name = wrap_array_method(DatetimeArray.day_name, True) |
1134 | | - |
1135 | 1145 | # -------------------------------------------------------------------- |
1136 | 1146 |
|
1137 | 1147 | @Substitution(klass='DatetimeIndex') |
|
0 commit comments