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,24 @@ 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-
13801372 if len (self ) == 0 :
1381- if not other . name == self . name :
1382- other .name = None
1373+ other = _ensure_index ( other , copy = True )
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-
1383+ #FIXME: can't we just continue with this method?
13911384 return this .union (other )
13921385
13931386 if self .is_monotonic and other .is_monotonic :
@@ -1430,13 +1423,9 @@ def union(self, other):
14301423 "incomparable objects" % e , RuntimeWarning )
14311424
14321425 # 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
1426+ return self ._wrap_union_result (other , result , result_name )
14391427
1428+ def _wrap_union_result (self , other , result , name = None ):
14401429 return self .__class__ (data = result , name = name )
14411430
14421431 def intersection (self , other ):
@@ -1457,13 +1446,12 @@ def intersection(self, other):
14571446
14581447 self ._assert_can_do_setop (other )
14591448
1449+ result_name = _maybe_match_name (self , other )
1450+
14601451 other = _ensure_index (other )
14611452
14621453 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
1454+ self .name = result_name
14671455 return self
14681456
14691457 if not is_dtype_equal (self .dtype ,other .dtype ):
@@ -1474,8 +1462,7 @@ def intersection(self, other):
14741462 if self .is_monotonic and other .is_monotonic :
14751463 try :
14761464 result = self ._inner_indexer (self .values , other .values )[0 ]
1477-
1478- return self ._wrap_union_result (other , result )
1465+ return self ._wrap_union_result (other , result , result_name )
14791466 except TypeError :
14801467 pass
14811468
@@ -1488,8 +1475,7 @@ def intersection(self, other):
14881475 indexer = indexer [indexer != - 1 ]
14891476
14901477 taken = self .take (indexer )
1491- if self .name != other .name and not other .name is None :
1492- taken .name = None
1478+ taken .name = result_name
14931479
14941480 return taken
14951481
@@ -1515,14 +1501,12 @@ def difference(self, other):
15151501 if not hasattr (other , '__iter__' ):
15161502 raise TypeError ('Input must be iterable!' )
15171503
1504+ result_name = _maybe_match_name (self , other )
15181505 if self .equals (other ):
1519- return Index ([], name = self . name )
1506+ return Index ([], name = result_name )
15201507
15211508 if not isinstance (other , Index ):
15221509 other = np .asarray (other )
1523- result_name = self .name
1524- else :
1525- result_name = self .name if self .name == other .name else None
15261510
15271511 theDiff = sorted (set (self ) - set (other ))
15281512 return Index (theDiff , name = result_name )
@@ -1567,9 +1551,11 @@ def sym_diff(self, other, result_name=None):
15671551 if not hasattr (other , '__iter__' ):
15681552 raise TypeError ('Input must be iterable!' )
15691553
1554+ if result_name is None :
1555+ result_name = _maybe_match_name (self , other )
1556+
15701557 if not isinstance (other , Index ):
15711558 other = Index (other )
1572- result_name = result_name or self .name
15731559
15741560 the_diff = sorted (set ((self .difference (other )).union (other .difference (self ))))
15751561 return Index (the_diff , name = result_name )
@@ -2880,6 +2866,7 @@ def _create_from_codes(self, codes, categories=None, ordered=None, name=None):
28802866 ordered = self .ordered
28812867 if name is None :
28822868 name = self .name
2869+
28832870 cat = Categorical .from_codes (codes , categories = categories , ordered = self .ordered )
28842871 return CategoricalIndex (cat , name = name )
28852872
@@ -3260,7 +3247,10 @@ def append(self, other):
32603247 to_concat , name = self ._ensure_compat_append (other )
32613248 to_concat = [ self ._is_dtype_compat (c ) for c in to_concat ]
32623249 codes = np .concatenate ([ c .codes for c in to_concat ])
3263- return self ._create_from_codes (codes , name = name )
3250+ new_index = self ._create_from_codes (codes , name = name )
3251+ #if name should be set to None the create_from_codes method overrides that
3252+ new_index .name = name
3253+ return new_index
32643254
32653255 @classmethod
32663256 def _add_comparison_methods (cls ):
@@ -4420,7 +4410,6 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
44204410 of iterables
44214411 """
44224412 from pandas .core .categorical import Categorical
4423-
44244413 if len (arrays ) == 1 :
44254414 name = None if names is None else names [0 ]
44264415 return Index (arrays [0 ], name = name )
@@ -4430,7 +4419,6 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
44304419 labels = [c .codes for c in cats ]
44314420 if names is None :
44324421 names = [c .name for c in cats ]
4433-
44344422 return MultiIndex (levels = levels , labels = labels ,
44354423 sortorder = sortorder , names = names ,
44364424 verify_integrity = False )
@@ -5480,13 +5468,12 @@ def union(self, other):
54805468 """
54815469 self ._assert_can_do_setop (other )
54825470
5471+ result_names = self .names if hasattr (other ,'names' ) and self .names == other .names else []
5472+
54835473 if len (other ) == 0 or self .equals (other ):
5474+ self .names = result_names
54845475 return self
54855476
5486- result_names = None
5487- if self .names == other .names or other .names is None :
5488- result_names = self .names
5489-
54905477 uniq_tuples = lib .fast_unique_multiple ([self .values , other .values ])
54915478 return MultiIndex .from_arrays (lzip (* uniq_tuples ), sortorder = 0 ,
54925479 names = result_names )
@@ -5509,7 +5496,7 @@ def intersection(self, other):
55095496 return self
55105497
55115498 result_names = None
5512- if self .names == other .names or other .name is None :
5499+ if self .names == other .names or other .names is None :
55135500 result_names = self .names
55145501
55155502 self_tuples = self .values
0 commit comments