|
17 | 17 | from pandas.core.dtypes.generic import ABCDataFrame, ABCIndexClass, ABCSeries
|
18 | 18 | from pandas.core.dtypes.missing import isna
|
19 | 19 |
|
| 20 | +from pandas.core import algorithms |
20 | 21 | from pandas.core.construction import extract_array
|
21 | 22 | from pandas.core.ops.array_ops import (
|
22 | 23 | arithmetic_op,
|
@@ -562,18 +563,32 @@ def _frame_arith_method_with_reindex(
|
562 | 563 | DataFrame
|
563 | 564 | """
|
564 | 565 | # GH#31623, only operate on shared columns
|
565 |
| - cols = left.columns.intersection(right.columns) |
| 566 | + cols, lcols, rcols = left.columns.join( |
| 567 | + right.columns, how="inner", level=None, return_indexers=True |
| 568 | + ) |
566 | 569 |
|
567 |
| - new_left = left[cols] |
568 |
| - new_right = right[cols] |
| 570 | + new_left = left.iloc[:, lcols] |
| 571 | + new_right = right.iloc[:, rcols] |
569 | 572 | result = op(new_left, new_right)
|
570 | 573 |
|
571 | 574 | # Do the join on the columns instead of using _align_method_FRAME
|
572 | 575 | # to avoid constructing two potentially large/sparse DataFrames
|
573 | 576 | join_columns, _, _ = left.columns.join(
|
574 | 577 | right.columns, how="outer", level=None, return_indexers=True
|
575 | 578 | )
|
576 |
| - return result.reindex(join_columns, axis=1) |
| 579 | + |
| 580 | + if result.columns.has_duplicates: |
| 581 | + # Avoid reindexing with a duplicate axis. |
| 582 | + # https://github.com/pandas-dev/pandas/issues/35194 |
| 583 | + indexer, _ = result.columns.get_indexer_non_unique(join_columns) |
| 584 | + indexer = algorithms.unique1d(indexer) |
| 585 | + result = result._reindex_with_indexers( |
| 586 | + {1: [join_columns, indexer]}, allow_dups=True |
| 587 | + ) |
| 588 | + else: |
| 589 | + result = result.reindex(join_columns, axis=1) |
| 590 | + |
| 591 | + return result |
577 | 592 |
|
578 | 593 |
|
579 | 594 | def _maybe_align_series_as_frame(frame: "DataFrame", series: "Series", axis: int):
|
|
0 commit comments