Skip to content

Commit b553d69

Browse files
committed
Remove dedup_names and use column indices instead of names in DataFrame.combine.
1 parent 7b3045b commit b553d69

File tree

1 file changed

+8
-26
lines changed

1 file changed

+8
-26
lines changed

pandas/core/frame.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@
195195
)
196196

197197
from pandas.io.common import (
198-
dedup_names,
199198
get_handle,
200199
)
201200
from pandas.io.formats import (
@@ -9107,28 +9106,13 @@ def combine(
91079106
if self.empty and len(other) == other_idxlen:
91089107
return other.copy()
91099108

9110-
new_columns_out = self.columns.union(other_columns, sort=False)
9111-
# Deduplicate column names if necessary
9112-
self_columns = Index(
9113-
dedup_names(list(self_columns), False), dtype=self_columns.dtype
9114-
)
9115-
other_columns = Index(
9116-
dedup_names(list(other_columns), False), dtype=other_columns.dtype
9117-
)
9118-
this.columns = Index(
9119-
dedup_names(list(this.columns), False), dtype=this.columns.dtype
9120-
)
9121-
other.columns = Index(
9122-
dedup_names(list(other.columns), False), dtype=other.columns.dtype
9123-
)
9124-
91259109
# preserve column order
9126-
new_columns_unique = self_columns.union(other_columns, sort=False)
9110+
new_columns = self_columns.union(other_columns, sort=False)
91279111
do_fill = fill_value is not None
91289112
result = {}
9129-
for col in new_columns_unique:
9130-
series = this[col]
9131-
other_series = other[col]
9113+
for i in range(this.shape[1]):
9114+
series = this.iloc[:, i]
9115+
other_series = other.iloc[:, i]
91329116

91339117
this_dtype = series.dtype
91349118
other_dtype = other_series.dtype
@@ -9139,7 +9123,7 @@ def combine(
91399123
# don't overwrite columns unnecessarily
91409124
# DO propagate if this column is not in the intersection
91419125
if not overwrite and other_mask.all():
9142-
result[col] = this[col].copy()
9126+
result.iloc[:, i] = this.iloc[:, i].copy()
91439127
continue
91449128

91459129
if do_fill:
@@ -9148,7 +9132,7 @@ def combine(
91489132
series[this_mask] = fill_value
91499133
other_series[other_mask] = fill_value
91509134

9151-
if col not in self.columns:
9135+
if other.columns[i] not in self.columns:
91529136
# If self DataFrame does not have col in other DataFrame,
91539137
# try to promote series, which is all NaN, as other_dtype.
91549138
new_dtype = other_dtype
@@ -9173,12 +9157,10 @@ def combine(
91739157
arr, new_dtype
91749158
)
91759159

9176-
result[col] = arr
9160+
result[new_columns[i]] = arr
91779161

91789162
# convert_objects just in case
9179-
frame_result = self._constructor(
9180-
result, index=new_index, columns=new_columns_out
9181-
)
9163+
frame_result = self._constructor(result, index=new_index, columns=new_columns)
91829164
return frame_result.__finalize__(self, method="combine")
91839165

91849166
def combine_first(self, other: DataFrame) -> DataFrame:

0 commit comments

Comments
 (0)