Skip to content

Commit 56e3f65

Browse files
committed
Add fix: deduplicate columns
1 parent d8760b7 commit 56e3f65

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

xarray/core/dataset.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7234,6 +7234,14 @@ def _to_dataframe(self, ordered_dims: Mapping[Any, int]):
72347234
0
72357235
]
72367236
broadcasted_df = broadcasted_df.join(extension_array_df)
7237+
# remove columns also represented as levels of the MultiIndex. This is necessary
7238+
# because otherwise 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+
]
72377245
return broadcasted_df[columns_in_order]
72387246

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

0 commit comments

Comments
 (0)