61
61
import pandas .core .sorting as sorting
62
62
from pandas .io .formats .printing import (
63
63
pprint_thing , default_pprint , format_object_summary , format_object_attrs )
64
- from pandas .core .ops import make_invalid_op
64
+ from pandas .core .ops import make_invalid_op , get_op_result_name
65
65
from pandas .core .strings import StringMethods
66
66
67
67
__all__ = ['Index' ]
@@ -1253,7 +1253,7 @@ def _convert_can_do_setop(self, other):
1253
1253
other = Index (other , name = self .name )
1254
1254
result_name = self .name
1255
1255
else :
1256
- result_name = self . name if self . name == other . name else None
1256
+ result_name = get_op_result_name ( self , other )
1257
1257
return other , result_name
1258
1258
1259
1259
def _convert_for_op (self , value ):
@@ -2745,19 +2745,15 @@ def __or__(self, other):
2745
2745
def __xor__ (self , other ):
2746
2746
return self .symmetric_difference (other )
2747
2747
2748
- def _get_consensus_name (self , other ):
2748
+ def _get_reconciled_name_object (self , other ):
2749
2749
"""
2750
- Given 2 indexes, give a consensus name meaning
2751
- we take the not None one, or None if the names differ.
2752
- Return a new object if we are resetting the name
2750
+ If the result of a set operation will be self,
2751
+ return self, unless the name changes, in which
2752
+ case make a shallow copy of self.
2753
2753
"""
2754
- if self .name != other .name :
2755
- if self .name is None or other .name is None :
2756
- name = self .name or other .name
2757
- else :
2758
- name = None
2759
- if self .name != name :
2760
- return self ._shallow_copy (name = name )
2754
+ name = get_op_result_name (self , other )
2755
+ if self .name != name :
2756
+ return self ._shallow_copy (name = name )
2761
2757
return self
2762
2758
2763
2759
def union (self , other ):
@@ -2785,10 +2781,10 @@ def union(self, other):
2785
2781
other = ensure_index (other )
2786
2782
2787
2783
if len (other ) == 0 or self .equals (other ):
2788
- return self ._get_consensus_name (other )
2784
+ return self ._get_reconciled_name_object (other )
2789
2785
2790
2786
if len (self ) == 0 :
2791
- return other ._get_consensus_name (self )
2787
+ return other ._get_reconciled_name_object (self )
2792
2788
2793
2789
# TODO: is_dtype_union_equal is a hack around
2794
2790
# 1. buggy set ops with duplicates (GH #13432)
@@ -2851,11 +2847,10 @@ def union(self, other):
2851
2847
stacklevel = 3 )
2852
2848
2853
2849
# for subclasses
2854
- return self ._wrap_union_result (other , result )
2850
+ return self ._wrap_setop_result (other , result )
2855
2851
2856
- def _wrap_union_result (self , other , result ):
2857
- name = self .name if self .name == other .name else None
2858
- return self .__class__ (result , name = name )
2852
+ def _wrap_setop_result (self , other , result ):
2853
+ return self ._constructor (result , name = get_op_result_name (self , other ))
2859
2854
2860
2855
def intersection (self , other ):
2861
2856
"""
@@ -2885,7 +2880,7 @@ def intersection(self, other):
2885
2880
other = ensure_index (other )
2886
2881
2887
2882
if self .equals (other ):
2888
- return self ._get_consensus_name (other )
2883
+ return self ._get_reconciled_name_object (other )
2889
2884
2890
2885
if not is_dtype_equal (self .dtype , other .dtype ):
2891
2886
this = self .astype ('O' )
@@ -2905,7 +2900,7 @@ def intersection(self, other):
2905
2900
if self .is_monotonic and other .is_monotonic :
2906
2901
try :
2907
2902
result = self ._inner_indexer (lvals , rvals )[0 ]
2908
- return self ._wrap_union_result (other , result )
2903
+ return self ._wrap_setop_result (other , result )
2909
2904
except TypeError :
2910
2905
pass
2911
2906
@@ -4175,7 +4170,7 @@ def _join_monotonic(self, other, how='left', return_indexers=False):
4175
4170
return join_index
4176
4171
4177
4172
def _wrap_joined_index (self , joined , other ):
4178
- name = self . name if self . name == other . name else None
4173
+ name = get_op_result_name ( self , other )
4179
4174
return Index (joined , name = name )
4180
4175
4181
4176
def _get_string_slice (self , key , use_lhs = True , use_rhs = True ):
0 commit comments