fix: raise ValueError for an out-of-range epoch in to_datetime - #5951
Open
eeshsaxena wants to merge 1 commit into
Open
fix: raise ValueError for an out-of-range epoch in to_datetime#5951eeshsaxena wants to merge 1 commit into
eeshsaxena wants to merge 1 commit into
Conversation
to_datetime parses number-like values as millisecond epochs via
datetime.fromtimestamp(epoch / 1000). A non-finite value ('inf', '-inf') or one
large enough to be out of range (1e30, a huge millis integer) passes float()
but overflows fromtimestamp, leaking OverflowError or OSError. The function
documents ValueError as its failure mode (and to_timestamp / to_date wrap it),
so catch those and fall through to the existing ValueError.
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.
to_datetime(and thereforeto_timestamp/to_date) leaksOverflowError/OSErroron a number-like value that is out of range, instead of theValueErrorit documents:In the number branch, the value passes
float()(float("inf")and large floats do not raise),strptimefails, anddatetime.fromtimestamp(epoch / 1000.0)then overflows. OnlyValueErrorwas expected there, soOverflowError/OSErrorescape. The docstring states 'Raises: ValueError if value cannot be converted to a datetime', and"nan"already raisesValueErrorvia the same path, so this makes the out-of-range cases consistent.The fix catches
OverflowError/OSError/ValueErroraroundfromtimestampand falls through to the existingraise ValueError(...). Added a parametrized regression test intests/utils/test_date.py.