Optimize object conversion#11475
Open
pixelrahulnotfound wants to merge 2 commits into
Open
Conversation
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
optimise _possibly_convert_objects in xarray/core/variable.py by adding a scan for object arrays.
Here is the logic:
• We scan up the first 10,000(in 99% cases it will usually stop at 1 element and if an array is very large we do it to cap the limit so it doesnt lag in the scan) elements of the flattened object array.
• If a datetime-like type (such as datetime, np.datetime64, pd.Timestamp, pd.NaT, etc.) is found, we flag it as datetime-like and stop the scan.
• If a non-null, non-datetime element is found, we immediately determine it is not datetime-like and stop the scan.
• If no conclusive type is found in the first 10,000 elements (e.g., all elements are null) and the array is larger, we fall back to pandas' highly optimized C-compiled pd.api.types.infer_dtype on the entire array.
Checklist
whats-new.rstapi.rstTests
• python -m pytest -o addopts="" xarray/tests/test_possibly_convert_objects.py — 11
passed
• ruff check xarray/core/variable.py xarray/tests/test_possibly_convert_objects.py —
passed
• git diff --check — passed
AI Disclosure
[✓] This PR contains AI-generated content.
[✓] I have tested any AI-generated content in my PR.
[✓] I take responsibility for any AI-generated content in my PR.
Tools: Google Antigravity.