Skip to content

CLN: clearer lookups for period accessors #34442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 <accessor>pyear
elif code == 1:
elif field == "qyear":
return <accessor>pqyear
elif code == 2:
elif field == "quarter":
return <accessor>pquarter
elif code == 3:
elif field == "month":
return <accessor>pmonth
elif code == 4:
elif field == "day":
return <accessor>pday
elif code == 5:
elif field == "hour":
return <accessor>phour
elif code == 6:
elif field == "minute":
return <accessor>pminute
elif code == 7:
elif field == "second":
return <accessor>psecond
elif code == 8:
elif field == "week":
return <accessor>pweek
elif code == 9:
elif field == "day_of_year":
return <accessor>pday_of_year
elif code == 10:
elif field == "weekday":
return <accessor>pweekday
elif code == 11:
elif field == "days_in_month":
return <accessor>pdays_in_month
return NULL

Expand Down Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this edit i unrelated to everything else


raise ValueError('freq not specified and cannot be inferred')

Expand Down
23 changes: 6 additions & 17 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
""",
Expand Down