Skip to content

[Data] Fix write_json on Arrow-backed date columns with pandas 3 - #68619

Open
rayo5o wants to merge 1 commit into
ray-project:masterfrom
rayo5o:fix-write-json-date-pandas3
Open

rayo5o wants to merge 1 commit into
ray-project:masterfrom
rayo5o:fix-write-json-date-pandas3

Conversation

@rayo5o

@rayo5o rayo5o commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Description

With pandas 3, write_json fails on any date32 or date64 column with default arguments (NotImplementedError: as_unit not implemented for date32[day]), so read_parquet(...).write_json(...) breaks on a Parquet date column. Since 2.56, to_pandas() returns pd.ArrowDtype columns, and pandas 3's to_json calls .dt.as_unit on them for epoch output, which Arrow date types don't support. CI pins pandas 2.3.3, so it didn't show up there.

This casts those columns to integers in date_unit before to_json, only for epoch output. The values are what pandas 2 writes, so pandas 2 output is unchanged except for dates outside the nanosecond range, which used to overflow and are now written. ISO output and orient="table" are untouched.

The root cause is in pandas, but Ray has no pandas upper bound, so 3.0.x users would still hit it. The deprecation of date_format="epoch" in pandas 3 is left for a separate change.

Related issues

Closes #68089

Additional information

  • Not a duplicate: no other issue or PR covers date columns. [Data] Fix write_json for non-nanosecond timestamps and durations #68007 changed the same function for non-nanosecond timestamps and durations on pandas < 3; this is rebased on it.
  • Tests: the new test_json_write_date_columns (4 cases) passes on pandas 2.3.3 and 3.0.6 and fails on both without the fix. The rest of test_json.py passes on pandas 2.3.3; on 3.0.6, 6 tests fail on unmodified master too.
  • I also compared 660 write_json cases (nulls, 0 rows, multiple chunks, each date_unit and orient) on pandas 3 with this change against pandas 2.3.3. They match, except dates outside the nanosecond range and dates inside list or dictionary columns, which this doesn't touch.
Tests that also fail on unmodified master (pandas 3.0.6)
  • test_json_roundtrip[*-2] (4)
  • TestPandasJSONDatasource::test_read_stream_with_target_output_size_bytes (2)
  • AI assistance was used to prepare this PR.

With pandas 3, to_json(date_format="epoch") calls .dt.as_unit on every
column whose dtype kind is "M". pd.ArrowDtype date32 and date64 columns
report that kind but do not implement as_unit, so write_json fails with
NotImplementedError on any date column, which is what Arrow-backed
to_pandas produces by default since Ray 2.56.

Cast these columns to integers in date_unit before calling to_json. The
values are the ones pandas < 3 writes. Dates after 2262, which overflowed
on pandas < 3, are now written too.

Signed-off-by: rayo5o <[email protected]>
@rayo5o
rayo5o requested review from a team as code owners November 28, 2026 04:55

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a helper function _cast_arrow_dates_to_epoch in json_datasink.py to cast pd.ArrowDtype date32 and date64 columns to integers of a specified unit. This change prevents a NotImplementedError in pandas 3 and avoids overflow issues in older pandas versions when writing epoch values. Additionally, a comprehensive test suite test_json_write_date_columns has been added to verify this behavior across different block formats and date units. I have no feedback to provide as the implementation is clean and well-tested.

@rayo5o

rayo5o commented Nov 28, 2026

Copy link
Copy Markdown
Contributor Author

Reported the pandas side as pandas-dev/pandas#72399, with a fix on https://github.com/rayo5o/pandas/tree/to-json-arrow-date. Even if that's accepted, it would only ship in a future pandas release, and pandas 3.0.6 and 3.1.1 both raise today, so Ray still needs this change.

@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Nov 28, 2026

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

community-contribution Contributed by the community data Ray Data-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Data] write_json fails on date columns with pandas 3

1 participant