Repository navigation
Conversation
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]>
There was a problem hiding this comment.
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.
|
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. |
Description
With pandas 3,
write_jsonfails on anydate32ordate64column with default arguments (NotImplementedError: as_unit not implemented for date32[day]), soread_parquet(...).write_json(...)breaks on a Parquet date column. Since 2.56,to_pandas()returnspd.ArrowDtypecolumns, and pandas 3'sto_jsoncalls.dt.as_uniton 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_unitbeforeto_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 andorient="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
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 oftest_json.pypasses on pandas 2.3.3; on 3.0.6, 6 tests fail on unmodified master too.write_jsoncases (nulls, 0 rows, multiple chunks, eachdate_unitandorient) 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)