Fix object array infer string perf 11470 - #11484
Open
JJFlorian wants to merge 6 commits into
Open
Conversation
dcherian
reviewed
Jul 30, 2026
| result = pd.to_datetime(values.ravel()).to_numpy().reshape(values.shape) | ||
| elif inferred == "timedelta": | ||
| result = pd.to_timedelta(values.ravel()).to_numpy().reshape(values.shape) | ||
| elif inferred in ["datetime64", "timedelta64"]: |
Contributor
There was a problem hiding this comment.
let's add a comment about infer_dtype dropping units
Contributor
Author
There was a problem hiding this comment.
Hm, I would say the cast drops them
JJFlorian
marked this pull request as ready for review
July 31, 2026 07:09
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.
Description
As discussed in #11470, this PR optimizes the
_possibly_convert_objectsfunction. It usespd.api.types.infer_dtypeto classify the values, then converts using a targetedpd.to_datetime/pd.to_timedeltacall instead of the genericpd.Seriesroundtrip where safe to do so. This is only the case forstring,datetime, andtimedelta.datetime64andtimedelta64get tricky becausepd.to_datetime/pd.to_timedeltare-infer units rather than preserving the input's existing unit. So for these 2 cases, and all other return types of theinfer_dtype, I still use thepd.Seriesroundtrip for now.Is there a way to cast these 2 datatypes while preserving their units?
I added 3 new cases to
test_as_compatible_data_writeablecovering the datetime and timedelta fast paths. Without them, an implementation that returns thepd.to_datetime/pd.to_timedeltaresult directly (bypassing the existing writeable fix) would still pass the full test suite, but would silently return read only arrays instead of the writeable arrays xarray currently guarantees.Results
With the example from the issue, we now get:
Checklist
objectdtype very slow when Pandasfuture.infer_stringis enabled #11470AI Disclosure