Skip to content

Commit 936847d

Browse files
fixed format errors
1 parent 23ba779 commit 936847d

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

pandas/core/indexes/datetimes.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,13 @@ def _is_iso_format_string(date_str: str) -> bool:
115115
"""
116116
Check if a date string follows ISO8601 format.
117117
118-
ISO format must start with a 4-digit year (YYYY), optionally followed by
119-
hyphen-separated month and day or 'T' for time component.
118+
Uses date.fromisoformat() to validate full ISO dates, with fallback to regex
119+
for reduced precision dates (YYYY or YYYY-MM) which are not supported by
120+
fromisoformat().
120121
121122
Examples of ISO format (True):
122-
- 2024
123-
- 2024-01
123+
- 2024 (reduced precision)
124+
- 2024-01 (reduced precision)
124125
- 2024-01-10
125126
- 2024-01-10T00:00:00
126127
@@ -131,7 +132,19 @@ def _is_iso_format_string(date_str: str) -> bool:
131132
- 10/01/2024 (DD/MM/YYYY)
132133
- 01-10-2024 (MM-DD-YYYY)
133134
"""
134-
return re.match(r"^\d{4}(?:-|T|$)", date_str) is not None
135+
try:
136+
# Standard library validates full ISO dates (YYYY-MM-DD format)
137+
dt.date.fromisoformat(date_str)
138+
return True
139+
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
135148

136149

137150
@inherit_names(

pandas/tests/indexes/datetimes/test_indexing.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,11 @@ def test_get_loc_timedelta_invalid_key(self, key):
489489
with pytest.raises(TypeError, match=msg):
490490
dti.get_loc(key)
491491

492-
@pytest.mark.filterwarnings(
493-
"ignore:Parsing non-ISO datetime strings:pandas.errors.Pandas4Warning"
494-
)
495492
def test_get_loc_reasonable_key_error(self):
496493
# GH#1062
497-
index = DatetimeIndex(["1/3/2000"])
494+
index = DatetimeIndex(["2000-01-03"])
498495
with pytest.raises(KeyError, match="2000"):
499-
index.get_loc("1/1/2000")
496+
index.get_loc("2000-01-01")
500497

501498
def test_get_loc_year_str(self):
502499
rng = date_range("1/1/2000", "1/1/2010")

pandas/tests/indexes/period/test_indexing.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,22 +172,19 @@ def test_getitem_list_periods(self):
172172
tm.assert_series_equal(ts[[Period("2012-01-02", freq="D")]], exp)
173173

174174
@pytest.mark.arm_slow
175-
@pytest.mark.filterwarnings(
176-
"ignore:Parsing non-ISO datetime strings:pandas.errors.Pandas4Warning"
177-
)
178175
def test_getitem_seconds(self):
179176
# GH#6716
180-
didx = date_range(start="2013/01/01 09:00:00", freq="s", periods=4000)
181-
pidx = period_range(start="2013/01/01 09:00:00", freq="s", periods=4000)
177+
didx = date_range(start="2013-01-01 09:00:00", freq="s", periods=4000)
178+
pidx = period_range(start="2013-01-01 09:00:00", freq="s", periods=4000)
182179

183180
for idx in [didx, pidx]:
184181
# getitem against index should raise ValueError
185182
values = [
186183
"2014",
187-
"2013/02",
188-
"2013/01/02",
189-
"2013/02/01 9h",
190-
"2013/02/01 09:00",
184+
"2013-02",
185+
"2013-01-02",
186+
"2013-02-01 9h",
187+
"2013-02-01 09:00",
191188
]
192189
for val in values:
193190
# GH7116
@@ -197,9 +194,9 @@ def test_getitem_seconds(self):
197194
idx[val]
198195

199196
ser = Series(np.random.default_rng(2).random(len(idx)), index=idx)
200-
tm.assert_series_equal(ser["2013/01/01 10:00"], ser[3600:3660])
201-
tm.assert_series_equal(ser["2013/01/01 9h"], ser[:3600])
202-
for d in ["2013/01/01", "2013/01", "2013"]:
197+
tm.assert_series_equal(ser["2013-01-01 10:00"], ser[3600:3660])
198+
tm.assert_series_equal(ser["2013-01-01 9h"], ser[:3600])
199+
for d in ["2013-01-01", "2013-01", "2013"]:
203200
tm.assert_series_equal(ser[d], ser)
204201

205202
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)