Repository navigation
fix(io): round-trip pyarrow timestamp and duration check statistics - #2707
Open
adityashankarv wants to merge 2 commits into
Open
adityashankarv wants to merge 2 commits into
adityashankarv wants to merge 2 commits into
Conversation
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>
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>
Author
|
The only red check on the latest run is |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #2697 (issue #2696), as agreed with @maoji-y ("yes, please go ahead with the
_deserialize_check_statsfollow-up, it's yours").#2697 made a schema with a
timestamp[us][pyarrow]column load back fromto_yaml/to_json.Check values on those columns still did not survive serialization:
Same for
in_range, tz-aware timestamps andTimedeltabounds onduration[...][pyarrow].Cause:
_serialize_check_stats/_deserialize_check_statsinpandera/io/pandas_io.pyonly recognised the numpy-backed
pandas_engine.DateTime/Timedeltadtypes, so thepyarrow-backed
ArrowTimestamp/ArrowDurationfell through and the rawTimestamp/Timedeltareached the yaml/json writer.Fix (16 lines in
pandas_io.py): treatArrowTimestamplikeDateTimefor the existingdatetime comparison checks (ISO 8601 strings: keeps sub-second precision and the UTC offset,
same encoding as #2613) and
ArrowDurationlikeTimedelta(integer nanoseconds). Thepyarrow classes are looked up with
getattr(..., ())because they only exist when pyarrow isinstalled.
Out of scope (left unchanged on purpose):
date32/date64bounds. yaml already round-tripsthem; json fails the same way for the native
pandera.Datedtype, which is a separatelimitation 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,minimalon/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+1validate 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 xfailedruff check,ruff format --check,codespellclean;mypyreports 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.