Dataset Index not included as DataFrame column in `._to_dataframe()` when name different from dimension name
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 72/100
Direzione di ricerca
Inizia eseguendo l’esempio minimo e analizza Dataset.to_dataframe(), concentrandoti su come viene gestito un indice il cui nome differisce dalla relativa dimensione. Il lavoro è completato quando il DataFrame risultante include la coordinata pf come colonna, mantenendo al contempo l’output esistente dell’esempio.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
What happened?
The .to_dataframe function describes in the documentation "Other coordinates are included as columns in the DataFrame.".
When applying the function on a Dataset that contains an index that is not the same 'name' as the corresponding dimension, the coordinate is not included in the resulting Pandas DataFrame. E.g.
import xarray as xr
import pandas as pd
import numpy as np
ds_temp = xr.Dataset(data_vars=dict(temp=(["time", "pos"], np.array([[5, 10, 15, 20, 25]]))), coords=dict(pf=("pos", [1., 2., 4.2, 8., 10.]), time=("time", [pd.to_datetime("2025-01-01")]))).set_xindex("pf")
The example Dataset looks like
<xarray.Dataset> Size: 88B
Dimensions: (time: 1, pos: 5)
Coordinates:
* time (time) datetime64[ns] 8B 2025-01-01
* pf (pos) float64 40B 1.0 2.0 4.2 8.0 10.0
Dimensions without coordinates: pos
Data variables:
temp (time, pos) int64 40B 5 10 15 20 25
Converting the Dataset to a Pandas DataFrame:
ds_temp.to_dataframe()
The resulting DataFrame is missing the pf coordinate in the returned DataFrame:
temp
time pos
2025-01-01 0 5
1 10
2 15
3 20
4 25
Dropping the index and applying to_dataframe does actually include the respective coords in the DataFrame:
>>> ds_temp.drop_indexes("pf").to_dataframe()
temp pf
time pos
2025-01-01 0 5 1.0
1 10 2.0
2 15 4.2
3 20 8.0
4 25 10.0
This behavior changed in between recent release as in version 2025.1.2 the column was included. I assume this change results from the support for ExtensionArray.
What did you expect to happen?
An index that has not the same name as the dimension is also included in the resulting DataFrame, in the case of the example having pf in the final DataFrame.
Minimal Complete Verifiable Example
import xarray as xr
import pandas as pd
import numpy as np
xr.show_versions()
ds_temp = xr.Dataset(data_vars=dict(temp=(["time", "pos"], np.array([[5, 10, 15, 20, 25]]))), coords=dict(pf=("pos", [1., 2., 4.2, 8., 10.]), time=("time", [pd.to_datetime("2025-01-01")]))).set_xindex("pf")
df = ds_temp.to_dataframe()
assert "pf" in df.columns
Steps to reproduce
The resulting DataFrame lacks the pf as a column
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?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.11.2 (main, Nov 30 2024, 21:22:50) [GCC 12.2.0]
python-bits: 64
OS: Linux
OS-release: 6.1.0-37-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.2
libnetcdf: 4.9.4-development
xarray: 2025.10.1
pandas: 2.3.3
numpy: 2.2.3
scipy: 1.15.2
netCDF4: 1.7.2
pydap: None
h5netcdf: None
h5py: None
zarr: 3.0.4
cftime: 1.6.4.post1
nc_time_axis: None
iris: None
bottleneck: None
dask: 2025.2.0
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: 2025.2.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 75.8.0
pip: 25.0.1
conda: None
pytest: 8.3.5
mypy: 1.15.0
IPython: 9.0.1
sphinx: 8.2.3
- Lingua principale
- Python
- Stelle
- 4.2k
- Fork
- 1.5k
- Merge medio
- 2g 20h
- PR unite (30g)
- 70
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pydata/xarray
-
xarray removes axis grid from pcolormeshForse già presa @Sealdot l’ha presa 1 giorno fa. Apertabug needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 1 giorno
-
`DataTree.map_over_datasets` behavior can be obscure sometimesForse già presa @mohansree14 l’ha presa 24 giorni fa. Apertaneeds triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
pydata/xarray#11813 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Comparing attrs that contain a dict of NumPy arrays raises ValueError in identical / assert_identicalForse già presa @SohamXYZDev l’ha presa 30 giorni fa. Apertabug needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 73/100
pydata/xarray#11777 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
RangeIndex` with datetime64 bounds cannot be printed (`Unknown format code 'g'`)Forse già presa @d-chambers l’ha presa 61 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
pydata/xarray#11598 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
DataTree constructor error message says dict even though dict is not acceptedForse già presa @aryansk l’ha presa 100 giorni fa. Apertabug contrib-good-first-issue topic-DataTree topic-error reporting
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
pydata/xarray#11514 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di pydata/xarray
Issue simili
-
LLMContextNode tool builders fail on nested params like `params:retrieval.max_matches`Forse già presa @Shizoqua l’ha presa oggi. ApertaCommunity
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 2 giorni
-
security
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
btclib-org/portanode#985 ·
I maintainer di solito rispondono entro 1 giorno
-
[Bug]: maps skill OSRM foot/cycling returns car times (router.project-osrm.org ignores profile)Apertacomp/tools P2 tool/skills type/bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
NousResearch/hermes-agent#158494 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
needs-format
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
Niko1221/Strata#6475 · 1 commento ·
I maintainer di solito rispondono entro 6 giorni