|
| 1 | +"""``dayfirst=True`` must not reinterpret unambiguous ISO-8601 dates. |
| 2 | +
|
| 3 | +``dayfirst`` exists to resolve *ambiguous* short dates such as ``05/12/2021``. |
| 4 | +An ISO-8601 date is ``YYYY-MM-DD`` by definition, so there is nothing for it to |
| 5 | +resolve. Before this fix ``fd.clean(df, dayfirst=True)`` silently returned |
| 6 | +``2021-05-01`` for the input ``2021-01-05``. |
| 7 | +
|
| 8 | +The cause was pandas 2's format inference, which picks ONE format for a whole |
| 9 | +column from its first value and, with ``dayfirst=True``, reads an ISO date as |
| 10 | +``%Y-%d-%m``. pandas 1.x infers per value and was never affected. |
| 11 | +
|
| 12 | +The corruption was data-dependent, which is what made it dangerous: it is |
| 13 | +silent only while every day is <= 12. A day >= 13 makes the guessed format fail |
| 14 | +on that value, dropping the parse share below ``datetime_threshold`` so the |
| 15 | +mixed-format retry replaces the column with the correct reading. The same |
| 16 | +column therefore read correctly or incorrectly depending on values it happened |
| 17 | +to contain, or on an unrelated threshold. |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import pandas as pd |
| 23 | +import pytest |
| 24 | + |
| 25 | +import freshdata as fd |
| 26 | + |
| 27 | +ISO = ["2021-01-05", "2021-02-11", "2021-03-09"] |
| 28 | + |
| 29 | + |
| 30 | +def _dates(frame, column="v"): |
| 31 | + return [str(v.date()) for v in frame[column]] |
| 32 | + |
| 33 | + |
| 34 | +def test_iso_dates_are_not_reinterpreted_when_dayfirst_is_true(): |
| 35 | + """The regression. Before the fix this returned 2021-05-01 and friends.""" |
| 36 | + out = fd.clean(pd.DataFrame({"v": ISO}), dayfirst=True, verbose=False) |
| 37 | + assert _dates(out) == ISO |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize("dayfirst", [True, False, "auto", None]) |
| 41 | +def test_iso_dates_read_the_same_whatever_dayfirst_says(dayfirst): |
| 42 | + """``dayfirst`` is not a question ISO-8601 input can answer differently.""" |
| 43 | + kwargs = {} if dayfirst is None else {"dayfirst": dayfirst} |
| 44 | + out = fd.clean(pd.DataFrame({"v": ISO}), verbose=False, **kwargs) |
| 45 | + assert _dates(out) == ISO |
| 46 | + |
| 47 | + |
| 48 | +def test_dayfirst_still_does_its_actual_job_on_ambiguous_slash_dates(): |
| 49 | + """The fix must not disarm ``dayfirst`` where it is genuinely needed.""" |
| 50 | + ambiguous = ["05/12/2021", "06/11/2021"] |
| 51 | + day_first = fd.clean(pd.DataFrame({"v": ambiguous}), dayfirst=True, verbose=False) |
| 52 | + assert _dates(day_first) == ["2021-12-05", "2021-11-06"] |
| 53 | + |
| 54 | + month_first = fd.clean(pd.DataFrame({"v": ambiguous}), dayfirst=False, verbose=False) |
| 55 | + assert _dates(month_first) == ["2021-05-12", "2021-06-11"] |
| 56 | + |
| 57 | + |
| 58 | +def test_the_reading_no_longer_depends_on_whether_a_day_exceeds_twelve(): |
| 59 | + """The data-dependence that made the corruption hard to notice. |
| 60 | +
|
| 61 | + Nineteen consecutive January dates. Before the fix these read correctly at |
| 62 | + the default threshold but became 2021-01-01, 2021-02-01, 2021-03-01 ... at |
| 63 | + ``datetime_threshold=0.5``, losing seven values to ``NaT``. |
| 64 | + """ |
| 65 | + dates = [f"2021-01-{d:02d}" for d in range(1, 20)] |
| 66 | + |
| 67 | + default = fd.clean(pd.DataFrame({"v": dates}), dayfirst=True, verbose=False) |
| 68 | + lowered = fd.clean( |
| 69 | + pd.DataFrame({"v": dates}), dayfirst=True, datetime_threshold=0.5, verbose=False |
| 70 | + ) |
| 71 | + |
| 72 | + assert _dates(default) == dates |
| 73 | + assert _dates(lowered) == dates |
| 74 | + assert not default["v"].isna().any() |
| 75 | + assert not lowered["v"].isna().any() |
| 76 | + |
| 77 | + |
| 78 | +def test_a_column_mixing_iso_and_ambiguous_forms_reads_each_by_its_own_shape(): |
| 79 | + mixed = ["2021-01-05", "06/11/2021", "2021-03-09"] |
| 80 | + out = fd.clean(pd.DataFrame({"v": mixed}), dayfirst=True, verbose=False) |
| 81 | + assert _dates(out) == ["2021-01-05", "2021-11-06", "2021-03-09"] |
0 commit comments