13
13
14
14
import numpy as np
15
15
import pandas as pd
16
+ from packaging .version import Version
16
17
17
18
from xarray .core import duck_array_ops
18
19
from xarray .core .nputils import NumpyVIndexAdapter
@@ -505,9 +506,14 @@ class ExplicitlyIndexed:
505
506
506
507
__slots__ = ()
507
508
508
- def __array__ (self , dtype : np .typing .DTypeLike = None ) -> np .ndarray :
509
+ def __array__ (
510
+ self , dtype : np .typing .DTypeLike = None , / , * , copy : bool | None = None
511
+ ) -> np .ndarray :
509
512
# Leave casting to an array up to the underlying array type.
510
- return np .asarray (self .get_duck_array (), dtype = dtype )
513
+ if Version (np .__version__ ) >= Version ("2.0.0" ):
514
+ return np .asarray (self .get_duck_array (), dtype = dtype , copy = copy )
515
+ else :
516
+ return np .asarray (self .get_duck_array (), dtype = dtype )
511
517
512
518
def get_duck_array (self ):
513
519
return self .array
@@ -520,11 +526,6 @@ def get_duck_array(self):
520
526
key = BasicIndexer ((slice (None ),) * self .ndim )
521
527
return self [key ]
522
528
523
- def __array__ (self , dtype : np .typing .DTypeLike = None ) -> np .ndarray :
524
- # This is necessary because we apply the indexing key in self.get_duck_array()
525
- # Note this is the base class for all lazy indexing classes
526
- return np .asarray (self .get_duck_array (), dtype = dtype )
527
-
528
529
def _oindex_get (self , indexer : OuterIndexer ):
529
530
raise NotImplementedError (
530
531
f"{ self .__class__ .__name__ } ._oindex_get method should be overridden"
@@ -570,8 +571,13 @@ def __init__(self, array, indexer_cls: type[ExplicitIndexer] = BasicIndexer):
570
571
self .array = as_indexable (array )
571
572
self .indexer_cls = indexer_cls
572
573
573
- def __array__ (self , dtype : np .typing .DTypeLike = None ) -> np .ndarray :
574
- return np .asarray (self .get_duck_array (), dtype = dtype )
574
+ def __array__ (
575
+ self , dtype : np .typing .DTypeLike = None , / , * , copy : bool | None = None
576
+ ) -> np .ndarray :
577
+ if Version (np .__version__ ) >= Version ("2.0.0" ):
578
+ return np .asarray (self .get_duck_array (), dtype = dtype , copy = copy )
579
+ else :
580
+ return np .asarray (self .get_duck_array (), dtype = dtype )
575
581
576
582
def get_duck_array (self ):
577
583
return self .array .get_duck_array ()
@@ -830,9 +836,6 @@ def __init__(self, array):
830
836
def _ensure_cached (self ):
831
837
self .array = as_indexable (self .array .get_duck_array ())
832
838
833
- def __array__ (self , dtype : np .typing .DTypeLike = None ) -> np .ndarray :
834
- return np .asarray (self .get_duck_array (), dtype = dtype )
835
-
836
839
def get_duck_array (self ):
837
840
self ._ensure_cached ()
838
841
return self .array .get_duck_array ()
@@ -1674,15 +1677,21 @@ def __init__(self, array: pd.Index, dtype: DTypeLike = None):
1674
1677
def dtype (self ) -> np .dtype :
1675
1678
return self ._dtype
1676
1679
1677
- def __array__ (self , dtype : DTypeLike = None ) -> np .ndarray :
1680
+ def __array__ (
1681
+ self , dtype : np .typing .DTypeLike = None , / , * , copy : bool | None = None
1682
+ ) -> np .ndarray :
1678
1683
if dtype is None :
1679
1684
dtype = self .dtype
1680
1685
array = self .array
1681
1686
if isinstance (array , pd .PeriodIndex ):
1682
1687
with suppress (AttributeError ):
1683
1688
# this might not be public API
1684
1689
array = array .astype ("object" )
1685
- return np .asarray (array .values , dtype = dtype )
1690
+
1691
+ if Version (np .__version__ ) >= Version ("2.0.0" ):
1692
+ return np .asarray (array .values , dtype = dtype , copy = copy )
1693
+ else :
1694
+ return np .asarray (array .values , dtype = dtype )
1686
1695
1687
1696
def get_duck_array (self ) -> np .ndarray :
1688
1697
return np .asarray (self )
@@ -1831,15 +1840,17 @@ def __init__(
1831
1840
super ().__init__ (array , dtype )
1832
1841
self .level = level
1833
1842
1834
- def __array__ (self , dtype : DTypeLike = None ) -> np .ndarray :
1843
+ def __array__ (
1844
+ self , dtype : np .typing .DTypeLike = None , / , * , copy : bool | None = None
1845
+ ) -> np .ndarray :
1835
1846
if dtype is None :
1836
1847
dtype = self .dtype
1837
1848
if self .level is not None :
1838
1849
return np .asarray (
1839
1850
self .array .get_level_values (self .level ).values , dtype = dtype
1840
1851
)
1841
1852
else :
1842
- return super ().__array__ (dtype )
1853
+ return super ().__array__ (dtype , copy = copy )
1843
1854
1844
1855
def _convert_scalar (self , item ):
1845
1856
if isinstance (item , tuple ) and self .level is not None :
0 commit comments