@@ -117,10 +117,12 @@ def __copy__(self) -> Index:
117117 def __deepcopy__ (self , memo : dict [int , Any ] | None = None ) -> Index :
118118 return self ._copy (deep = True , memo = memo )
119119
120- def copy (self , deep : bool = True ) -> Index :
120+ def copy (self : T_Index , deep : bool = True ) -> T_Index :
121121 return self ._copy (deep = deep )
122122
123- def _copy (self , deep : bool = True , memo : dict [int , Any ] | None = None ) -> Index :
123+ def _copy (
124+ self : T_Index , deep : bool = True , memo : dict [int , Any ] | None = None
125+ ) -> T_Index :
124126 cls = self .__class__
125127 copied = cls .__new__ (cls )
126128 if deep :
@@ -269,6 +271,9 @@ def get_indexer_nd(index, labels, method=None, tolerance=None):
269271 return indexer
270272
271273
274+ T_PandasIndex = TypeVar ("T_PandasIndex" , bound = "PandasIndex" )
275+
276+
272277class PandasIndex (Index ):
273278 """Wrap a pandas.Index as an xarray compatible index."""
274279
@@ -532,8 +537,11 @@ def rename(self, name_dict, dims_dict):
532537 new_dim = dims_dict .get (self .dim , self .dim )
533538 return self ._replace (index , dim = new_dim )
534539
535- def copy (self , deep = True ):
540+ def _copy (
541+ self : T_PandasIndex , deep : bool = True , memo : dict [int , Any ] | None = None
542+ ) -> T_PandasIndex :
536543 if deep :
544+ # pandas is not using the memo
537545 index = self .index .copy (deep = True )
538546 else :
539547 # index will be copied in constructor
@@ -1265,11 +1273,19 @@ def to_pandas_indexes(self) -> Indexes[pd.Index]:
12651273 return Indexes (indexes , self ._variables )
12661274
12671275 def copy_indexes (
1268- self , deep : bool = True
1276+ self , deep : bool = True , memo : dict [ int , Any ] | None = None
12691277 ) -> tuple [dict [Hashable , T_PandasOrXarrayIndex ], dict [Hashable , Variable ]]:
12701278 """Return a new dictionary with copies of indexes, preserving
12711279 unique indexes.
12721280
1281+ Parameters
1282+ ----------
1283+ deep : bool, default: True
1284+ Whether the indexes are deep or shallow copied onto the new object.
1285+ memo : dict if object id to copied objects or None, optional
1286+ To prevent infinite recursion deepcopy stores all copied elements
1287+ in this dict.
1288+
12731289 """
12741290 new_indexes = {}
12751291 new_index_vars = {}
@@ -1285,7 +1301,7 @@ def copy_indexes(
12851301 else :
12861302 convert_new_idx = False
12871303
1288- new_idx = idx .copy (deep = deep )
1304+ new_idx = idx ._copy (deep = deep , memo = memo )
12891305 idx_vars = idx .create_variables (coords )
12901306
12911307 if convert_new_idx :
0 commit comments