Skip to content

Conversation

@raghavendranath
Copy link

@raghavendranath raghavendranath commented Nov 5, 2025

Before:

ser.loc["1/10/2024":"1/14/2024"]      # Returns Jan 10-14
ser.loc["1/10/2024":"14/1/2024"]      # Also returns Jan 10-14
ser.loc["10/1/2024":"14/1/2024"]      # Returns empty

After:

ser.loc["1/10/2024":"1/14/2024"]      # Still returns Jan 10-14 + FutureWarning
ser.loc["2024-01-10":"2024-01-14"]    # Use ISO format instead

There's no way to know if "1/10/2024" is being parsed as MM/DD or DD/MM until you run it. This deprecation pushes users toward ISO format to avoid the confusion.

@raghavendranath raghavendranath changed the title DEPR: Deprecate non-ISO date string formats in DatetimeIndex.loc #58302 DEPR: Deprecate non-ISO date string formats in DatetimeIndex.loc Nov 5, 2025
@raghavendranath raghavendranath force-pushed the depr-datetimeindex-loc-slice-dayfirst branch 5 times, most recently from 415367c to 109e1c2 Compare November 7, 2025 03:02
@raghavendranath
Copy link
Author

Hi @WillAyd @MarcoGorelli @mroeschke The PR is ready for review whenever you have time. Thanks

@natmokval natmokval added the Indexing Related to indexing on series/frames, not to indexes themselves label Nov 9, 2025
return result


def _is_iso_format_string(date_str: str) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Credit to @stefmolin for the original idea, but I don't think we should try and roll our own regex here if we can avoid it.

I see the standard library provides date.fromisostring, although that is documented to not work with "Reduced Precision" dates:

https://docs.python.org/3/library/datetime.html

Even still I wonder if we can't use that first and only fallback when it fails

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @stefmolin @WillAyd!. Switched to using date.fromisoformat() like you suggested. Added a regex fallback to handle the reduced precision dates (YYYY and YYYY-MM) that fromisoformat doesn't support .

@pytest.mark.parametrize("bins", [None, [0, 5]], ids=repr)
@pytest.mark.parametrize("isort", [True, False])
@pytest.mark.parametrize("normalize, name", [(True, "proportion"), (False, "count")])
@pytest.mark.filterwarnings(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this test need to filter warnings? It seems unrelated to the change?

Copy link
Author

@raghavendranath raghavendranath Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we group by keys='2nd' (the date column - from parameterized tests), it triggers the deprecation warning internally during the groupby operation. Without it, those test cases fail in CI. So, added the filterwarnings.

with pytest.raises(TypeError, match=msg):
dti.get_loc(key)

@pytest.mark.filterwarnings(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test still exhibit the expected behavior if you change the test to use ISO strings?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works as expected. Updated the test to use ISO format strings instead and removed the filterwarnings.

tm.assert_series_equal(ts[[Period("2012-01-02", freq="D")]], exp)

@pytest.mark.arm_slow
@pytest.mark.filterwarnings(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can change the test data here to use ISO strings, unless it explicitly tests non-ISO for a reason

Copy link
Author

@raghavendranath raghavendranath Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting all the dates to ISO format in this test.

Copy link
Author

@raghavendranath raghavendranath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WillAyd Fixed all your comments. Ready for review whenever you get a chance.

@raghavendranath raghavendranath force-pushed the depr-datetimeindex-loc-slice-dayfirst branch from b82bf18 to 936847d Compare November 11, 2025 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Indexing Related to indexing on series/frames, not to indexes themselves

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: .loc with DateTimeIndex allows day first string for stop in slice, but not start

3 participants