-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix expand_dims string coordinate dtype inference #11069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix expand_dims string coordinate dtype inference #11069
Conversation
jsignell
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need any changes in this file actually.
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we need to be quite so precise in only targeting objects. This seems to work just as well:
| 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 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