@@ -117,10 +117,12 @@ def __copy__(self) -> Index:
117
117
def __deepcopy__ (self , memo : dict [int , Any ] | None = None ) -> Index :
118
118
return self ._copy (deep = True , memo = memo )
119
119
120
- def copy (self , deep : bool = True ) -> Index :
120
+ def copy (self : T_Index , deep : bool = True ) -> T_Index :
121
121
return self ._copy (deep = deep )
122
122
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 :
124
126
cls = self .__class__
125
127
copied = cls .__new__ (cls )
126
128
if deep :
@@ -269,6 +271,9 @@ def get_indexer_nd(index, labels, method=None, tolerance=None):
269
271
return indexer
270
272
271
273
274
+ T_PandasIndex = TypeVar ("T_PandasIndex" , bound = "PandasIndex" )
275
+
276
+
272
277
class PandasIndex (Index ):
273
278
"""Wrap a pandas.Index as an xarray compatible index."""
274
279
@@ -532,8 +537,11 @@ def rename(self, name_dict, dims_dict):
532
537
new_dim = dims_dict .get (self .dim , self .dim )
533
538
return self ._replace (index , dim = new_dim )
534
539
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 :
536
543
if deep :
544
+ # pandas is not using the memo
537
545
index = self .index .copy (deep = True )
538
546
else :
539
547
# index will be copied in constructor
@@ -1265,11 +1273,19 @@ def to_pandas_indexes(self) -> Indexes[pd.Index]:
1265
1273
return Indexes (indexes , self ._variables )
1266
1274
1267
1275
def copy_indexes (
1268
- self , deep : bool = True
1276
+ self , deep : bool = True , memo : dict [ int , Any ] | None = None
1269
1277
) -> tuple [dict [Hashable , T_PandasOrXarrayIndex ], dict [Hashable , Variable ]]:
1270
1278
"""Return a new dictionary with copies of indexes, preserving
1271
1279
unique indexes.
1272
1280
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
+
1273
1289
"""
1274
1290
new_indexes = {}
1275
1291
new_index_vars = {}
@@ -1285,7 +1301,7 @@ def copy_indexes(
1285
1301
else :
1286
1302
convert_new_idx = False
1287
1303
1288
- new_idx = idx .copy (deep = deep )
1304
+ new_idx = idx ._copy (deep = deep , memo = memo )
1289
1305
idx_vars = idx .create_variables (coords )
1290
1306
1291
1307
if convert_new_idx :
0 commit comments