@@ -39,6 +39,11 @@ class Index(np.ndarray):
39
39
----
40
40
An Index instance can **only** contain hashable objects
41
41
"""
42
+ _map_indices = lib .map_indices_object
43
+ _is_monotonic = lib .is_monotonic_object
44
+ _groupby = lib .groupby_object
45
+ _arrmap = lib .arrmap_object
46
+
42
47
name = None
43
48
def __new__ (cls , data , dtype = None , copy = False , name = None ):
44
49
if isinstance (data , np .ndarray ):
@@ -67,6 +72,10 @@ def dtype(self):
67
72
def nlevels (self ):
68
73
return 1
69
74
75
+ @property
76
+ def _constructor (self ):
77
+ return Index
78
+
70
79
def summary (self ):
71
80
if len (self ) > 0 :
72
81
index_summary = ', %s to %s' % (str (self [0 ]), str (self [- 1 ]))
@@ -82,15 +91,16 @@ def values(self):
82
91
83
92
@cache_readonly
84
93
def is_monotonic (self ):
85
- return lib . is_monotonic_object (self )
94
+ return self . _is_monotonic (self )
86
95
87
96
_indexMap = None
88
97
_integrity = False
98
+
89
99
@property
90
100
def indexMap (self ):
91
101
"{label -> location}"
92
102
if self ._indexMap is None :
93
- self ._indexMap = lib . map_indices_object (self )
103
+ self ._indexMap = self . _map_indices (self )
94
104
self ._integrity = len (self ._indexMap ) == len (self )
95
105
96
106
if not self ._integrity :
@@ -185,7 +195,7 @@ def take(self, *args, **kwargs):
185
195
Analogous to ndarray.take
186
196
"""
187
197
taken = self .view (np .ndarray ).take (* args , ** kwargs )
188
- return Index (taken , name = self .name )
198
+ return self . _constructor (taken , name = self .name )
189
199
190
200
def format (self , name = False ):
191
201
"""
@@ -305,7 +315,7 @@ def union(self, other):
305
315
return _ensure_index (other )
306
316
307
317
if self .is_monotonic and other .is_monotonic :
308
- result = lib .outer_join_indexer_object (self , other )[0 ]
318
+ result = lib .outer_join_indexer_object (self , other . values )[0 ]
309
319
else :
310
320
indexer = self .get_indexer (other )
311
321
indexer = (indexer == - 1 ).nonzero ()[0 ]
@@ -356,9 +366,10 @@ def intersection(self, other):
356
366
other = other .astype (object )
357
367
358
368
if self .is_monotonic and other .is_monotonic :
359
- return Index (lib .inner_join_indexer_object (self , other )[0 ])
369
+ return Index (lib .inner_join_indexer_object (self ,
370
+ other .values )[0 ])
360
371
else :
361
- indexer = self .get_indexer (other )
372
+ indexer = self .get_indexer (other . values )
362
373
indexer = indexer .take ((indexer != - 1 ).nonzero ()[0 ])
363
374
return self .take (indexer )
364
375
@@ -446,10 +457,10 @@ def get_indexer(self, target, method=None):
446
457
return indexer
447
458
448
459
def groupby (self , to_groupby ):
449
- return lib . groupby_object (self .values , to_groupby )
460
+ return self . _groupby (self .values , to_groupby )
450
461
451
462
def map (self , mapper ):
452
- return lib . arrmap_object (self .values , mapper )
463
+ return self . _arrmap (self .values , mapper )
453
464
454
465
def _get_method (self , method ):
455
466
if method :
@@ -621,6 +632,11 @@ def copy(self, order='C'):
621
632
622
633
class Int64Index (Index ):
623
634
635
+ _map_indices = lib .map_indices_int64
636
+ _is_monotonic = lib .is_monotonic_int64
637
+ _groupby = lib .groupby_int64
638
+ _arrmap = lib .arrmap_int64
639
+
624
640
def __new__ (cls , data , dtype = None , copy = False , name = None ):
625
641
if not isinstance (data , np .ndarray ):
626
642
if np .isscalar (data ):
@@ -648,29 +664,17 @@ def __new__(cls, data, dtype=None, copy=False, name=None):
648
664
subarr .name = name
649
665
return subarr
650
666
667
+ @property
668
+ def _constructor (self ):
669
+ return Int64Index
670
+
651
671
def astype (self , dtype ):
652
672
return Index (self .values .astype (dtype ))
653
673
654
674
@property
655
675
def dtype (self ):
656
676
return np .dtype ('int64' )
657
677
658
- @cache_readonly
659
- def is_monotonic (self ):
660
- return lib .is_monotonic_int64 (self )
661
-
662
- @property
663
- def indexMap (self ):
664
- "{label -> location}"
665
- if self ._indexMap is None :
666
- self ._indexMap = lib .map_indices_int64 (self )
667
- self ._integrity = len (self ._indexMap ) == len (self )
668
-
669
- if not self ._integrity :
670
- raise Exception ('Index cannot contain duplicate values!' )
671
-
672
- return self ._indexMap
673
-
674
678
def is_all_dates (self ):
675
679
"""
676
680
Checks that all the labels are datetime objects
@@ -771,19 +775,6 @@ def union(self, other):
771
775
return Int64Index (result )
772
776
union .__doc__ = Index .union .__doc__
773
777
774
- def groupby (self , to_groupby ):
775
- return lib .groupby_int64 (self , to_groupby )
776
-
777
- def map (self , mapper ):
778
- return lib .arrmap_int64 (self , mapper )
779
-
780
- def take (self , * args , ** kwargs ):
781
- """
782
- Analogous to ndarray.take
783
- """
784
- taken = self .values .take (* args , ** kwargs )
785
- return Int64Index (taken , name = self .name )
786
-
787
778
class DateIndex (Index ):
788
779
pass
789
780
@@ -1267,16 +1258,9 @@ def get_indexer(self, target, method=None):
1267
1258
"""
1268
1259
method = self ._get_method (method )
1269
1260
1261
+ target_index = target
1270
1262
if isinstance (target , MultiIndex ):
1271
1263
target_index = target .get_tuple_index ()
1272
- else :
1273
- if len (target ) > 0 :
1274
- val = target [0 ]
1275
- if not isinstance (val , tuple ) or len (val ) != self .nlevels :
1276
- raise ValueError ('can only pass MultiIndex or '
1277
- 'array of tuples' )
1278
-
1279
- target_index = target
1280
1264
1281
1265
self_index = self .get_tuple_index ()
1282
1266
@@ -1509,6 +1493,9 @@ def union(self, other):
1509
1493
-------
1510
1494
Index
1511
1495
"""
1496
+ if not isinstance (other , MultiIndex ):
1497
+ return other .union (self )
1498
+
1512
1499
self ._assert_can_do_setop (other )
1513
1500
1514
1501
if len (other ) == 0 or self .equals (other ):
@@ -1533,6 +1520,9 @@ def intersection(self, other):
1533
1520
-------
1534
1521
Index
1535
1522
"""
1523
+ if not isinstance (other , MultiIndex ):
1524
+ return other .intersection (self )
1525
+
1536
1526
self ._assert_can_do_setop (other )
1537
1527
1538
1528
if self .equals (other ):
0 commit comments