Skip to content

fix(io): round-trip pyarrow timestamp and duration check statistics - #2707

Open
adityashankarv wants to merge 2 commits into
unionai-oss:mainfrom
adityashankarv:bugfix/pyarrow-timestamp-check-stats
Open

adityashankarv wants to merge 2 commits into
unionai-oss:mainfrom
adityashankarv:bugfix/pyarrow-timestamp-check-stats

Conversation

@adityashankarv

Copy link
Copy Markdown

Summary

Follow-up to #2697 (issue #2696), as agreed with @maoji-y ("yes, please go ahead with the
_deserialize_check_stats follow-up, it's yours").

#2697 made a schema with a timestamp[us][pyarrow] column load back from to_yaml/to_json.
Check values on those columns still did not survive serialization:

import pandas as pd, pyarrow as pa, pandera.pandas as pandera
schema = pandera.DataFrameSchema({"c": pandera.Column(
    pd.ArrowDtype(pa.timestamp("us")),
    pandera.Check.greater_than(pd.Timestamp("2020-01-01")))})
schema.to_yaml()  # yaml.representer.RepresenterError: cannot represent an object, Timestamp('2020-01-01 00:00:00')
schema.to_json()  # TypeError: Object of type Timestamp is not JSON serializable

Same for in_range, tz-aware timestamps and Timedelta bounds on duration[...][pyarrow].

Cause: _serialize_check_stats / _deserialize_check_stats in pandera/io/pandas_io.py
only recognised the numpy-backed pandas_engine.DateTime / Timedelta dtypes, so the
pyarrow-backed ArrowTimestamp / ArrowDuration fell through and the raw Timestamp /
Timedelta reached the yaml/json writer.

