From 18c553dd2bf089503948377ff123a1c96f9f2c1d Mon Sep 17 00:00:00 2001 From: Yuvraj Lamba Date: Tue, 24 Nov 2026 14:29:01 +0530 Subject: [PATCH 1/2] fix(to_dataframe): preserve indexed coordinates whose name differs from dimension (#10851) --- doc/whats-new.rst | 3 +++ xarray/core/dataset.py | 10 +++++++--- xarray/tests/test_dataset.py | 25 +++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index c7f888d77f5..b5c7a874ec9 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -223,6 +223,9 @@ Deprecations Bug Fixes ~~~~~~~~~ +- Preserve indexed coordinates whose name differs from their dimension as + columns in :py:meth:`Dataset.to_dataframe` (:issue:`10851`). + By `Yuvraj Lamba `_. - Weighted variance, standard deviation and sums of squared deviations now use conjugated deviations for complex data, matching unweighted reductions and correcting weighted self-correlation. diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index f02751a2ce1..6e5124251dc 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -7632,9 +7632,13 @@ def _to_dataframe(self, ordered_dims: Mapping[Any, int]): from xarray.core.extension_array import PandasExtensionArray # All and only non-index arrays (whether data or coordinates) should - # become columns in the output DataFrame. Excluding indexes rather - # than dims handles the case of a MultiIndex along a single dimension. - columns_in_order = [k for k in self.variables if k not in self.xindexes] + # become columns in the output DataFrame. Excluding only the dimensions + # and index levels ensures that indexed coordinates whose name differs + # from their dimension are preserved as columns (GH10851). + index_names = set(ordered_dims) | set( + self.coords.to_index([*ordered_dims]).names + ) + columns_in_order = [k for k in self.variables if k not in index_names] non_extension_array_columns = [ k for k in columns_in_order diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 0c76e8456a3..94954f9aa16 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -5659,6 +5659,31 @@ def test_to_and_from_dataframe(self) -> None: expected = pd.DataFrame([[]], index=idx) assert expected.equals(actual), (expected, actual) + def test_to_dataframe_indexed_coord_differing_name(self) -> None: + # GH10851: Index coordinates whose name differs from dimension name + # should still be included in DataFrame columns. + ds = Dataset( + data_vars={"temp": (["time", "pos"], np.array([[5, 10, 15, 20, 25]]))}, + coords={ + "pf": ("pos", [1.0, 2.0, 4.2, 8.0, 10.0]), + "time": ("time", [pd.to_datetime("2025-01-01")]), + }, + ).set_xindex("pf") + + df = ds.to_dataframe() + assert "pf" in df.columns + assert "temp" in df.columns + np.testing.assert_array_equal(df["pf"].values, [1.0, 2.0, 4.2, 8.0, 10.0]) + + # Verify dimension index coordinate is still excluded from columns + ds_dim = Dataset( + data_vars={"temp": (["x"], [1, 2, 3])}, + coords={"x": ("x", [10, 20, 30])}, + ) + df_dim = ds_dim.to_dataframe() + assert "temp" in df_dim.columns + assert "x" not in df_dim.columns + @requires_sparse def test_to_dataframe_sparse_crash_regression(self) -> None: # `Dataset.to_dataframe()` used to unconditionally call `.values` on From d65204d01ed0bbd9b3ef14f0c8ec0a3195eaeb55 Mon Sep 17 00:00:00 2001 From: Yuvraj Lamba Date: Tue, 24 Nov 2026 14:32:01 +0530 Subject: [PATCH 2/2] ci: add Lamba to allowed words in typos config --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 77699edc40e..680ce69dfa4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -474,6 +474,7 @@ Noice = "Noice" Hax = "Hax" Yau = "Yau" whn = "whn" +Lamba = "Lamba" # Tests Ome = "Ome"