Fix JsonlWriter crash on pandas.Timestamp metadata#490
Open
discobot wants to merge 1 commit into
Open
Conversation
orjson serializes datetime objects natively but raises TypeError for their subclasses, so documents with pandas.Timestamp metadata (e.g. timestamp columns read by ParquetReader) crashed JsonlWriter. Pass a default handler to orjson.dumps that converts datetime date/time subclasses to their ISO format, matching orjson's native datetime output. Unknown types still raise TypeError. Adds a regression test writing a document with pandas.Timestamp metadata. Fixes huggingface#325
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.
Fixes #325.
orjson serializes plain
datetime/date/timenatively but deliberately rejects their subclasses, andpandas.Timestamp(whatParquetReaderyields for timestamp columns) is adatetime.datetimesubclass — with nodefault=handler,orjson.dumpsraises. The handler added here emitsisoformat()for date/time subclasses, matching orjson's native datetime output exactly, while other unserializable types still fail loudly (vs. a blanketdefault=str, which would silently stringify anything — happy to switch to that if preferred).The new regression test fails on
mainwith the exactTypeError: Type is not JSON serializable: Timestampfrom the issue and passes with this change.Note
Low Risk
Narrow change to JSON serialization in JsonlWriter with explicit errors for unknown types; no auth, security, or data-path changes beyond fixing a metadata edge case.
Overview
Fixes
JsonlWritercrashing when document metadata containspandas.Timestamp(and otherdate/timesubclasses) by passing adefault=_json_defaulthook toorjson.dumps. Subclasses that orjson won’t serialize natively are written asisoformat()strings, aligned with native datetime output; other non-JSON types still raiseTypeError.Adds a regression test that writes a document with timestamp metadata and asserts the ISO string in the JSONL output, plus a
require_pandasskip decorator for optional test deps.Reviewed by Cursor Bugbot for commit a555fec. Bugbot is set up for automated code reviews on this repo. Configure here.