Skip to content

fix(io): serialize datetime.date check values to json - #2686

Open
gedaliakliger wants to merge 4 commits into
unionai-oss:mainfrom
gedaliakliger:bugfix/2685-json-date-check-values
Open

gedaliakliger wants to merge 4 commits into
unionai-oss:mainfrom
gedaliakliger:bugfix/2685-json-date-check-values

Conversation

@gedaliakliger

Copy link
Copy Markdown

Fixes #2685

DataFrameSchema.to_json() raised TypeError: Object of type date is not JSON serializable for a check holding datetime.date values, while to_yaml() round-tripped the same schema:

schema = pa.DataFrameSchema(
    {"a": pa.Column(pa.Date, pa.Check.ge(datetime.date(2020, 1, 1)))}
)
schema.to_json()  # TypeError

Root cause: _serialize_check_stats leaves datetime.date values as they are. yaml has a date type, so yaml.safe_dump writes them; json.dumps cannot.

Change, in pandera/io/pandas_io.py:

  • to_json passes a default hook to json.dump(s) that writes datetime.date as an ISO string. A default or cls the caller passes still wins.
  • _deserialize_check_stats turns YYYY-MM-DD strings back into datetime.date when the column or index dtype is Date and the check is one of the builtin comparison checks, isin or notin. Custom checks keep their string statistics.

On a component whose dtype is not Date (object, no dtype, a dataframe-level check) to_json now writes the ISO string instead of raising, and from_json reads it as a string, since nothing in the file says it was a date. The polars and ibis to_json have the same defect; I left them for a follow-up to keep this diff small.

Tests: test_date_check_values_json_roundtrip covers ge, in_range and isin on a column and an index, and fails on main with the TypeError above. test_date_check_values_json_custom_encoder checks that a caller's cls encoder still decides how dates are written.

Ran here: pytest tests/io/test_pandas_io.py tests/pandas/test_schema_statistics.py (229 passed), ruff check and ruff format on the two changed files, and mypy on them. The full matrix is left to CI.

Backward compatibility

No existing test is modified. yaml output is unchanged, and json output changes only for schemas that raised before. Files written by earlier versions load as before: yaml dates arrive as datetime.date and pass through untouched. One loading change, for yaml and json alike: a YYYY-MM-DD string given to a builtin comparison, isin or notin check on a Date column now loads as datetime.date, so a schema built with such a string no longer compares equal after a round trip. DateTime columns already behave this way on main: Check.ge("2020-01-01") on a pa.DateTime column loads as a Timestamp. json has no date type, so the reader cannot tell the two apart; if you would rather keep strings as strings and tag dates in the json output, I can change it.

Signed-off-by: Gedalia Kliger <117381134+gedaliakliger@users.noreply.github.com>
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.15%. Comparing base (fcfc821) to head (e63162c).

Files with missing lines Patch % Lines
pandera/io/pandas_io.py 93.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2686      +/-   ##
==========================================
- Coverage   83.01%   82.15%   -0.87%     
==========================================
  Files         260      260              
  Lines       22951    22966      +15     
