@@ -2590,12 +2590,14 @@ class IndexVariable(Variable):
2590
2590
unless another name is given.
2591
2591
"""
2592
2592
2593
- __slots__ = ()
2593
+ __slots__ = ("_name" , )
2594
2594
2595
2595
# TODO: PandasIndexingAdapter doesn't match the array api:
2596
2596
_data : PandasIndexingAdapter # type: ignore[assignment]
2597
2597
2598
- def __init__ (self , dims , data , attrs = None , encoding = None , fastpath = False ):
2598
+ def __init__ (
2599
+ self , dims , data , attrs = None , encoding = None , fastpath = False , name = None
2600
+ ):
2599
2601
super ().__init__ (dims , data , attrs , encoding , fastpath )
2600
2602
if self .ndim != 1 :
2601
2603
raise ValueError (f"{ type (self ).__name__ } objects must be 1-dimensional" )
@@ -2604,6 +2606,11 @@ def __init__(self, dims, data, attrs=None, encoding=None, fastpath=False):
2604
2606
if not isinstance (self ._data , PandasIndexingAdapter ):
2605
2607
self ._data = PandasIndexingAdapter (self ._data )
2606
2608
2609
+ if name is None :
2610
+ self ._name = self .dims [0 ]
2611
+ else :
2612
+ self ._name = name
2613
+
2607
2614
def __dask_tokenize__ (self ) -> object :
2608
2615
from dask .base import normalize_token
2609
2616
@@ -2753,7 +2760,22 @@ def copy(self, deep: bool = True, data: T_DuckArray | ArrayLike | None = None):
2753
2760
attrs = copy .deepcopy (self ._attrs ) if deep else copy .copy (self ._attrs )
2754
2761
encoding = copy .deepcopy (self ._encoding ) if deep else copy .copy (self ._encoding )
2755
2762
2756
- return self ._replace (data = ndata , attrs = attrs , encoding = encoding )
2763
+ copied = self ._replace (data = ndata , attrs = attrs , encoding = encoding )
2764
+
2765
+ return copied
2766
+
2767
+ def _replace (
2768
+ self ,
2769
+ dims = _default ,
2770
+ data = _default ,
2771
+ attrs = _default ,
2772
+ encoding = _default ,
2773
+ ) -> Self :
2774
+ replaced = super ()._replace (
2775
+ dims = dims , data = data , attrs = attrs , encoding = encoding
2776
+ )
2777
+ replaced ._name = self ._name
2778
+ return replaced
2757
2779
2758
2780
def equals (self , other , equiv = None ):
2759
2781
# if equiv is specified, super up
@@ -2825,7 +2847,7 @@ def get_level_variable(self, level):
2825
2847
2826
2848
@property
2827
2849
def name (self ) -> Hashable :
2828
- return self .dims [ 0 ]
2850
+ return self ._name
2829
2851
2830
2852
@name .setter
2831
2853
def name (self , value ) -> NoReturn :
0 commit comments