Fix (16 lines in pandas_io.py): treat ArrowTimestamp like DateTime for the existing
datetime comparison checks (ISO 8601 strings: keeps sub-second precision and the UTC offset,
same encoding as #2613) and ArrowDuration like Timedelta (integer nanoseconds). The
pyarrow classes are looked up with getattr(..., ()) because they only exist when pyarrow is
installed.

Out of scope (left unchanged on purpose): date32/date64 bounds. yaml already round-trips
them; json fails the same way for the native pandera.Date dtype, which is a separate
limitation that is not pyarrow specific.

Tests

New in tests/io/test_pandas_io.py (no existing test modified):

  • test_roundtrip_pyarrow_temporal_check_statistics (yaml + json, minimal on/off):
    naive timestamp[us], timestamp[ns, tz=UTC] (1 ns bound), timestamp[us, tz=Asia/Kolkata],
    duration[us]. Asserts the statistics are equal after the round trip and that bound-1/bound/bound+1
    validate identically for the original and the loaded schema.
  • test_roundtrip_pyarrow_timestamp_in_range_check (yaml + json).

Verified they fail without the fix (18 of the 20 new cases fail on main) and pass with it.

Run locally (Python 3.13, pandas 3.1.1, pyarrow 26.0.1):

  • pytest tests/io tests/pandas/test_pandas_engine.py tests/pandas/test_schemas.py tests/pandas/test_dtypes.py tests/pandas/test_checks.py tests/pandas/test_pandas_accessor.py -> 1600 passed, 14 skipped, 1 xfailed
  • ruff check, ruff format --check, codespell clean; mypy reports no new errors (same pre-existing set before/after).

AI assistance disclosure

I used an AI assistant (Grok Bot) to help investigate, write and test this change. I've reviewed it
and can explain the whole change.

Backward compatibility

No existing test was changed. Schemas with numpy-backed datetime/timedelta columns serialize
exactly as before; only pyarrow-backed timestamp/duration columns, which previously raised on
serialization, are affected.

Check statistics on pyarrow-backed timestamp and duration columns, e.g.
Check.greater_than(pd.Timestamp(...)) on a timestamp[us][pyarrow] column,
could not be serialized: to_yaml raised a RepresenterError and to_json a
"Object of type Timestamp is not JSON serializable" TypeError, because
the datetime and timedelta handling in _serialize_check_stats and
_deserialize_check_stats only recognized the numpy-backed DateTime and
Timedelta dtypes.

Treat ArrowTimestamp like DateTime for the datetime comparison checks
(ISO 8601 strings, which keep sub-second precision and the UTC offset of
timezone-aware bounds) and ArrowDuration like Timedelta (integer
nanoseconds), so the statistics survive a to_yaml/from_yaml and
to_json/from_json round trip and keep their validation semantics.

date32/date64 check values are not changed: yaml already handles them
and json fails the same way for the native pandera Date dtype, which is
a separate limitation.

Follow-up to unionai-oss#2697 (see unionai-oss#2696).

Signed-off-by: adityashankarv <143811563+adityashankarv@users.noreply.github.com>
@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.01%. Comparing base (fcfc821) to head (6d1f064).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2707      +/-   ##
==========================================
- Coverage   83.01%   83.01%   -0.01%     
==========================================
  Files         260      260              
  Lines       22951    22953       +2     
==========================================
+ Hits        19053    19054       +1     
- Misses       3898     3899       +1     
Flag Coverage Δ
unit-tests-io-Linux-py3.10-pandas2.3.3-pydantic2.12.3 34.04% <100.00%> (+<0.01%) ⬆️
unit-tests-io-Linux-py3.11-pandas2.3.3-pydantic2.12.3 34.04% <100.00%> (+<0.01%) ⬆️
unit-tests-io-Linux-py3.11-pandas3.0.0-pydantic2.12.3 34.01% <100.00%> (+<0.01%) ⬆️
unit-tests-io-Linux-py3.12-pandas2.3.3-pydantic2.12.3 34.04% <100.00%> (+<0.01%) ⬆️
unit-tests-io-Linux-py3.12-pandas3.0.0-pydantic2.12.3 34.01% <100.00%> (+0.01%) ⬆️
unit-tests-io-Linux-py3.13-pandas2.3.3-pydantic2.12.3 34.04% <100.00%> (+<0.01%) ⬆️
unit-tests-io-Linux-py3.13-pandas3.0.0-pydantic2.12.3 34.01% <100.00%> (+0.01%) ⬆️
unit-tests-io-Linux-py3.14-pandas2.3.3-pydantic2.12.3 33.95% <100.00%> (+<0.01%) ⬆️
unit-tests-io-Linux-py3.14-pandas3.0.0-pydantic2.12.3 33.92% <100.00%> (+0.01%) ⬆️
unit-tests-io-Windows-py3.10-pandas2.3.3-pydantic2.12.3 33.84% <100.00%> (-0.26%) ⬇️
unit-tests-io-Windows-py3.11-pandas3.0.0-pydantic2.12.3 33.81% <100.00%> (+0.01%) ⬆️
unit-tests-io-Windows-py3.12-pandas2.3.3-pydantic2.12.3 33.84% <100.00%> (-0.26%) ⬇️
unit-tests-io-Windows-py3.12-pandas3.0.0-pydantic2.12.3 33.81% <100.00%> (-0.26%) ⬇️
unit-tests-io-Windows-py3.13-pandas2.3.3-pydantic2.12.3 33.84% <100.00%> (-0.26%) ⬇️
unit-tests-io-Windows-py3.13-pandas3.0.0-pydantic2.12.3 33.81% <100.00%> (-0.26%) ⬇️
unit-tests-io-Windows-py3.14-pandas2.3.3-pydantic2.12.3 33.75% <100.00%> (-0.26%) ⬇️
unit-tests-io-Windows-py3.14-pandas3.0.0-pydantic2.12.3 33.71% <100.00%> (-0.26%) ⬇️
unit-tests-io-macOS-py3.10-pandas2.3.3-pydantic2.12.3 34.00% <100.00%> (-0.26%) ⬇️
unit-tests-io-macOS-py3.11-pandas2.3.3-pydantic2.12.3 34.00% <100.00%> (-0.26%) ⬇️
unit-tests-io-macOS-py3.11-pandas3.0.0-pydantic2.12.3 33.97% <100.00%> (-0.26%) ⬇️
unit-tests-io-macOS-py3.12-pandas2.3.3-pydantic2.12.3 34.00% <100.00%> (-0.26%) ⬇️
unit-tests-io-macOS-py3.12-pandas3.0.0-pydantic2.12.3 33.97% <100.00%> (-0.26%) ⬇️
unit-tests-io-macOS-py3.13-pandas2.3.3-pydantic2.12.3 34.00% <100.00%> (-0.26%) ⬇️
unit-tests-io-macOS-py3.14-pandas2.3.3-pydantic2.12.3 33.90% <100.00%> (-0.26%) ⬇️
unit-tests-io-macOS-py3.14-pandas3.0.0-pydantic2.12.3 33.87% <100.00%> (-0.26%) ⬇️
unit-tests-pandas-Linux-py3.10-pandas2.3.3-pydantic1.10.11 41.83% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.10-pandas2.3.3-pydantic2.12.3 42.00% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.11-pandas2.3.3-pydantic1.10.11 41.83% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.11-pandas2.3.3-pydantic2.12.3 42.00% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.11-pandas3.0.0-pydantic1.10.11 41.83% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.11-pandas3.0.0-pydantic2.12.3 42.00% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.12-pandas2.3.3-pydantic1.10.11 41.82% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.12-pandas2.3.3-pydantic2.12.3 41.99% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.12-pandas3.0.0-pydantic1.10.11 41.82% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-Linux-py3.12-pandas3.0.0-pydantic2.12.3 42.00% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.13-pandas2.3.3-pydantic1.10.11 41.82% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.13-pandas2.3.3-pydantic2.12.3 41.99% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.13-pandas3.0.0-pydantic1.10.11 41.82% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.13-pandas3.0.0-pydantic2.12.3 42.00% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-Linux-py3.14-pandas2.3.3-pydantic2.12.3 41.94% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Linux-py3.14-pandas3.0.0-pydantic2.12.3 41.94% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.10-pandas2.3.3-pydantic1.10.11 41.76% <50.00%> (-0.04%) ⬇️
unit-tests-pandas-Windows-py3.10-pandas2.3.3-pydantic2.12.3 41.94% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-Windows-py3.11-pandas2.3.3-pydantic1.10.11 41.76% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.11-pandas3.0.0-pydantic1.10.11 41.77% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.11-pandas3.0.0-pydantic2.12.3 41.94% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-Windows-py3.12-pandas2.3.3-pydantic1.10.11 41.75% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-Windows-py3.12-pandas2.3.3-pydantic2.12.3 41.93% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.12-pandas3.0.0-pydantic1.10.11 41.76% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.12-pandas3.0.0-pydantic2.12.3 41.93% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.13-pandas2.3.3-pydantic1.10.11 41.75% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.13-pandas2.3.3-pydantic2.12.3 41.93% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-Windows-py3.13-pandas3.0.0-pydantic1.10.11 41.76% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.13-pandas3.0.0-pydantic2.12.3 41.93% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-Windows-py3.14-pandas2.3.3-pydantic2.12.3 41.87% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-Windows-py3.14-pandas3.0.0-pydantic2.12.3 41.88% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.10-pandas2.3.3-pydantic2.12.3 41.95% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.11-pandas2.3.3-pydantic1.10.11 41.78% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-macOS-py3.11-pandas2.3.3-pydantic2.12.3 41.95% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.11-pandas3.0.0-pydantic1.10.11 41.78% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.11-pandas3.0.0-pydantic2.12.3 41.96% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-macOS-py3.12-pandas2.3.3-pydantic1.10.11 41.77% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.12-pandas2.3.3-pydantic2.12.3 41.95% <50.00%> (-0.04%) ⬇️
unit-tests-pandas-macOS-py3.12-pandas3.0.0-pydantic1.10.11 41.78% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.12-pandas3.0.0-pydantic2.12.3 41.95% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.13-pandas2.3.3-pydantic1.10.11 41.77% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.13-pandas2.3.3-pydantic2.12.3 41.95% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-macOS-py3.13-pandas3.0.0-pydantic1.10.11 41.78% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-macOS-py3.13-pandas3.0.0-pydantic2.12.3 41.95% <50.00%> (+<0.01%) ⬆️
unit-tests-pandas-macOS-py3.14-pandas2.3.3-pydantic2.12.3 41.89% <50.00%> (+0.01%) ⬆️
unit-tests-pandas-macOS-py3.14-pandas3.0.0-pydantic2.12.3 41.90% <50.00%> (+<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.

Pandera sets PYARROW_IGNORE_TIMEZONE=1 on import (for pyspark). On
pandas 2.x, constructing a Series with dtype=timestamp[..., tz=...][pyarrow]
then treats the wall-clock time as UTC and converts it into the target
zone, so 12:30+05:30 becomes 18:00+05:30. The original schema failed
before any YAML/JSON round trip; the UTC cases hid this because a zero
offset is unchanged.

Build the fixture with Series(...).astype(arrow_dtype) so the instant
is preserved on pandas 2.x and 3.x. Serialization of the check
statistics was already correct.

Assisted-by: Grok Bot (AI assistant)
Signed-off-by: adityashankarv <143811563+adityashankarv@users.noreply.github.com>
@adityashankarv

Copy link
Copy Markdown
Author

The only red check on the latest run is Unit Tests Pandas: python-3.10 macos-latest (extra-pandas, pandas-2.3.3, pydantic-1.10.11), which was cancelled after sitting in the queue without ever getting a runner (no steps or logs). Every other job passed, including the same Python/pandas/pydantic combination on Ubuntu and Windows. Could a maintainer rerun that job when convenient? https://github.com/unionai-oss/pandera/actions/runs/45837248665 Thanks!

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.

1 participant