24
24
)
25
25
26
26
if TYPE_CHECKING :
27
- from xarray .core .types import ErrorOptions , JoinOptions , T_Index
27
+ from xarray .core .types import ErrorOptions , JoinOptions , Self
28
28
from xarray .core .variable import Variable
29
29
30
30
@@ -60,11 +60,11 @@ class Index:
60
60
61
61
@classmethod
62
62
def from_variables (
63
- cls : type [ T_Index ] ,
63
+ cls ,
64
64
variables : Mapping [Any , Variable ],
65
65
* ,
66
66
options : Mapping [str , Any ],
67
- ) -> T_Index :
67
+ ) -> Self :
68
68
"""Create a new index object from one or more coordinate variables.
69
69
70
70
This factory method must be implemented in all subclasses of Index.
@@ -88,11 +88,11 @@ def from_variables(
88
88
89
89
@classmethod
90
90
def concat (
91
- cls : type [ T_Index ] ,
92
- indexes : Sequence [T_Index ],
91
+ cls ,
92
+ indexes : Sequence [Self ],
93
93
dim : Hashable ,
94
94
positions : Iterable [Iterable [int ]] | None = None ,
95
- ) -> T_Index :
95
+ ) -> Self :
96
96
"""Create a new index by concatenating one or more indexes of the same
97
97
type.
98
98
@@ -120,9 +120,7 @@ def concat(
120
120
raise NotImplementedError ()
121
121
122
122
@classmethod
123
- def stack (
124
- cls : type [T_Index ], variables : Mapping [Any , Variable ], dim : Hashable
125
- ) -> T_Index :
123
+ def stack (cls , variables : Mapping [Any , Variable ], dim : Hashable ) -> Self :
126
124
"""Create a new index by stacking coordinate variables into a single new
127
125
dimension.
128
126
@@ -208,8 +206,8 @@ def to_pandas_index(self) -> pd.Index:
208
206
raise TypeError (f"{ self !r} cannot be cast to a pandas.Index object" )
209
207
210
208
def isel (
211
- self : T_Index , indexers : Mapping [Any , int | slice | np .ndarray | Variable ]
212
- ) -> T_Index | None :
209
+ self , indexers : Mapping [Any , int | slice | np .ndarray | Variable ]
210
+ ) -> Self | None :
213
211
"""Maybe returns a new index from the current index itself indexed by
214
212
positional indexers.
215
213
@@ -264,7 +262,7 @@ def sel(self, labels: dict[Any, Any]) -> IndexSelResult:
264
262
"""
265
263
raise NotImplementedError (f"{ self !r} doesn't support label-based selection" )
266
264
267
- def join (self : T_Index , other : T_Index , how : JoinOptions = "inner" ) -> T_Index :
265
+ def join (self , other : Self , how : JoinOptions = "inner" ) -> Self :
268
266
"""Return a new index from the combination of this index with another
269
267
index of the same type.
270
268
@@ -286,7 +284,7 @@ def join(self: T_Index, other: T_Index, how: JoinOptions = "inner") -> T_Index:
286
284
f"{ self !r} doesn't support alignment with inner/outer join method"
287
285
)
288
286
289
- def reindex_like (self : T_Index , other : T_Index ) -> dict [Hashable , Any ]:
287
+ def reindex_like (self , other : Self ) -> dict [Hashable , Any ]:
290
288
"""Query the index with another index of the same type.
291
289
292
290
Implementation is optional but required in order to support alignment.
@@ -304,7 +302,7 @@ def reindex_like(self: T_Index, other: T_Index) -> dict[Hashable, Any]:
304
302
"""
305
303
raise NotImplementedError (f"{ self !r} doesn't support re-indexing labels" )
306
304
307
- def equals (self : T_Index , other : T_Index ) -> bool :
305
+ def equals (self , other : Self ) -> bool :
308
306
"""Compare this index with another index of the same type.
309
307
310
308
Implementation is optional but required in order to support alignment.
@@ -321,7 +319,7 @@ def equals(self: T_Index, other: T_Index) -> bool:
321
319
"""
322
320
raise NotImplementedError ()
323
321
324
- def roll (self : T_Index , shifts : Mapping [Any , int ]) -> T_Index | None :
322
+ def roll (self , shifts : Mapping [Any , int ]) -> Self | None :
325
323
"""Roll this index by an offset along one or more dimensions.
326
324
327
325
This method can be re-implemented in subclasses of Index, e.g., when the
@@ -347,10 +345,10 @@ def roll(self: T_Index, shifts: Mapping[Any, int]) -> T_Index | None:
347
345
return None
348
346
349
347
def rename (
350
- self : T_Index ,
348
+ self ,
351
349
name_dict : Mapping [Any , Hashable ],
352
350
dims_dict : Mapping [Any , Hashable ],
353
- ) -> T_Index :
351
+ ) -> Self :
354
352
"""Maybe update the index with new coordinate and dimension names.
355
353
356
354
This method should be re-implemented in subclasses of Index if it has
@@ -377,7 +375,7 @@ def rename(
377
375
"""
378
376
return self
379
377
380
- def copy (self : T_Index , deep : bool = True ) -> T_Index :
378
+ def copy (self , deep : bool = True ) -> Self :
381
379
"""Return a (deep) copy of this index.
382
380
383
381
Implementation in subclasses of Index is optional. The base class
@@ -396,15 +394,13 @@ def copy(self: T_Index, deep: bool = True) -> T_Index:
396
394
"""
397
395
return self ._copy (deep = deep )
398
396
399
- def __copy__ (self : T_Index ) -> T_Index :
397
+ def __copy__ (self ) -> Self :
400
398
return self .copy (deep = False )
401
399
402
400
def __deepcopy__ (self , memo : dict [int , Any ] | None = None ) -> Index :
403
401
return self ._copy (deep = True , memo = memo )
404
402
405
- def _copy (
406
- self : T_Index , deep : bool = True , memo : dict [int , Any ] | None = None
407
- ) -> T_Index :
403
+ def _copy (self , deep : bool = True , memo : dict [int , Any ] | None = None ) -> Self :
408
404
cls = self .__class__
409
405
copied = cls .__new__ (cls )
410
406
if deep :
@@ -414,7 +410,7 @@ def _copy(
414
410
copied .__dict__ .update (self .__dict__ )
415
411
return copied
416
412
417
- def __getitem__ (self : T_Index , indexer : Any ) -> T_Index :
413
+ def __getitem__ (self , indexer : Any ) -> Self :
418
414
raise NotImplementedError ()
419
415
420
416
def _repr_inline_ (self , max_width ):
@@ -674,10 +670,10 @@ def _concat_indexes(indexes, dim, positions=None) -> pd.Index:
674
670
@classmethod
675
671
def concat (
676
672
cls ,
677
- indexes : Sequence [PandasIndex ],
673
+ indexes : Sequence [Self ],
678
674
dim : Hashable ,
679
675
positions : Iterable [Iterable [int ]] | None = None ,
680
- ) -> PandasIndex :
676
+ ) -> Self :
681
677
new_pd_index = cls ._concat_indexes (indexes , dim , positions )
682
678
683
679
if not indexes :
@@ -800,7 +796,11 @@ def equals(self, other: Index):
800
796
return False
801
797
return self .index .equals (other .index ) and self .dim == other .dim
802
798
803
- def join (self : PandasIndex , other : PandasIndex , how : str = "inner" ) -> PandasIndex :
799
+ def join (
800
+ self ,
801
+ other : Self ,
802
+ how : str = "inner" ,
803
+ ) -> Self :
804
804
if how == "outer" :
805
805
index = self .index .union (other .index )
806
806
else :
@@ -811,7 +811,7 @@ def join(self: PandasIndex, other: PandasIndex, how: str = "inner") -> PandasInd
811
811
return type (self )(index , self .dim , coord_dtype = coord_dtype )
812
812
813
813
def reindex_like (
814
- self , other : PandasIndex , method = None , tolerance = None
814
+ self , other : Self , method = None , tolerance = None
815
815
) -> dict [Hashable , Any ]:
816
816
if not self .index .is_unique :
817
817
raise ValueError (
@@ -963,12 +963,12 @@ def from_variables(
963
963
return obj
964
964
965
965
@classmethod
966
- def concat ( # type: ignore[override]
966
+ def concat (
967
967
cls ,
968
- indexes : Sequence [PandasMultiIndex ],
968
+ indexes : Sequence [Self ],
969
969
dim : Hashable ,
970
970
positions : Iterable [Iterable [int ]] | None = None ,
971
- ) -> PandasMultiIndex :
971
+ ) -> Self :
972
972
new_pd_index = cls ._concat_indexes (indexes , dim , positions )
973
973
974
974
if not indexes :
@@ -1602,7 +1602,7 @@ def to_pandas_indexes(self) -> Indexes[pd.Index]:
1602
1602
return Indexes (indexes , self ._variables , index_type = pd .Index )
1603
1603
1604
1604
def copy_indexes (
1605
- self , deep : bool = True , memo : dict [int , Any ] | None = None
1605
+ self , deep : bool = True , memo : dict [int , T_PandasOrXarrayIndex ] | None = None
1606
1606
) -> tuple [dict [Hashable , T_PandasOrXarrayIndex ], dict [Hashable , Variable ]]:
1607
1607
"""Return a new dictionary with copies of indexes, preserving
1608
1608
unique indexes.
@@ -1619,6 +1619,7 @@ def copy_indexes(
1619
1619
new_indexes = {}
1620
1620
new_index_vars = {}
1621
1621
1622
+ idx : T_PandasOrXarrayIndex
1622
1623
for idx , coords in self .group_by_index ():
1623
1624
if isinstance (idx , pd .Index ):
1624
1625
convert_new_idx = True
0 commit comments