From c8ae5ad52416664ab09b3f45d11cd04f0de1e8ad Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Thu, 28 May 2020 14:19:35 -0700 Subject: [PATCH] CLN: clearer lookups for period accessors --- pandas/_libs/tslibs/period.pyx | 40 +++++++++++++++------------------- pandas/core/arrays/period.py | 23 +++++-------------- 2 files changed, 24 insertions(+), 39 deletions(-) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index ec6f8de159dae..e722ca6f5a56c 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -1331,15 +1331,15 @@ cdef int pdays_in_month(int64_t ordinal, int freq): @cython.wraparound(False) @cython.boundscheck(False) -def get_period_field_arr(int code, const int64_t[:] arr, int freq): +def get_period_field_arr(str field, const int64_t[:] arr, int freq): cdef: Py_ssize_t i, sz int64_t[:] out accessor f - func = _get_accessor_func(code) + func = _get_accessor_func(field) if func is NULL: - raise ValueError(f"Unrecognized period code: {code}") + raise ValueError(f"Unrecognized field name: {field}") sz = len(arr) out = np.empty(sz, dtype=np.int64) @@ -1353,30 +1353,30 @@ def get_period_field_arr(int code, const int64_t[:] arr, int freq): return out.base # .base to access underlying np.ndarray -cdef accessor _get_accessor_func(int code): - if code == 0: +cdef accessor _get_accessor_func(str field): + if field == "year": return pyear - elif code == 1: + elif field == "qyear": return pqyear - elif code == 2: + elif field == "quarter": return pquarter - elif code == 3: + elif field == "month": return pmonth - elif code == 4: + elif field == "day": return pday - elif code == 5: + elif field == "hour": return phour - elif code == 6: + elif field == "minute": return pminute - elif code == 7: + elif field == "second": return psecond - elif code == 8: + elif field == "week": return pweek - elif code == 9: + elif field == "day_of_year": return pday_of_year - elif code == 10: + elif field == "weekday": return pweekday - elif code == 11: + elif field == "days_in_month": return pdays_in_month return NULL @@ -1429,12 +1429,8 @@ def extract_freq(ndarray[object] values): for i in range(n): value = values[i] - try: - # now Timestamp / NaT has freq attr - if is_period_object(value): - return value.freq - except AttributeError: - pass + if is_period_object(value): + return value.freq raise ValueError('freq not specified and cannot be inferred') diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 8b2925b2c0827..4601e7fa5389e 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -49,10 +49,10 @@ from pandas.tseries.offsets import DateOffset -def _field_accessor(name: str, alias: int, docstring=None): +def _field_accessor(name: str, docstring=None): def f(self): - base, mult = libfrequencies.get_freq_code(self.freq) - result = get_period_field_arr(alias, self.asi8, base) + base, _ = libfrequencies.get_freq_code(self.freq) + result = get_period_field_arr(name, self.asi8, base) return result f.__name__ = name @@ -324,80 +324,69 @@ def __arrow_array__(self, type=None): year = _field_accessor( "year", - 0, """ The year of the period. """, ) month = _field_accessor( "month", - 3, """ The month as January=1, December=12. """, ) day = _field_accessor( "day", - 4, """ The days of the period. """, ) hour = _field_accessor( "hour", - 5, """ The hour of the period. """, ) minute = _field_accessor( "minute", - 6, """ The minute of the period. """, ) second = _field_accessor( "second", - 7, """ The second of the period. """, ) weekofyear = _field_accessor( "week", - 8, """ The week ordinal of the year. """, ) week = weekofyear dayofweek = _field_accessor( - "dayofweek", - 10, + "weekday", """ The day of the week with Monday=0, Sunday=6. """, ) weekday = dayofweek dayofyear = day_of_year = _field_accessor( - "dayofyear", - 9, + "day_of_year", """ The ordinal day of the year. """, ) quarter = _field_accessor( "quarter", - 2, """ The quarter of the date. """, ) - qyear = _field_accessor("qyear", 1) + qyear = _field_accessor("qyear") days_in_month = _field_accessor( "days_in_month", - 11, """ The number of days in the month. """,