Skip to content

REF: simplify __can_partial_date_slice, _parsed_string_to_bounds #42258

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
Jul 1, 2021
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
25 changes: 5 additions & 20 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,21 +567,6 @@ def _parsed_string_to_bounds(self, reso: Resolution, parsed: datetime):
-------
lower, upper: pd.Timestamp
"""
assert isinstance(reso, Resolution), (type(reso), reso)
valid_resos = {
"year",
"month",
"quarter",
"day",
"hour",
"minute",
"second",
"millisecond",
"microsecond",
}
if reso.attrname not in valid_resos:
raise KeyError

grp = reso.freq_group
per = Period(parsed, freq=grp.value)
start, end = per.start_time, per.end_time
Expand All @@ -590,17 +575,17 @@ def _parsed_string_to_bounds(self, reso: Resolution, parsed: datetime):
# If an incoming date string contained a UTC offset, need to localize
# the parsed date to this offset first before aligning with the index's
# timezone
start = start.tz_localize(parsed.tzinfo)
end = end.tz_localize(parsed.tzinfo)

if parsed.tzinfo is not None:
if self.tz is None:
raise ValueError(
"The index must be timezone aware when indexing "
"with a date string with a UTC offset"
)
start = start.tz_localize(parsed.tzinfo).tz_convert(self.tz)
end = end.tz_localize(parsed.tzinfo).tz_convert(self.tz)
elif self.tz is not None:
start = start.tz_localize(self.tz)
end = end.tz_localize(self.tz)
start = self._maybe_cast_for_get_loc(start)
end = self._maybe_cast_for_get_loc(end)
return start, end

def _can_partial_date_slice(self, reso: Resolution) -> bool:
Expand Down
12 changes: 2 additions & 10 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,16 +503,8 @@ def _parsed_string_to_bounds(self, reso: Resolution, parsed: datetime):

def _can_partial_date_slice(self, reso: Resolution) -> bool:
assert isinstance(reso, Resolution), (type(reso), reso)
grp = reso.freq_group
freqn = self.dtype.freq_group_code

if not grp.value < freqn:
# TODO: we used to also check for
# reso in ["day", "hour", "minute", "second"]
# why is that check not needed?
return False

return True
# e.g. test_getitem_setitem_periodindex
return reso > self.dtype.resolution


def period_range(
Expand Down