pydata / pydata/xarray

ds.coords.dims can display dims not present in the coordinate vars

Open
#9,466 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

This (on main) looks wrong to me:

In [1]: import xarray as xr

In [2]: ds = xr.Dataset({'a': ('x', [0, 1])})

In [3]: ds
Out[3]: 
<xarray.Dataset> Size: 16B
Dimensions:  (x: 2)
Dimensions without coordinates: x
Data variables:
    a        (x) int64 16B 0 1

In [4]: ds.coords
Out[4]: 
Coordinates:
    *empty*

In [5]: ds.coords.dims
Out[5]: FrozenMappingWarningOnValuesAccess({'x': 2})

I mean the fact no-one has raised this before means it probably isn't of much consequence, but it does seem incorrect / misleading.

Originally posted by @TomNicholas in https://github.com/pydata/xarray/pull/9451#discussion_r1749072922

The issue here being that the DatasetCoordinates object displays a dim with no associated variable.

I'm not sure whether anything about the concept of "virtual coordinate variables" affects this.

(The FrozenMappingWarningOnValuesAccess is just a Mapping, it's only weird due to the ongoing deprecation cycle started in #8500.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example with an xarray Dataset and inspect the DatasetCoordinates object, especially its dims mapping. Read the surrounding coordinate and virtual-coordinate handling before deciding the expected behavior; done means the displayed dimensions accurately reflect the coordinate variables and existing relevant tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.