2121from pandas .core .common import (isnull , array_equivalent , is_dtype_equal , is_object_dtype ,
2222 _values_from_object , is_float , is_integer , is_iterator , is_categorical_dtype ,
2323 ABCSeries , ABCCategorical , _ensure_object , _ensure_int64 , is_bool_indexer ,
24- is_list_like , is_bool_dtype , is_null_slice , is_integer_dtype )
24+ is_list_like , is_bool_dtype , is_null_slice , is_integer_dtype , _maybe_match_name )
2525from pandas .core .config import get_option
2626from pandas .io .common import PerformanceWarning
2727
@@ -1088,7 +1088,6 @@ def _ensure_compat_append(self, other):
10881088 -------
10891089 list of to_concat, name of result Index
10901090 """
1091- name = self .name
10921091 to_concat = [self ]
10931092
10941093 if isinstance (other , (list , tuple )):
@@ -1097,16 +1096,13 @@ def _ensure_compat_append(self, other):
10971096 to_concat .append (other )
10981097
10991098 for obj in to_concat :
1100- if (isinstance (obj , Index ) and
1101- obj .name != name and
1102- obj .name is not None ):
1103- name = None
1104- break
1099+ result_name = _maybe_match_name (self , obj )
1100+ self .name = result_name
11051101
11061102 to_concat = self ._ensure_compat_concat (to_concat )
11071103 to_concat = [x .values if isinstance (x , Index ) else x
11081104 for x in to_concat ]
1109- return to_concat , name
1105+ return to_concat , result_name
11101106
11111107 def append (self , other ):
11121108 """
@@ -1367,27 +1363,23 @@ def union(self, other):
13671363 if not hasattr (other , '__iter__' ):
13681364 raise TypeError ('Input must be iterable.' )
13691365
1366+ result_name = _maybe_match_name (self , other )
1367+
13701368 if len (other ) == 0 or self .equals (other ):
1371- if (len (other ) and
1372- hasattr (other , 'name' ) and not
1373- other .name == self .name and not
1374- other .name is None ):
1375- self .name = None
1369+ self .name = result_name
13761370 return self
13771371
1378- other = _ensure_index (other )
1379-
1372+ other = _ensure_index (other , copy = True )
13801373 if len (self ) == 0 :
1381- if not other .name == self .name :
1382- other .name = None
1374+ other .name = result_name
13831375 return other
13841376
13851377 self ._assert_can_do_setop (other )
13861378
1387- if not is_dtype_equal (self .dtype ,other .dtype ):
1379+ #FIXME: right now crashes if we union with python array
1380+ if not is_dtype_equal (self .dtype , other .dtype ):
13881381 this = self .astype ('O' )
13891382 other = other .astype ('O' )
1390-
13911383 return this .union (other )
13921384
13931385 if self .is_monotonic and other .is_monotonic :
@@ -1430,13 +1422,9 @@ def union(self, other):
14301422 "incomparable objects" % e , RuntimeWarning )
14311423
14321424 # for subclasses
1433- return self ._wrap_union_result (other , result )
1434-
1435- def _wrap_union_result (self , other , result ):
1436- name = None
1437- if self .name == other .name or other .name is None :
1438- name = self .name
1425+ return self ._wrap_union_result (other , result , result_name )
14391426
1427+ def _wrap_union_result (self , other , result , name = None ):
14401428 return self .__class__ (data = result , name = name )
14411429
14421430 def intersection (self , other ):
@@ -1457,13 +1445,12 @@ def intersection(self, other):
14571445
14581446 self ._assert_can_do_setop (other )
14591447
1448+ result_name = _maybe_match_name (self , other )
1449+
14601450 other = _ensure_index (other )
14611451
14621452 if self .equals (other ):
1463- if (hasattr (other , 'name' )
1464- and not other .name is None
1465- and not other .name == self .name ):
1466- self .name = None
1453+ self .name = result_name
14671454 return self
14681455
14691456 if not is_dtype_equal (self .dtype ,other .dtype ):
@@ -1474,8 +1461,7 @@ def intersection(self, other):
14741461 if self .is_monotonic and other .is_monotonic :
14751462 try :
14761463 result = self ._inner_indexer (self .values , other .values )[0 ]
1477-
1478- return self ._wrap_union_result (other , result )
1464+ return self ._wrap_union_result (other , result , result_name )
14791465 except TypeError :
14801466 pass
14811467
@@ -1488,8 +1474,7 @@ def intersection(self, other):
14881474 indexer = indexer [indexer != - 1 ]
14891475
14901476 taken = self .take (indexer )
1491- if self .name != other .name and not other .name is None :
1492- taken .name = None
1477+ taken .name = result_name
14931478
14941479 return taken
14951480
@@ -1515,14 +1500,12 @@ def difference(self, other):
15151500 if not hasattr (other , '__iter__' ):
15161501 raise TypeError ('Input must be iterable!' )
15171502
1503+ result_name = _maybe_match_name (self , other )
15181504 if self .equals (other ):
1519- return Index ([], name = self . name )
1505+ return Index ([], name = result_name )
15201506
15211507 if not isinstance (other , Index ):
15221508 other = np .asarray (other )
1523- result_name = self .name
1524- else :
1525- result_name = self .name if self .name == other .name else None
15261509
15271510 theDiff = sorted (set (self ) - set (other ))
15281511 return Index (theDiff , name = result_name )
@@ -1567,9 +1550,11 @@ def sym_diff(self, other, result_name=None):
15671550 if not hasattr (other , '__iter__' ):
15681551 raise TypeError ('Input must be iterable!' )
15691552
1553+ if result_name is None :
1554+ result_name = _maybe_match_name (self , other )
1555+
15701556 if not isinstance (other , Index ):
15711557 other = Index (other )
1572- result_name = result_name or self .name
15731558
15741559 the_diff = sorted (set ((self .difference (other )).union (other .difference (self ))))
15751560 return Index (the_diff , name = result_name )
@@ -2880,6 +2865,7 @@ def _create_from_codes(self, codes, categories=None, ordered=None, name=None):
28802865 ordered = self .ordered
28812866 if name is None :
28822867 name = self .name
2868+
28832869 cat = Categorical .from_codes (codes , categories = categories , ordered = self .ordered )
28842870 return CategoricalIndex (cat , name = name )
28852871
@@ -3260,7 +3246,10 @@ def append(self, other):
32603246 to_concat , name = self ._ensure_compat_append (other )
32613247 to_concat = [ self ._is_dtype_compat (c ) for c in to_concat ]
32623248 codes = np .concatenate ([ c .codes for c in to_concat ])
3263- return self ._create_from_codes (codes , name = name )
3249+ new_index = self ._create_from_codes (codes , name = name )
3250+ #if name should be set to None the create_from_codes method overrides that
3251+ new_index .name = name
3252+ return new_index
32643253
32653254 @classmethod
32663255 def _add_comparison_methods (cls ):
@@ -4420,7 +4409,6 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
44204409 of iterables
44214410 """
44224411 from pandas .core .categorical import Categorical
4423-
44244412 if len (arrays ) == 1 :
44254413 name = None if names is None else names [0 ]
44264414 return Index (arrays [0 ], name = name )
@@ -4430,7 +4418,6 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
44304418 labels = [c .codes for c in cats ]
44314419 if names is None :
44324420 names = [c .name for c in cats ]
4433-
44344421 return MultiIndex (levels = levels , labels = labels ,
44354422 sortorder = sortorder , names = names ,
44364423 verify_integrity = False )
@@ -5480,13 +5467,12 @@ def union(self, other):
54805467 """
54815468 self ._assert_can_do_setop (other )
54825469
5470+ result_names = self .names if hasattr (other ,'names' ) and self .names == other .names else []
5471+
54835472 if len (other ) == 0 or self .equals (other ):
5473+ self .names = result_names
54845474 return self
54855475
5486- result_names = None
5487- if self .names == other .names or other .names is None :
5488- result_names = self .names
5489-
54905476 uniq_tuples = lib .fast_unique_multiple ([self .values , other .values ])
54915477 return MultiIndex .from_arrays (lzip (* uniq_tuples ), sortorder = 0 ,
54925478 names = result_names )
@@ -5509,7 +5495,7 @@ def intersection(self, other):
55095495 return self
55105496
55115497 result_names = None
5512- if self .names == other .names or other .name is None :
5498+ if self .names == other .names or other .names is None :
55135499 result_names = self .names
55145500
55155501 self_tuples = self .values
0 commit comments