21
21
from pandas .core .common import (isnull , array_equivalent , is_dtype_equal , is_object_dtype ,
22
22
_values_from_object , is_float , is_integer , is_iterator , is_categorical_dtype ,
23
23
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 )
25
25
from pandas .core .config import get_option
26
26
from pandas .io .common import PerformanceWarning
27
27
@@ -1088,7 +1088,6 @@ def _ensure_compat_append(self, other):
1088
1088
-------
1089
1089
list of to_concat, name of result Index
1090
1090
"""
1091
- name = self .name
1092
1091
to_concat = [self ]
1093
1092
1094
1093
if isinstance (other , (list , tuple )):
@@ -1097,16 +1096,13 @@ def _ensure_compat_append(self, other):
1097
1096
to_concat .append (other )
1098
1097
1099
1098
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
1105
1101
1106
1102
to_concat = self ._ensure_compat_concat (to_concat )
1107
1103
to_concat = [x .values if isinstance (x , Index ) else x
1108
1104
for x in to_concat ]
1109
- return to_concat , name
1105
+ return to_concat , result_name
1110
1106
1111
1107
def append (self , other ):
1112
1108
"""
@@ -1367,27 +1363,23 @@ def union(self, other):
1367
1363
if not hasattr (other , '__iter__' ):
1368
1364
raise TypeError ('Input must be iterable.' )
1369
1365
1366
+ result_name = _maybe_match_name (self , other )
1367
+
1370
1368
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
1376
1370
return self
1377
1371
1378
- other = _ensure_index (other )
1379
-
1372
+ other = _ensure_index (other , copy = True )
1380
1373
if len (self ) == 0 :
1381
- if not other .name == self .name :
1382
- other .name = None
1374
+ other .name = result_name
1383
1375
return other
1384
1376
1385
1377
self ._assert_can_do_setop (other )
1386
1378
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 ):
1388
1381
this = self .astype ('O' )
1389
1382
other = other .astype ('O' )
1390
-
1391
1383
return this .union (other )
1392
1384
1393
1385
if self .is_monotonic and other .is_monotonic :
@@ -1430,13 +1422,9 @@ def union(self, other):
1430
1422
"incomparable objects" % e , RuntimeWarning )
1431
1423
1432
1424
# 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 )
1439
1426
1427
+ def _wrap_union_result (self , other , result , name = None ):
1440
1428
return self .__class__ (data = result , name = name )
1441
1429
1442
1430
def intersection (self , other ):
@@ -1457,13 +1445,12 @@ def intersection(self, other):
1457
1445
1458
1446
self ._assert_can_do_setop (other )
1459
1447
1448
+ result_name = _maybe_match_name (self , other )
1449
+
1460
1450
other = _ensure_index (other )
1461
1451
1462
1452
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
1467
1454
return self
1468
1455
1469
1456
if not is_dtype_equal (self .dtype ,other .dtype ):
@@ -1474,8 +1461,7 @@ def intersection(self, other):
1474
1461
if self .is_monotonic and other .is_monotonic :
1475
1462
try :
1476
1463
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 )
1479
1465
except TypeError :
1480
1466
pass
1481
1467
@@ -1488,8 +1474,7 @@ def intersection(self, other):
1488
1474
indexer = indexer [indexer != - 1 ]
1489
1475
1490
1476
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
1493
1478
1494
1479
return taken
1495
1480
@@ -1515,14 +1500,12 @@ def difference(self, other):
1515
1500
if not hasattr (other , '__iter__' ):
1516
1501
raise TypeError ('Input must be iterable!' )
1517
1502
1503
+ result_name = _maybe_match_name (self , other )
1518
1504
if self .equals (other ):
1519
- return Index ([], name = self . name )
1505
+ return Index ([], name = result_name )
1520
1506
1521
1507
if not isinstance (other , Index ):
1522
1508
other = np .asarray (other )
1523
- result_name = self .name
1524
- else :
1525
- result_name = self .name if self .name == other .name else None
1526
1509
1527
1510
theDiff = sorted (set (self ) - set (other ))
1528
1511
return Index (theDiff , name = result_name )
@@ -1567,9 +1550,11 @@ def sym_diff(self, other, result_name=None):
1567
1550
if not hasattr (other , '__iter__' ):
1568
1551
raise TypeError ('Input must be iterable!' )
1569
1552
1553
+ if result_name is None :
1554
+ result_name = _maybe_match_name (self , other )
1555
+
1570
1556
if not isinstance (other , Index ):
1571
1557
other = Index (other )
1572
- result_name = result_name or self .name
1573
1558
1574
1559
the_diff = sorted (set ((self .difference (other )).union (other .difference (self ))))
1575
1560
return Index (the_diff , name = result_name )
@@ -2880,6 +2865,7 @@ def _create_from_codes(self, codes, categories=None, ordered=None, name=None):
2880
2865
ordered = self .ordered
2881
2866
if name is None :
2882
2867
name = self .name
2868
+
2883
2869
cat = Categorical .from_codes (codes , categories = categories , ordered = self .ordered )
2884
2870
return CategoricalIndex (cat , name = name )
2885
2871
@@ -3260,7 +3246,10 @@ def append(self, other):
3260
3246
to_concat , name = self ._ensure_compat_append (other )
3261
3247
to_concat = [ self ._is_dtype_compat (c ) for c in to_concat ]
3262
3248
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
3264
3253
3265
3254
@classmethod
3266
3255
def _add_comparison_methods (cls ):
@@ -4420,7 +4409,6 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
4420
4409
of iterables
4421
4410
"""
4422
4411
from pandas .core .categorical import Categorical
4423
-
4424
4412
if len (arrays ) == 1 :
4425
4413
name = None if names is None else names [0 ]
4426
4414
return Index (arrays [0 ], name = name )
@@ -4430,7 +4418,6 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
4430
4418
labels = [c .codes for c in cats ]
4431
4419
if names is None :
4432
4420
names = [c .name for c in cats ]
4433
-
4434
4421
return MultiIndex (levels = levels , labels = labels ,
4435
4422
sortorder = sortorder , names = names ,
4436
4423
verify_integrity = False )
@@ -5480,13 +5467,12 @@ def union(self, other):
5480
5467
"""
5481
5468
self ._assert_can_do_setop (other )
5482
5469
5470
+ result_names = self .names if hasattr (other ,'names' ) and self .names == other .names else []
5471
+
5483
5472
if len (other ) == 0 or self .equals (other ):
5473
+ self .names = result_names
5484
5474
return self
5485
5475
5486
- result_names = None
5487
- if self .names == other .names or other .names is None :
5488
- result_names = self .names
5489
-
5490
5476
uniq_tuples = lib .fast_unique_multiple ([self .values , other .values ])
5491
5477
return MultiIndex .from_arrays (lzip (* uniq_tuples ), sortorder = 0 ,
5492
5478
names = result_names )
@@ -5509,7 +5495,7 @@ def intersection(self, other):
5509
5495
return self
5510
5496
5511
5497
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 :
5513
5499
result_names = self .names
5514
5500
5515
5501
self_tuples = self .values
0 commit comments