Skip to content

Commit 33e86bf

Browse files
authored
REF: simplify DTI._parse_string_to_bounds (#31519)
1 parent 42065cd commit 33e86bf

File tree

1 file changed

+8
-55
lines changed

1 file changed

+8
-55
lines changed

pandas/core/indexes/datetimes.py

+8-55
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,8 @@
55

66
import numpy as np
77

8-
from pandas._libs import (
9-
NaT,
10-
Timedelta,
11-
Timestamp,
12-
index as libindex,
13-
lib,
14-
tslib as libts,
15-
)
16-
from pandas._libs.tslibs import ccalendar, fields, parsing, timezones
8+
from pandas._libs import NaT, Period, Timestamp, index as libindex, lib, tslib as libts
9+
from pandas._libs.tslibs import fields, parsing, timezones
1710
from pandas.util._decorators import cache_readonly
1811

1912
from pandas.core.dtypes.common import _NS_DTYPE, is_float, is_integer, is_scalar
@@ -465,15 +458,14 @@ def _parsed_string_to_bounds(self, reso: str, parsed: datetime):
465458
466459
Parameters
467460
----------
468-
reso : Resolution
461+
reso : str
469462
Resolution provided by parsed string.
470463
parsed : datetime
471464
Datetime from parsed string.
472465
473466
Returns
474467
-------
475468
lower, upper: pd.Timestamp
476-
477469
"""
478470
valid_resos = {
479471
"year",
@@ -489,50 +481,11 @@ def _parsed_string_to_bounds(self, reso: str, parsed: datetime):
489481
}
490482
if reso not in valid_resos:
491483
raise KeyError
492-
if reso == "year":
493-
start = Timestamp(parsed.year, 1, 1)
494-
end = Timestamp(parsed.year + 1, 1, 1) - Timedelta(nanoseconds=1)
495-
elif reso == "month":
496-
d = ccalendar.get_days_in_month(parsed.year, parsed.month)
497-
start = Timestamp(parsed.year, parsed.month, 1)
498-
end = start + Timedelta(days=d, nanoseconds=-1)
499-
elif reso == "quarter":
500-
qe = (((parsed.month - 1) + 2) % 12) + 1 # two months ahead
501-
d = ccalendar.get_days_in_month(parsed.year, qe) # at end of month
502-
start = Timestamp(parsed.year, parsed.month, 1)
503-
end = Timestamp(parsed.year, qe, 1) + Timedelta(days=d, nanoseconds=-1)
504-
elif reso == "day":
505-
start = Timestamp(parsed.year, parsed.month, parsed.day)
506-
end = start + Timedelta(days=1, nanoseconds=-1)
507-
elif reso == "hour":
508-
start = Timestamp(parsed.year, parsed.month, parsed.day, parsed.hour)
509-
end = start + Timedelta(hours=1, nanoseconds=-1)
510-
elif reso == "minute":
511-
start = Timestamp(
512-
parsed.year, parsed.month, parsed.day, parsed.hour, parsed.minute
513-
)
514-
end = start + Timedelta(minutes=1, nanoseconds=-1)
515-
elif reso == "second":
516-
start = Timestamp(
517-
parsed.year,
518-
parsed.month,
519-
parsed.day,
520-
parsed.hour,
521-
parsed.minute,
522-
parsed.second,
523-
)
524-
end = start + Timedelta(seconds=1, nanoseconds=-1)
525-
elif reso == "microsecond":
526-
start = Timestamp(
527-
parsed.year,
528-
parsed.month,
529-
parsed.day,
530-
parsed.hour,
531-
parsed.minute,
532-
parsed.second,
533-
parsed.microsecond,
534-
)
535-
end = start + Timedelta(microseconds=1, nanoseconds=-1)
484+
485+
grp = Resolution.get_freq_group(reso)
486+
per = Period(parsed, freq=(grp, 1))
487+
start, end = per.start_time, per.end_time
488+
536489
# GH 24076
537490
# If an incoming date string contained a UTC offset, need to localize
538491
# the parsed date to this offset first before aligning with the index's

0 commit comments

Comments
 (0)