Skip to content

Commit 86f1cee

Browse files
authored
CLN: clearer lookups for period accessors (pandas-dev#34442)
1 parent 9223d19 commit 86f1cee

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

pandas/_libs/tslibs/period.pyx

+18-22
Original file line numberDiff line numberDiff line change
@@ -1334,15 +1334,15 @@ cdef int pdays_in_month(int64_t ordinal, int freq):
13341334

13351335
@cython.wraparound(False)
13361336
@cython.boundscheck(False)
1337-
def get_period_field_arr(int code, const int64_t[:] arr, int freq):
1337+
def get_period_field_arr(str field, const int64_t[:] arr, int freq):
13381338
cdef:
13391339
Py_ssize_t i, sz
13401340
int64_t[:] out
13411341
accessor f
13421342

1343-
func = _get_accessor_func(code)
1343+
func = _get_accessor_func(field)
13441344
if func is NULL:
1345-
raise ValueError(f"Unrecognized period code: {code}")
1345+
raise ValueError(f"Unrecognized field name: {field}")
13461346

13471347
sz = len(arr)
13481348
out = np.empty(sz, dtype=np.int64)
@@ -1356,30 +1356,30 @@ def get_period_field_arr(int code, const int64_t[:] arr, int freq):
13561356
return out.base # .base to access underlying np.ndarray
13571357

13581358

1359-
cdef accessor _get_accessor_func(int code):
1360-
if code == 0:
1359+
cdef accessor _get_accessor_func(str field):
1360+
if field == "year":
13611361
return <accessor>pyear
1362-
elif code == 1:
1362+
elif field == "qyear":
13631363
return <accessor>pqyear
1364-
elif code == 2:
1364+
elif field == "quarter":
13651365
return <accessor>pquarter
1366-
elif code == 3:
1366+
elif field == "month":
13671367
return <accessor>pmonth
1368-
elif code == 4:
1368+
elif field == "day":
13691369
return <accessor>pday
1370-
elif code == 5:
1370+
elif field == "hour":
13711371
return <accessor>phour
1372-
elif code == 6:
1372+
elif field == "minute":
13731373
return <accessor>pminute
1374-
elif code == 7:
1374+
elif field == "second":
13751375
return <accessor>psecond
1376-
elif code == 8:
1376+
elif field == "week":
13771377
return <accessor>pweek
1378-
elif code == 9:
1378+
elif field == "day_of_year":
13791379
return <accessor>pday_of_year
1380-
elif code == 10:
1380+
elif field == "weekday":
13811381
return <accessor>pweekday
1382-
elif code == 11:
1382+
elif field == "days_in_month":
13831383
return <accessor>pdays_in_month
13841384
return NULL
13851385

@@ -1432,12 +1432,8 @@ def extract_freq(ndarray[object] values):
14321432
for i in range(n):
14331433
value = values[i]
14341434

1435-
try:
1436-
# now Timestamp / NaT has freq attr
1437-
if is_period_object(value):
1438-
return value.freq
1439-
except AttributeError:
1440-
pass
1435+
if is_period_object(value):
1436+
return value.freq
14411437

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

pandas/core/arrays/period.py

+6-17
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
from pandas.tseries.offsets import DateOffset
5050

5151

52-
def _field_accessor(name: str, alias: int, docstring=None):
52+
def _field_accessor(name: str, docstring=None):
5353
def f(self):
54-
base, mult = libfrequencies.get_freq_code(self.freq)
55-
result = get_period_field_arr(alias, self.asi8, base)
54+
base, _ = libfrequencies.get_freq_code(self.freq)
55+
result = get_period_field_arr(name, self.asi8, base)
5656
return result
5757

5858
f.__name__ = name
@@ -324,80 +324,69 @@ def __arrow_array__(self, type=None):
324324

325325
year = _field_accessor(
326326
"year",
327-
0,
328327
"""
329328
The year of the period.
330329
""",
331330
)
332331
month = _field_accessor(
333332
"month",
334-
3,
335333
"""
336334
The month as January=1, December=12.
337335
""",
338336
)
339337
day = _field_accessor(
340338
"day",
341-
4,
342339
"""
343340
The days of the period.
344341
""",
345342
)
346343
hour = _field_accessor(
347344
"hour",
348-
5,
349345
"""
350346
The hour of the period.
351347
""",
352348
)
353349
minute = _field_accessor(
354350
"minute",
355-
6,
356351
"""
357352
The minute of the period.
358353
""",
359354
)
360355
second = _field_accessor(
361356
"second",
362-
7,
363357
"""
364358
The second of the period.
365359
""",
366360
)
367361
weekofyear = _field_accessor(
368362
"week",
369-
8,
370363
"""
371364
The week ordinal of the year.
372365
""",
373366
)
374367
week = weekofyear
375368
dayofweek = _field_accessor(
376-
"dayofweek",
377-
10,
369+
"weekday",
378370
"""
379371
The day of the week with Monday=0, Sunday=6.
380372
""",
381373
)
382374
weekday = dayofweek
383375
dayofyear = day_of_year = _field_accessor(
384-
"dayofyear",
385-
9,
376+
"day_of_year",
386377
"""
387378
The ordinal day of the year.
388379
""",
389380
)
390381
quarter = _field_accessor(
391382
"quarter",
392-
2,
393383
"""
394384
The quarter of the date.
395385
""",
396386
)
397-
qyear = _field_accessor("qyear", 1)
387+
qyear = _field_accessor("qyear")
398388
days_in_month = _field_accessor(
399389
"days_in_month",
400-
11,
401390
"""
402391
The number of days in the month.
403392
""",

0 commit comments

Comments
 (0)