Skip to content

Commit ff03b8a

Browse files
committed
Simpler and cleaner fix
1 parent 9734a2b commit ff03b8a

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

xarray/core/dataset.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7183,7 +7183,10 @@ def to_pandas(self) -> pd.Series | pd.DataFrame:
71837183
def _to_dataframe(self, ordered_dims: Mapping[Any, int]):
71847184
from xarray.core.extension_array import PandasExtensionArray
71857185

7186-
columns_in_order = [k for k in self.variables if k not in self.dims]
7186+
# All and only non-index arrays (whether data or coordinates) should
7187+
# become columns in the output DataFrame. Excluding indexes rather
7188+
# than dims handles the case of a MultiIndex along a single dimension.
7189+
columns_in_order = [k for k in self.variables if k not in self.indexes]
71877190
non_extension_array_columns = [
71887191
k
71897192
for k in columns_in_order
@@ -7234,14 +7237,6 @@ def _to_dataframe(self, ordered_dims: Mapping[Any, int]):
72347237
0
72357238
]
72367239
broadcasted_df = broadcasted_df.join(extension_array_df)
7237-
# remove columns also represented as levels of the MultiIndex. This is necessary
7238-
# because MultiIndex levels may otherwise be duplicated as columns and
7239-
# is safe because xarray forbids clashes between Data variable names and coordinate names,
7240-
# so any named multi-index levels that clash with a coordinate-derived column
7241-
# must be derived from that same array.
7242-
columns_in_order = [
7243-
c for c in columns_in_order if c not in broadcasted_df.index.names
7244-
]
72457240
return broadcasted_df[columns_in_order]
72467241

72477242
def to_dataframe(self, dim_order: Sequence[Hashable] | None = None) -> pd.DataFrame:

0 commit comments

Comments
 (0)