Fix expand_dims string coordinate dtype inference#11069
Open
garg-khushi wants to merge 3 commits intopydata:mainfrom
Open
Fix expand_dims string coordinate dtype inference#11069garg-khushi wants to merge 3 commits intopydata:mainfrom
garg-khushi wants to merge 3 commits intopydata:mainfrom
Conversation
jsignell
reviewed
Jan 22, 2026
Member
jsignell
left a comment
There was a problem hiding this comment.
Thanks for taking the time to work on this @garg-khushi and sorry it's taken a bit to review. I was wondering if there might be a simpler solution as mentioned in the comments. I tried out the change that I suggested locally and just had to tweak one test:
diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py
index d25ef5a2..6f9edae4 100644
--- a/xarray/tests/test_dataset.py
+++ b/xarray/tests/test_dataset.py
@@ -3860,7 +3860,7 @@ class TestDataset:
# Regression test for https://github.com/pydata/xarray/issues/7493#issuecomment-1953091000
# todo: test still needed?
ds = Dataset().expand_dims({"time": [np.datetime64("2018-01-01", "m")]})
- assert ds.time.dtype == np.dtype("datetime64[s]")
+ assert ds.time.dtype == np.dtype("datetime64[m]")
def test_set_index(self) -> None:
expected = create_test_multiindex()| variables.update(name_and_new_1d_var) | ||
| coord_names.add(k) | ||
| dim[k] = variables[k].size | ||
|
|
Member
There was a problem hiding this comment.
I don't think we need any changes in this file actually.
Comment on lines
675
to
+685
| coord_dtype = get_valid_numpy_dtype(index) | ||
| if coord_dtype == object and index.dtype == object: | ||
| inferred = getattr(index, "inferred_type", None) | ||
| if inferred in ("string", "unicode"): | ||
| coord_dtype = np.dtype(str) | ||
| else: | ||
| data = index.to_numpy(dtype=object, copy=False) | ||
| if data.size and all( | ||
| isinstance(x, (str, np.str_)) for x in data.ravel() | ||
| ): | ||
| coord_dtype = np.asarray(data, dtype=str).dtype |
Member
There was a problem hiding this comment.
I'm not sure if we need to be quite so precise in only targeting objects. This seems to work just as well:
Suggested change
| coord_dtype = get_valid_numpy_dtype(index) | |
| if coord_dtype == object and index.dtype == object: | |
| inferred = getattr(index, "inferred_type", None) | |
| if inferred in ("string", "unicode"): | |
| coord_dtype = np.dtype(str) | |
| else: | |
| data = index.to_numpy(dtype=object, copy=False) | |
| if data.size and all( | |
| isinstance(x, (str, np.str_)) for x in data.ravel() | |
| ): | |
| coord_dtype = np.asarray(data, dtype=str).dtype | |
| coord_dtype = get_valid_numpy_dtype(np.asarray(array)) |
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.
This PR fixes an inconsistency where
expand_dimscreated object-dtypecoordinates for string inputs instead of NumPy unicode dtype.
Changes:
expand_dimsconcatby improvingPandasIndex coord dtype inference
Fixes #11061