Skip to content

Commit 3228823

Browse files
committed
PERF: speed up PeriodArray creation by querying dayfirst/yearfirst once
1 parent 210538e commit 3228823

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

pandas/_libs/tslibs/period.pyx

+12-3
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,10 @@ def extract_ordinals(object[:] values, freq):
14451445

14461446
freqstr = Period._maybe_convert_freq(freq).freqstr
14471447

1448+
from pandas.core.config import get_option
1449+
dayfirst = get_option('display.date_dayfirst')
1450+
yearfirst = get_option('display.date_yearfirst')
1451+
14481452
for i in range(n):
14491453
p = values[i]
14501454

@@ -1459,7 +1463,8 @@ def extract_ordinals(object[:] values, freq):
14591463
raise IncompatibleFrequency(msg)
14601464

14611465
except AttributeError:
1462-
p = Period(p, freq=freq)
1466+
p = Period(p, freq=freq, dayfirst=dayfirst,
1467+
yearfirst=yearfirst)
14631468
if p is NaT:
14641469
# input may contain NaT-like string
14651470
ordinals[i] = NPY_NAT
@@ -2397,11 +2402,14 @@ class Period(_Period):
23972402
hour : int, default 0
23982403
minute : int, default 0
23992404
second : int, default 0
2405+
dayfirst : bool, default None
2406+
yearfirst : bool, default None
24002407
"""
24012408

24022409
def __new__(cls, value=None, freq=None, ordinal=None,
24032410
year=None, month=None, quarter=None, day=None,
2404-
hour=None, minute=None, second=None):
2411+
hour=None, minute=None, second=None, dayfirst=None,
2412+
yearfirst=None):
24052413
# freq points to a tuple (base, mult); base is one of the defined
24062414
# periods such as A, Q, etc. Every five minutes would be, e.g.,
24072415
# ('T', 5) but may be passed in as a string like '5T'
@@ -2457,7 +2465,8 @@ class Period(_Period):
24572465
if util.is_integer_object(value):
24582466
value = str(value)
24592467
value = value.upper()
2460-
dt, _, reso = parse_time_string(value, freq)
2468+
dt, _, reso = parse_time_string(value, freq, dayfirst=dayfirst,
2469+
yearfirst=yearfirst)
24612470
if dt is NaT:
24622471
ordinal = NPY_NAT
24632472

0 commit comments

Comments
 (0)