Fix bug where numpy datetime contained in Python list gets converted to integer #5415
+32
−2
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.
Closes #3957, closes #5355
Fixes an issue where numpy datetimes with nanosecond precision, contained in a Python list and passed to a
gotrace constructor, were treated as integers rather than dates, and displayed on a numerical axis rather than a date axis.This is more severe than it seems at first glance, since pandas
Timestampobjects (which are represented by the numpydatetime64datatype under the hood) use nanosecond precision by default, even when not required, so it is very easy to create a nanosecond precision datetime without intending to.The issue
Before this fix, the following code produces the incorrect result below. The x-axis should be a date axis, but instead it shows up as a numeric axis with very large numbers.
The fix
These nanosecond-precision datetimes are converted to ints during the coercion step; specifically by a call to the numpy
.item()function which converts a single value from a numpy type to a built-in Python type. Fordatetime64types with microsecond precision or less, a call to.item()returns a Pandasdatetimetype. But since the Pandas datetime does not support any precision greater than microseconds, calling.item()on adatetime64with nanosecond precision or greater simply returns a large integer.This PR resolves the issue by adding an additional check to verify whether the value is a
datetime64with nanosecond precision, and if so, casts it to microsecond precision before calling.item().To test
Run the code snippet given under "The issue" above. On this branch, it should produce a chart with the x-axis being a date axis:
