-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
BUG: Fixed DataFrame.combine with non-unique columns #62760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
5e4a066
dcac1a3
6507206
74fd8b5
36ed5ec
3c23ac7
7b3045b
b553d69
51a6455
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,7 +194,10 @@ | |
| nargsort, | ||
| ) | ||
|
|
||
| from pandas.io.common import get_handle | ||
| from pandas.io.common import ( | ||
| dedup_names, | ||
| get_handle, | ||
| ) | ||
| from pandas.io.formats import ( | ||
| console, | ||
| format as fmt, | ||
|
|
@@ -9093,7 +9096,7 @@ def combine( | |
| 2 NaN 3.0 1.0 | ||
| """ | ||
| other_idxlen = len(other.index) # save for compare | ||
| other_columns = other.columns | ||
| self_columns, other_columns = self.columns, other.columns | ||
|
|
||
| this, other = self.align(other) | ||
| new_index = this.index | ||
|
|
@@ -9104,11 +9107,26 @@ def combine( | |
| if self.empty and len(other) == other_idxlen: | ||
| return other.copy() | ||
|
|
||
| new_columns_out = self.columns.union(other_columns, sort=False) | ||
| # Deduplicate column names if necessary | ||
| self_columns = Index( | ||
| dedup_names(list(self_columns), False), dtype=self_columns.dtype | ||
| ) | ||
| other_columns = Index( | ||
| dedup_names(list(other_columns), False), dtype=other_columns.dtype | ||
| ) | ||
| this.columns = Index( | ||
|
||
| dedup_names(list(this.columns), False), dtype=this.columns.dtype | ||
| ) | ||
| other.columns = Index( | ||
| dedup_names(list(other.columns), False), dtype=other.columns.dtype | ||
| ) | ||
|
|
||
| # preserve column order | ||
| new_columns = self.columns.union(other_columns, sort=False) | ||
| new_columns_unique = self_columns.union(other_columns, sort=False) | ||
| do_fill = fill_value is not None | ||
| result = {} | ||
| for col in new_columns: | ||
| for col in new_columns_unique: | ||
| series = this[col] | ||
|
||
| other_series = other[col] | ||
|
|
||
|
|
@@ -9158,7 +9176,9 @@ def combine( | |
| result[col] = arr | ||
|
|
||
| # convert_objects just in case | ||
| frame_result = self._constructor(result, index=new_index, columns=new_columns) | ||
| frame_result = self._constructor( | ||
| result, index=new_index, columns=new_columns_out | ||
| ) | ||
| return frame_result.__finalize__(self, method="combine") | ||
|
|
||
| def combine_first(self, other: DataFrame) -> DataFrame: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why would dedup_names be necessay here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having unique column names would preserve the original logic of using column names. Switching to indices would require multiple indices.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
huh?