35
35
from pandas .util ._decorators import (
36
36
Appender ,
37
37
Substitution ,
38
+ cache_readonly ,
38
39
)
39
40
from pandas .util ._exceptions import find_stack_level
40
41
@@ -651,16 +652,6 @@ def __init__(
651
652
652
653
self .indicator = indicator
653
654
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
-
664
655
if not is_bool (left_index ):
665
656
raise ValueError (
666
657
f"left_index parameter must be of type bool, not { type (left_index )} "
@@ -753,6 +744,17 @@ def _maybe_drop_cross_column(
753
744
if cross_col is not None :
754
745
del result [cross_col ]
755
746
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
+
756
758
def _indicator_pre_merge (
757
759
self , left : DataFrame , right : DataFrame
758
760
) -> tuple [DataFrame , DataFrame ]:
@@ -765,7 +767,7 @@ def _indicator_pre_merge(
765
767
"Cannot use `indicator=True` option when "
766
768
f"data contains a column named { i } "
767
769
)
768
- if self .indicator_name in columns :
770
+ if self ._indicator_name in columns :
769
771
raise ValueError (
770
772
"Cannot use name of an existing column for indicator column"
771
773
)
@@ -786,13 +788,13 @@ def _indicator_post_merge(self, result: DataFrame) -> DataFrame:
786
788
result ["_left_indicator" ] = result ["_left_indicator" ].fillna (0 )
787
789
result ["_right_indicator" ] = result ["_right_indicator" ].fillna (0 )
788
790
789
- result [self .indicator_name ] = Categorical (
791
+ result [self ._indicator_name ] = Categorical (
790
792
(result ["_left_indicator" ] + result ["_right_indicator" ]),
791
793
categories = [1 , 2 , 3 ],
792
794
)
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" ] )
796
798
797
799
result = result .drop (labels = ["_left_indicator" , "_right_indicator" ], axis = 1 )
798
800
return result
0 commit comments