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 4 times, most recently from 2619e36 to 42c889a Compare November 6, 2025 07:10
@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

@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?

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?

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

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