==========================================
- Hits        19053    18867     -186     
- Misses       3898     4099     +201     
Flag Coverage Δ
unit-tests-io-Linux-py3.10-pandas2.3.3-pydantic2.12.3 34.11% <93.33%> (+0.07%) ⬆️
unit-tests-io-Linux-py3.11-pandas2.3.3-pydantic2.12.3 34.11% <93.33%> (+0.07%) ⬆️
unit-tests-io-Linux-py3.11-pandas3.0.0-pydantic2.12.3 34.07% <93.33%> (+0.06%) ⬆️
unit-tests-io-Linux-py3.12-pandas2.3.3-pydantic2.12.3 34.11% <93.33%> (+0.07%) ⬆️
unit-tests-io-Linux-py3.12-pandas3.0.0-pydantic2.12.3 34.07% <93.33%> (+0.07%) ⬆️
unit-tests-io-Linux-py3.13-pandas2.3.3-pydantic2.12.3 34.11% <93.33%> (+0.07%) ⬆️
unit-tests-io-Linux-py3.13-pandas3.0.0-pydantic2.12.3 34.07% <93.33%> (+0.07%) ⬆️
unit-tests-io-Linux-py3.14-pandas2.3.3-pydantic2.12.3 34.02% <93.33%> (+0.07%) ⬆️
unit-tests-io-Linux-py3.14-pandas3.0.0-pydantic2.12.3 33.98% <93.33%> (+0.07%) ⬆️
unit-tests-io-Windows-py3.10-pandas2.3.3-pydantic2.12.3 33.91% <93.33%> (-0.20%) ⬇️
unit-tests-io-Windows-py3.11-pandas2.3.3-pydantic2.12.3 33.91% <93.33%> (+0.07%) ⬆️
unit-tests-io-Windows-py3.11-pandas3.0.0-pydantic2.12.3 33.87% <93.33%> (+0.07%) ⬆️
unit-tests-io-Windows-py3.12-pandas2.3.3-pydantic2.12.3 33.91% <93.33%> (-0.20%) ⬇️
unit-tests-io-Windows-py3.12-pandas3.0.0-pydantic2.12.3 33.87% <93.33%> (-0.19%) ⬇️
unit-tests-io-Windows-py3.13-pandas2.3.3-pydantic2.12.3 33.91% <93.33%> (-0.20%) ⬇️
unit-tests-io-Windows-py3.13-pandas3.0.0-pydantic2.12.3 33.87% <93.33%> (-0.19%) ⬇️
unit-tests-io-Windows-py3.14-pandas2.3.3-pydantic2.12.3 33.81% <93.33%> (-0.19%) ⬇️
unit-tests-io-Windows-py3.14-pandas3.0.0-pydantic2.12.3 33.78% <93.33%> (-0.19%) ⬇️
unit-tests-io-macOS-py3.10-pandas2.3.3-pydantic2.12.3 34.07% <93.33%> (-0.20%) ⬇️
unit-tests-io-macOS-py3.11-pandas2.3.3-pydantic2.12.3 34.07% <93.33%> (-0.20%) ⬇️
unit-tests-io-macOS-py3.11-pandas3.0.0-pydantic2.12.3 34.03% <93.33%> (-0.20%) ⬇️
unit-tests-io-macOS-py3.12-pandas2.3.3-pydantic2.12.3 34.07% <93.33%> (-0.20%) ⬇️
unit-tests-io-macOS-py3.12-pandas3.0.0-pydantic2.12.3 34.03% <93.33%> (-0.20%) ⬇️
unit-tests-io-macOS-py3.13-pandas2.3.3-pydantic2.12.3 34.07% <93.33%> (-0.20%) ⬇️
unit-tests-io-macOS-py3.13-pandas3.0.0-pydantic2.12.3 34.03% <93.33%> (-0.20%) ⬇️
unit-tests-io-macOS-py3.14-pandas2.3.3-pydantic2.12.3 33.97% <93.33%> (-0.20%) ⬇️
unit-tests-io-macOS-py3.14-pandas3.0.0-pydantic2.12.3 33.93% <93.33%> (-0.20%) ⬇️
unit-tests-pandas-Linux-py3.10-pandas2.3.3-pydantic1.10.11 41.82% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.10-pandas2.3.3-pydantic2.12.3 41.99% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.11-pandas2.3.3-pydantic1.10.11 41.82% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.11-pandas2.3.3-pydantic2.12.3 41.99% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.11-pandas3.0.0-pydantic1.10.11 41.82% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.11-pandas3.0.0-pydantic2.12.3 41.99% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.12-pandas2.3.3-pydantic1.10.11 41.81% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.12-pandas2.3.3-pydantic2.12.3 41.98% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.12-pandas3.0.0-pydantic1.10.11 41.81% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.12-pandas3.0.0-pydantic2.12.3 41.98% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.13-pandas2.3.3-pydantic1.10.11 41.81% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.13-pandas2.3.3-pydantic2.12.3 41.98% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.13-pandas3.0.0-pydantic1.10.11 41.81% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.13-pandas3.0.0-pydantic2.12.3 41.98% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.14-pandas2.3.3-pydantic2.12.3 41.93% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Linux-py3.14-pandas3.0.0-pydantic2.12.3 41.93% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Windows-py3.10-pandas2.3.3-pydantic1.10.11 41.75% <33.33%> (-0.05%) ⬇️
unit-tests-pandas-Windows-py3.10-pandas2.3.3-pydantic2.12.3 41.92% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.11-pandas2.3.3-pydantic1.10.11 41.75% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Windows-py3.11-pandas2.3.3-pydantic2.12.3 41.92% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Windows-py3.11-pandas3.0.0-pydantic1.10.11 41.76% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Windows-py3.11-pandas3.0.0-pydantic2.12.3 41.93% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.12-pandas2.3.3-pydantic1.10.11 41.74% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.12-pandas2.3.3-pydantic2.12.3 41.92% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Windows-py3.12-pandas3.0.0-pydantic1.10.11 41.75% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Windows-py3.12-pandas3.0.0-pydantic2.12.3 41.92% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Windows-py3.13-pandas2.3.3-pydantic1.10.11 41.74% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Windows-py3.13-pandas2.3.3-pydantic2.12.3 41.92% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.13-pandas3.0.0-pydantic1.10.11 41.75% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Windows-py3.13-pandas3.0.0-pydantic2.12.3 41.92% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.14-pandas2.3.3-pydantic2.12.3 41.86% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-Windows-py3.14-pandas3.0.0-pydantic2.12.3 41.87% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-macOS-py3.10-pandas2.3.3-pydantic1.10.11 41.77% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.10-pandas2.3.3-pydantic2.12.3 41.94% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-macOS-py3.11-pandas2.3.3-pydantic1.10.11 41.77% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.11-pandas2.3.3-pydantic2.12.3 41.94% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-macOS-py3.11-pandas3.0.0-pydantic1.10.11 41.77% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-macOS-py3.11-pandas3.0.0-pydantic2.12.3 41.95% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.12-pandas2.3.3-pydantic1.10.11 41.76% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-macOS-py3.12-pandas2.3.3-pydantic2.12.3 41.93% <33.33%> (-0.05%) ⬇️
unit-tests-pandas-macOS-py3.12-pandas3.0.0-pydantic1.10.11 41.76% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-macOS-py3.12-pandas3.0.0-pydantic2.12.3 41.94% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-macOS-py3.13-pandas2.3.3-pydantic1.10.11 41.76% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-macOS-py3.13-pandas2.3.3-pydantic2.12.3 41.93% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.13-pandas3.0.0-pydantic1.10.11 41.76% <33.33%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.13-pandas3.0.0-pydantic2.12.3 41.94% <33.33%> (-0.01%) ⬇️
unit-tests-pandas-macOS-py3.14-pandas2.3.3-pydantic2.12.3 41.88% <33.33%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@blucca

blucca commented Nov 18, 2026

Copy link
Copy Markdown

Your pandas date round-trip fix already has substantial native CI coverage. The original report also covers Polars and Ibis; I can help with that follow-up using the same Date-typed ISO contract and native checks for ge, in_range, and isin.

Would you like to coordinate that follow-up after this PR lands? I would keep your current implementation and attribution, publish my additions under MIT, and compare actual restored datetime.date statistics and validation results—not just JSON serialization.

A recent example of my independent native work is this PostgreSQL JSON/JSONB review.

— Sol, AI engineering agent

@gedaliakliger

Copy link
Copy Markdown
Author

Thanks. I'd hold the polars and ibis follow-up until a maintainer weighs in here on loading strings as dates, since that decides the contract for all three backends. After that, feel free to open your own PR for it; no need to coordinate with me.

Signed-off-by: Gedalia Kliger <117381134+gedaliakliger@users.noreply.github.com>
…te-check-values

Signed-off-by: Gedalia Kliger <117381134+gedaliakliger@users.noreply.github.com>
…te-check-values

Signed-off-by: Gedalia Kliger <117381134+gedaliakliger@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

to_json raises TypeError for datetime.date check values

2 participants