Skip to content

Commit ab2d8bc

Browse files
fix lint errors
1 parent fd49ac4 commit ab2d8bc

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

pandas/core/indexes/datetimes.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,10 @@ def _is_iso_format_string(date_str: str) -> bool:
137137
dt.date.fromisoformat(date_str)
138138
return True
139139
except (ValueError, TypeError):
140-
# Fallback regex for reduced precision dates not supported by fromisoformat()
141-
# Checks if string starts with ISO pattern (YYYY, YYYY-MM, YYYY-MM-DD, etc.)
142-
# Pattern: ^\d{4}(?:-|T|$)
143-
# - Requires exactly 4 digits at start (year)
144-
# - Followed by: hyphen (YYYY-), T (YYYY-T...), or end (YYYY)
145-
# Examples that match: "2024", "2024-01", "2024-01-10", "2024-01-10T00:00:00"
146-
# Examples that don't: "01/10/2024", "2024 01 10", "1/1/2024"
147-
return re.match(r"^\d{4}(?:-|T|$)", date_str) is not None
140+
# Fallback for reduced precision dates not supported by fromisoformat()
141+
# Match YYYY, YYYY-MM, YYYY-MM-DD with optional time component
142+
pattern = r"\d{4}(?:-\d{2})?(?:-\d{2})?(?:T.*)?"
143+
return re.fullmatch(pattern, date_str) is not None
148144

149145

150146
@inherit_names(

pandas/tests/indexes/datetimes/test_partial_slicing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,8 @@ def test_loc_non_string_keys_no_warning(self, ser_daily):
561561
with tm.assert_produces_warning(None):
562562
result = ser_daily.loc[Timestamp("2024-01-10")]
563563
assert result == 9
564+
565+
def test_loc_indexing_invalid_iso_pattern_raises_keyerror(self, ser_daily):
566+
# GH#58302 - Malformed date strings fail at parsing, before ISO check
567+
with pytest.raises(KeyError, match="2025-ANYTHING-GOES"):
568+
ser_daily.loc["2025-ANYTHING-GOES"]

0 commit comments

Comments
 (0)