@@ -50,6 +50,9 @@ def _shouldbe_timestamp(obj):
5050 or tslib .is_timestamp_array (obj ))
5151
5252
53+ _Identity = object
54+
55+
5356class Index (FrozenNDArray ):
5457 """
5558 Immutable ndarray implementing an ordered, sliceable set. The basic object
@@ -87,6 +90,35 @@ class Index(FrozenNDArray):
8790
8891 _engine_type = _index .ObjectEngine
8992
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+
90122 def __new__ (cls , data , dtype = None , copy = False , name = None , fastpath = False ,
91123 ** kwargs ):
92124
@@ -151,6 +183,7 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False,
151183 return subarr
152184
153185 def __array_finalize__ (self , obj ):
186+ self ._reset_identity ()
154187 if not isinstance (obj , type (self )):
155188 # Only relevant if array being created from an Index instance
156189 return
@@ -279,6 +312,7 @@ def set_names(self, names, inplace=False):
279312 raise TypeError ("Must pass list-like as `names`." )
280313 if inplace :
281314 idx = self
315+ idx ._reset_identity ()
282316 else :
283317 idx = self ._shallow_copy ()
284318 idx ._set_names (names )
@@ -554,7 +588,7 @@ def equals(self, other):
554588 """
555589 Determines if two Index objects contain the same elements.
556590 """
557- if self is other :
591+ if self . is_ ( other ) :
558592 return True
559593
560594 if not isinstance (other , Index ):
@@ -1536,7 +1570,7 @@ def equals(self, other):
15361570 """
15371571 Determines if two Index objects contain the same elements.
15381572 """
1539- if self is other :
1573+ if self . is_ ( other ) :
15401574 return True
15411575
15421576 # if not isinstance(other, Int64Index):
@@ -1645,6 +1679,7 @@ def set_levels(self, levels, inplace=False):
16451679 idx = self
16461680 else :
16471681 idx = self ._shallow_copy ()
1682+ idx ._reset_identity ()
16481683 idx ._set_levels (levels )
16491684 return idx
16501685
@@ -1683,6 +1718,7 @@ def set_labels(self, labels, inplace=False):
16831718 idx = self
16841719 else :
16851720 idx = self ._shallow_copy ()
1721+ idx ._reset_identity ()
16861722 idx ._set_labels (labels )
16871723 return idx
16881724
@@ -1736,6 +1772,8 @@ def __array_finalize__(self, obj):
17361772 Update custom MultiIndex attributes when a new array is created by
17371773 numpy, e.g. when calling ndarray.view()
17381774 """
1775+ # overriden if a view
1776+ self ._reset_identity ()
17391777 if not isinstance (obj , type (self )):
17401778 # Only relevant if this array is being created from an Index
17411779 # instance.
@@ -2754,7 +2792,7 @@ def equals(self, other):
27542792 --------
27552793 equal_levels
27562794 """
2757- if self is other :
2795+ if self . is_ ( other ) :
27582796 return True
27592797
27602798 if not isinstance (other , MultiIndex ):
0 commit comments