(fix): ndim accessible as np.ndim on PandasExtensionArray
#10414
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.
Reproducer for the underlying bug:
venv_13/lib/python3.13/site-packages/legacy_api_wrap/__init__.py:82: in fn_compatible return fn(*args_all, **kw) src/anndata/_core/anndata.py:1433: in to_memory new[attr_name] = to_memory(attr, copy=copy) /opt/homebrew/Cellar/[email protected]/3.13.0_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/functools.py:929: in wrapper return dispatch(args[0].__class__)(*args, **kw) src/anndata/_core/file_backing.py:168: in _ return x.to_memory(copy=copy) src/anndata/_core/xarray.py:203: in to_memory df = self.ds.to_dataframe() venv_13/lib/python3.13/site-packages/xarray/core/dataset.py:7164: in to_dataframe return self._to_dataframe(ordered_dims=ordered_dims) venv_13/lib/python3.13/site-packages/xarray/core/dataset.py:7086: in _to_dataframe if not is_extension_array_dtype(self.variables[k].data) venv_13/lib/python3.13/site-packages/xarray/core/variable.py:416: in data duck_array = self._data.get_duck_array() venv_13/lib/python3.13/site-packages/xarray/core/indexing.py:662: in get_duck_array return _wrap_numpy_scalars(array) venv_13/lib/python3.13/site-packages/xarray/core/indexing.py:772: in _wrap_numpy_scalars if np.ndim(array) == 0 and ( _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = PandasExtensionArray(array=['k', 'v', 'l', 'J', 't', ..., 'H', 'u', 'd', 'b', 'h'] Length: 1000 Categories (52, object): ['A', 'B', 'C', 'D', ..., 'w', 'x', 'y', 'z']), func = <function ndim at 0x101dfbe30>, types = (<class 'xarray.core.extension_array.PandasExtensionArray'>,) args = (['k', 'v', 'l', 'J', 't', ..., 'H', 'u', 'd', 'b', 'h'] Length: 1000 Categories (52, object): ['A', 'B', 'C', 'D', ..., 'w', 'x', 'y', 'z'],), kwargs = {} def __array_function__(self, func, types, args, kwargs): def replace_duck_with_extension_array(args) -> list: args_as_list = list(args) for index, value in enumerate(args_as_list): if isinstance(value, PandasExtensionArray): args_as_list[index] = value.array elif isinstance( value, tuple ): # should handle more than just tuple? iterable? args_as_list[index] = tuple( replace_duck_with_extension_array(value) ) elif isinstance(value, list): args_as_list[index] = replace_duck_with_extension_array(value) return args_as_list args = tuple(replace_duck_with_extension_array(args)) if func not in HANDLED_EXTENSION_ARRAY_FUNCTIONS: > raise KeyError("Function not registered for pandas extension arrays.") E KeyError: 'Function not registered for pandas extension arrays.'Since #8821 we need
ndimaccessible vianp.ndimand since https://github.com/pydata/xarray/pull/10317/files#diff-c803294f5216cbbdffa30f0b0c9f16a7e39855d4dd309c88d654bc317a78adc0L103-L104 we are not randomly falling back on whatever pandas offers. Hence this fix to explicitly addndimto thenumpy-registered functions.whats-new.rstapi.rst