Skip to content

Commit e3603e4

Browse files
jbrockmendelYYYasin19
authored andcommitted
REF: make indicator_name a cache_readonly (pandas-dev#48040)
1 parent 0892fa4 commit e3603e4

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

pandas/core/reshape/merge.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from pandas.util._decorators import (
3636
Appender,
3737
Substitution,
38+
cache_readonly,
3839
)
3940
from pandas.util._exceptions import find_stack_level
4041

@@ -651,16 +652,6 @@ def __init__(
651652

652653
self.indicator = indicator
653654

654-
self.indicator_name: str | None
655-
if isinstance(self.indicator, str):
656-
self.indicator_name = self.indicator
657-
elif isinstance(self.indicator, bool):
658-
self.indicator_name = "_merge" if self.indicator else None
659-
else:
660-
raise ValueError(
661-
"indicator option can only accept boolean or string arguments"
662-
)
663-
664655
if not is_bool(left_index):
665656
raise ValueError(
666657
f"left_index parameter must be of type bool, not {type(left_index)}"
@@ -753,6 +744,17 @@ def _maybe_drop_cross_column(
753744
if cross_col is not None:
754745
del result[cross_col]
755746

747+
@cache_readonly
748+
def _indicator_name(self) -> str | None:
749+
if isinstance(self.indicator, str):
750+
return self.indicator
751+
elif isinstance(self.indicator, bool):
752+
return "_merge" if self.indicator else None
753+
else:
754+
raise ValueError(
755+
"indicator option can only accept boolean or string arguments"
756+
)
757+
756758
def _indicator_pre_merge(
757759
self, left: DataFrame, right: DataFrame
758760
) -> tuple[DataFrame, DataFrame]:
@@ -765,7 +767,7 @@ def _indicator_pre_merge(
765767
"Cannot use `indicator=True` option when "
766768
f"data contains a column named {i}"
767769
)
768-
if self.indicator_name in columns:
770+
if self._indicator_name in columns:
769771
raise ValueError(
770772
"Cannot use name of an existing column for indicator column"
771773
)
@@ -786,13 +788,13 @@ def _indicator_post_merge(self, result: DataFrame) -> DataFrame:
786788
result["_left_indicator"] = result["_left_indicator"].fillna(0)
787789
result["_right_indicator"] = result["_right_indicator"].fillna(0)
788790

789-
result[self.indicator_name] = Categorical(
791+
result[self._indicator_name] = Categorical(
790792
(result["_left_indicator"] + result["_right_indicator"]),
791793
categories=[1, 2, 3],
792794
)
793-
result[self.indicator_name] = result[self.indicator_name].cat.rename_categories(
794-
["left_only", "right_only", "both"]
795-
)
795+
result[self._indicator_name] = result[
796+
self._indicator_name
797+
].cat.rename_categories(["left_only", "right_only", "both"])
796798

797799
result = result.drop(labels=["_left_indicator", "_right_indicator"], axis=1)
798800
return result

0 commit comments

Comments
 (0)