np.linalg.pinv of a DataArray results in mismatched coordinates
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
What happened?
In general, np.linalg.pinv takes an array of shape (..., n, m) and returns an array of shape (..., m, n). Something isn't recognizing that the last two axes get switched around, so it incorrectly retains the coordinates in the same order.
In the attached example, m1 is an DataArray with shape (3,4) and defined coordinates, and m2 is the result of np.linalg.pinv(m1). m2 has shape (4,3), but maintains the coordinates from m1, which are 3 and 4 in length.
>>> m1
<xarray.DataArray (foo: 3, bar: 4)> Size: 96B
...
Coordinates:
* foo (foo) <U1 12B 'x' 'y' 'z'
* bar (bar) <U1 16B 'a' 'b' 'c' 'd'
>>> m2
<xarray.DataArray (foo: 4, bar: 3)> Size: 96B
...
Coordinates:
* foo (foo) <U1 12B 'x' 'y' 'z'
* bar (bar) <U1 16B 'a' 'b' 'c' 'd'
What did you expect to happen?
That the coordinates of m2 would have the same length as their respective axis's shape. Or at least an error message or warning or something.
Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# "numpy"
# ]
# ///
#
# This script automatically imports the development branch of xarray to check for issues.
# Please delete this header if you have _not_ tested this script with `uv run`!
import xarray as xr
xr.show_versions()
# your reproducer code ...
import numpy as np
m1 = xr.DataArray(np.arange(12).reshape(3,4),
coords={'foo': ['x', 'y', 'z'],
'bar': ['a', 'b', 'c', 'd']})
m2 = np.linalg.pinv(m1)
Steps to reproduce
No response
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
Anything else we need to know?
This probably affects np.linalg.inv too, although less visibly.
Environment
xarray: 2026.4.0
pandas: 3.1.0.dev0+140.gcc9e131258
numpy: 2.5.0
scipy: 1.18.0
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.11.0
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 82.0.1
pip: 26.1.2
conda: None
pytest: 9.1.1
mypy: None
IPython: 9.14.1
sphinx: 9.1.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided MVCE with xarray and NumPy, focusing on the np.linalg.pinv DataArray dispatch path and how the final two dimensions are handled. Add a regression test for a 3×4 DataArray with named coordinates; done means the result has shape 4×3 and coordinates whose lengths match their respective dimensions, or a documented error is raised.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100