Skip to content

Commit c0bda6e

Browse files
fix: only warn on non-ISO formats in .loc indexing paths
1 parent 6877fec commit c0bda6e

File tree

14 files changed

+62
-10
lines changed

14 files changed

+62
-10
lines changed

pandas/core/indexes/datetimes.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -591,15 +591,6 @@ def _parsed_string_to_bounds(
591591
def _parse_with_reso(self, label: str) -> tuple[Timestamp, Resolution]:
592592
parsed, reso = super()._parse_with_reso(label)
593593

594-
# GH#58302 - Deprecate non-ISO string formats in .loc indexing
595-
if isinstance(label, str) and not _is_iso_format_string(label):
596-
msg = (
597-
"Parsing non-ISO datetime strings in .loc is deprecated and will be "
598-
"removed in a future version. Use ISO format (YYYY-MM-DD) instead. "
599-
f"Got '{label}'."
600-
)
601-
warnings.warn(msg, Pandas4Warning, stacklevel=find_stack_level())
602-
603594
parsed = Timestamp(parsed)
604595

605596
if self.tz is not None and parsed.tzinfo is None:
@@ -645,6 +636,14 @@ def get_loc(self, key):
645636
parsed, reso = self._parse_with_reso(key)
646637
except ValueError as err:
647638
raise KeyError(key) from err
639+
# GH#58302 - Deprecate non-ISO string formats in .loc indexing
640+
if not _is_iso_format_string(key):
641+
msg = (
642+
"Parsing non-ISO datetime strings in .loc is deprecated "
643+
"and will be removed in a future version. Use ISO format "
644+
f"(YYYY-MM-DD) instead. Got '{key}'."
645+
)
646+
warnings.warn(msg, Pandas4Warning, stacklevel=find_stack_level())
648647
self._disallow_mismatched_indexing(parsed)
649648

650649
if self._can_partial_date_slice(reso):

pandas/tests/groupby/test_groupby.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
from pandas.core.arrays import BooleanArray
2929
import pandas.core.common as com
3030

31-
pytestmark = pytest.mark.filterwarnings("ignore:Mean of empty slice:RuntimeWarning")
31+
pytestmark = [
32+
pytest.mark.filterwarnings("ignore:Mean of empty slice:RuntimeWarning"),
33+
pytest.mark.filterwarnings(
34+
"ignore:Parsing non-ISO datetime strings:pandas.errors.Pandas4Warning"
35+
),
36+
]
3237

3338

3439
def test_repr():

pandas/tests/indexes/datetimes/test_indexing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
from pandas.tseries.frequencies import to_offset
2828

29+
pytestmark = pytest.mark.filterwarnings(
30+
"ignore:Parsing non-ISO datetime strings:pandas.errors.Pandas4Warning"
31+
)
32+
2933
START, END = datetime(2009, 1, 1), datetime(2010, 1, 1)
3034

3135

pandas/tests/indexes/datetimes/test_partial_slicing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
)
2020
import pandas._testing as tm
2121

22+
pytestmark = pytest.mark.filterwarnings(
23+
"ignore:Parsing non-ISO datetime strings:pandas.errors.Pandas4Warning"
24+
)
25+
2226

2327
class TestSlicing:
2428
def test_string_index_series_name_converted(self):

pandas/tests/indexes/multi/test_partial_indexing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
)
1010
import pandas._testing as tm
1111

12+
pytestmark = pytest.mark.filterwarnings(
13+
"ignore:Parsing non-ISO datetime strings:pandas.errors.Pandas4Warning"
14+
)
15+
1216

1317
@pytest.fixture
1418
def df():

pandas/tests/indexes/period/test_indexing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
)
2222
import pandas._testing as tm
2323

24+
pytestmark = pytest.mark.filterwarnings(
25+
"ignore:Parsing non-ISO datetime strings:pandas.errors.Pandas4Warning"
26+
)
27+
2428
dti4 = date_range("2016-01-01", periods=4)
2529
dti = dti4[:-1]
2630
rng = pd.Index(range(3))

pandas/tests/indexes/period/test_partial_slicing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
)
1111
import pandas._testing as tm
1212

13+
pytestmark = pytest.mark.filterwarnings(
14+
"ignore:Parsing non-ISO datetime strings:pandas.errors.Pandas4Warning"
15+
)
16+
1317

1418
class TestPeriodIndex:
1519
def test_getitem_periodindex_duplicates_string_slice(self):

pandas/tests/indexing/multiindex/test_slice.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import pandas._testing as tm
2020
from pandas.tests.indexing.common import _mklbl
2121

22+
pytestmark = pytest.mark.filterwarnings(
23+
"ignore:Parsing non-ISO datetime strings:pandas.errors.Pandas4Warning"
24+
)
25+
2226

2327
class TestMultiIndexSlicers:
2428
def test_per_axis_per_level_getitem(self):

pandas/tests/indexing/test_loc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
from pandas.core.indexing import _one_ellipsis_message
4545
from pandas.tests.indexing.common import check_indexing_smoketest_or_raises
4646

47+
pytestmark = pytest.mark.filterwarnings(
48+
"ignore:Parsing non-ISO datetime strings:pandas.errors.Pandas4Warning"
49+
)
50+
4751

4852
@pytest.mark.parametrize(
4953
"series, new_series, expected_ser",

pandas/tests/resample/test_datetime_index.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
from pandas.tseries.frequencies import to_offset
3939
from pandas.tseries.offsets import Minute
4040

41+
pytestmark = pytest.mark.filterwarnings(
42+
"ignore:Parsing non-ISO datetime strings:pandas.errors.Pandas4Warning"
43+
)
44+
4145

4246
@pytest.fixture
4347
def simple_date_range_series():

0 commit comments

Comments
 (0)