@@ -50,6 +50,9 @@ def _shouldbe_timestamp(obj):
50
50
or tslib .is_timestamp_array (obj ))
51
51
52
52
53
+ _Identity = object
54
+
55
+
53
56
class Index (FrozenNDArray ):
54
57
"""
55
58
Immutable ndarray implementing an ordered, sliceable set. The basic object
@@ -87,6 +90,35 @@ class Index(FrozenNDArray):
87
90
88
91
_engine_type = _index .ObjectEngine
89
92
93
+ def is_ (self , other ):
94
+ """
95
+ More flexible, faster check like ``is`` but that works through views
96
+
97
+ Note: this is *not* the same as ``Index.identical()``, which checks
98
+ that metadata is also the same.
99
+
100
+ Parameters
101
+ ----------
102
+ other : object
103
+ other object to compare against.
104
+
105
+ Returns
106
+ -------
107
+ True if both have same underlying data, False otherwise : bool
108
+ """
109
+ # use something other than None to be clearer
110
+ return self ._id is getattr (other , '_id' , Ellipsis )
111
+
112
+ def _reset_identity (self ):
113
+ "Initializes or resets ``_id`` attribute with new object"
114
+ self ._id = _Identity ()
115
+
116
+ def view (self , * args , ** kwargs ):
117
+ result = super (Index , self ).view (* args , ** kwargs )
118
+ if isinstance (result , Index ):
119
+ result ._id = self ._id
120
+ return result
121
+
90
122
def __new__ (cls , data , dtype = None , copy = False , name = None , fastpath = False ,
91
123
** kwargs ):
92
124
@@ -151,6 +183,7 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False,
151
183
return subarr
152
184
153
185
def __array_finalize__ (self , obj ):
186
+ self ._reset_identity ()
154
187
if not isinstance (obj , type (self )):
155
188
# Only relevant if array being created from an Index instance
156
189
return
@@ -279,6 +312,7 @@ def set_names(self, names, inplace=False):
279
312
raise TypeError ("Must pass list-like as `names`." )
280
313
if inplace :
281
314
idx = self
315
+ idx ._reset_identity ()
282
316
else :
283
317
idx = self ._shallow_copy ()
284
318
idx ._set_names (names )
@@ -554,7 +588,7 @@ def equals(self, other):
554
588
"""
555
589
Determines if two Index objects contain the same elements.
556
590
"""
557
- if self is other :
591
+ if self . is_ ( other ) :
558
592
return True
559
593
560
594
if not isinstance (other , Index ):
@@ -1536,7 +1570,7 @@ def equals(self, other):
1536
1570
"""
1537
1571
Determines if two Index objects contain the same elements.
1538
1572
"""
1539
- if self is other :
1573
+ if self . is_ ( other ) :
1540
1574
return True
1541
1575
1542
1576
# if not isinstance(other, Int64Index):
@@ -1645,6 +1679,7 @@ def set_levels(self, levels, inplace=False):
1645
1679
idx = self
1646
1680
else :
1647
1681
idx = self ._shallow_copy ()
1682
+ idx ._reset_identity ()
1648
1683
idx ._set_levels (levels )
1649
1684
return idx
1650
1685
@@ -1683,6 +1718,7 @@ def set_labels(self, labels, inplace=False):
1683
1718
idx = self
1684
1719
else :
1685
1720
idx = self ._shallow_copy ()
1721
+ idx ._reset_identity ()
1686
1722
idx ._set_labels (labels )
1687
1723
return idx
1688
1724
@@ -1736,6 +1772,8 @@ def __array_finalize__(self, obj):
1736
1772
Update custom MultiIndex attributes when a new array is created by
1737
1773
numpy, e.g. when calling ndarray.view()
1738
1774
"""
1775
+ # overriden if a view
1776
+ self ._reset_identity ()
1739
1777
if not isinstance (obj , type (self )):
1740
1778
# Only relevant if this array is being created from an Index
1741
1779
# instance.
@@ -2754,7 +2792,7 @@ def equals(self, other):
2754
2792
--------
2755
2793
equal_levels
2756
2794
"""
2757
- if self is other :
2795
+ if self . is_ ( other ) :
2758
2796
return True
2759
2797
2760
2798
if not isinstance (other , MultiIndex ):
0 commit comments