|
5 | 5 | import numpy as np |
6 | 6 | import pytest |
7 | 7 |
|
| 8 | +from pandas.errors import Pandas4Warning |
| 9 | + |
8 | 10 | from pandas import ( |
9 | 11 | DataFrame, |
10 | 12 | DatetimeIndex, |
@@ -464,3 +466,96 @@ def test_slice_reduce_to_series(self): |
464 | 466 | ) |
465 | 467 | result = df.loc["2000", "A"] |
466 | 468 | tm.assert_series_equal(result, expected) |
| 469 | + |
| 470 | + |
| 471 | +class TestDatetimeIndexNonISODeprecation: |
| 472 | + """Tests for deprecation of non-ISO string formats in .loc indexing. GH#58302""" |
| 473 | + |
| 474 | + @pytest.fixture |
| 475 | + def ser_daily(self): |
| 476 | + """Create a Series with daily DatetimeIndex for testing.""" |
| 477 | + return Series( |
| 478 | + range(15), |
| 479 | + index=DatetimeIndex(date_range(start="2024-01-01", freq="D", periods=15)), |
| 480 | + ) |
| 481 | + |
| 482 | + @pytest.mark.parametrize( |
| 483 | + "date_string", |
| 484 | + [ |
| 485 | + "1/10/2024", # MM/DD/YYYY format |
| 486 | + "01/10/2024", # MM/DD/YYYY format with leading zero |
| 487 | + ], |
| 488 | + ) |
| 489 | + def test_loc_indexing_non_iso_single_key_deprecation(self, ser_daily, date_string): |
| 490 | + # GH#58302 |
| 491 | + msg = "Parsing non-ISO datetime strings in .loc is deprecated" |
| 492 | + |
| 493 | + with tm.assert_produces_warning(Pandas4Warning, match=msg): |
| 494 | + result = ser_daily.loc[date_string] |
| 495 | + assert result == 9 |
| 496 | + |
| 497 | + @pytest.mark.parametrize( |
| 498 | + "date_string,expected", |
| 499 | + [ |
| 500 | + ("2024-01-10", 9), # YYYY-MM-DD (dash) |
| 501 | + ("2024/01/10", 9), # YYYY/MM/DD (slash) |
| 502 | + ("2024 01 10", 9), # YYYY MM DD (space) |
| 503 | + ], |
| 504 | + ) |
| 505 | + def test_loc_indexing_iso_format_no_warning(self, ser_daily, date_string, expected): |
| 506 | + # GH#58302 - ISO formats should NOT warn |
| 507 | + with tm.assert_produces_warning(None): |
| 508 | + result = ser_daily.loc[date_string] |
| 509 | + assert result == expected |
| 510 | + |
| 511 | + @pytest.mark.parametrize( |
| 512 | + "start_string", |
| 513 | + [ |
| 514 | + "1/10/2024", # MM/DD/YYYY format |
| 515 | + "01/10/2024", # MM/DD/YYYY format with leading zero |
| 516 | + ], |
| 517 | + ) |
| 518 | + def test_loc_slicing_non_iso_start_deprecation(self, ser_daily, start_string): |
| 519 | + # GH#58302 - Non-ISO start in slice should warn |
| 520 | + msg = "Parsing non-ISO datetime strings in .loc is deprecated" |
| 521 | + |
| 522 | + with tm.assert_produces_warning(Pandas4Warning, match=msg): |
| 523 | + result = ser_daily.loc[start_string:"2024-01-15"] |
| 524 | + assert len(result) > 0 |
| 525 | + |
| 526 | + @pytest.mark.parametrize( |
| 527 | + "end_string", |
| 528 | + [ |
| 529 | + "5-01-2024", # DD-MM-YYYY format |
| 530 | + "05-01-2024", # DD-MM-YYYY format with leading zero |
| 531 | + ], |
| 532 | + ) |
| 533 | + def test_loc_slicing_non_iso_end_deprecation(self, ser_daily, end_string): |
| 534 | + # GH#58302 - Non-ISO end in slice should warn |
| 535 | + msg = "Parsing non-ISO datetime strings in .loc is deprecated" |
| 536 | + |
| 537 | + with tm.assert_produces_warning(Pandas4Warning, match=msg): |
| 538 | + result = ser_daily.loc["2024-01-01":end_string] |
| 539 | + assert len(result) > 0 |
| 540 | + |
| 541 | + def test_loc_slicing_both_non_iso_deprecation(self, ser_daily): |
| 542 | + # GH#58302 - Both non-ISO should warn (twice) |
| 543 | + msg = "Parsing non-ISO datetime strings in .loc is deprecated" |
| 544 | + |
| 545 | + with tm.assert_produces_warning( |
| 546 | + Pandas4Warning, match=msg, check_stacklevel=False |
| 547 | + ): |
| 548 | + result = ser_daily.loc["1/10/2024":"5-01-2024"] |
| 549 | + assert len(result) > 0 |
| 550 | + |
| 551 | + def test_loc_slicing_iso_formats_no_warning(self, ser_daily): |
| 552 | + # GH#58302 - ISO slice formats should NOT warn |
| 553 | + with tm.assert_produces_warning(None): |
| 554 | + result = ser_daily.loc["2024-01-05":"2024-01-10"] |
| 555 | + assert len(result) == 6 |
| 556 | + |
| 557 | + def test_loc_non_string_keys_no_warning(self, ser_daily): |
| 558 | + # GH#58302 - Non-string keys should not warn |
| 559 | + with tm.assert_produces_warning(None): |
| 560 | + result = ser_daily.loc[Timestamp("2024-01-10")] |
| 561 | + assert result == 9 |
0 commit comments