16
16
import pandas as pd
17
17
from pandas .errors import OutOfBoundsDatetime
18
18
19
- from xarray .core .duck_array_ops import array_equiv
20
- from xarray .core .indexing import ExplicitlyIndexed , MemoryCachedArray
19
+ from xarray .core .duck_array_ops import array_equiv , astype
20
+ from xarray .core .indexing import MemoryCachedArray
21
21
from xarray .core .options import OPTIONS , _get_boolean_with_default
22
- from xarray .core .pycompat import array_type
22
+ from xarray .core .pycompat import array_type , to_duck_array , to_numpy
23
23
from xarray .core .utils import is_duck_array
24
24
25
25
if TYPE_CHECKING :
@@ -68,6 +68,8 @@ def first_n_items(array, n_desired):
68
68
# might not be a numpy.ndarray. Moreover, access to elements of the array
69
69
# could be very expensive (e.g. if it's only available over DAP), so go out
70
70
# of our way to get them in a single call to __getitem__ using only slices.
71
+ from xarray .core .variable import Variable
72
+
71
73
if n_desired < 1 :
72
74
raise ValueError ("must request at least one item" )
73
75
@@ -78,7 +80,14 @@ def first_n_items(array, n_desired):
78
80
if n_desired < array .size :
79
81
indexer = _get_indexer_at_least_n_items (array .shape , n_desired , from_end = False )
80
82
array = array [indexer ]
81
- return np .asarray (array ).flat [:n_desired ]
83
+
84
+ # We pass variable objects in to handle indexing
85
+ # with indexer above. It would not work with our
86
+ # lazy indexing classes at the moment, so we cannot
87
+ # pass Variable._data
88
+ if isinstance (array , Variable ):
89
+ array = array ._data
90
+ return np .ravel (to_duck_array (array ))[:n_desired ]
82
91
83
92
84
93
def last_n_items (array , n_desired ):
@@ -87,13 +96,22 @@ def last_n_items(array, n_desired):
87
96
# might not be a numpy.ndarray. Moreover, access to elements of the array
88
97
# could be very expensive (e.g. if it's only available over DAP), so go out
89
98
# of our way to get them in a single call to __getitem__ using only slices.
99
+ from xarray .core .variable import Variable
100
+
90
101
if (n_desired == 0 ) or (array .size == 0 ):
91
102
return []
92
103
93
104
if n_desired < array .size :
94
105
indexer = _get_indexer_at_least_n_items (array .shape , n_desired , from_end = True )
95
106
array = array [indexer ]
96
- return np .asarray (array ).flat [- n_desired :]
107
+
108
+ # We pass variable objects in to handle indexing
109
+ # with indexer above. It would not work with our
110
+ # lazy indexing classes at the moment, so we cannot
111
+ # pass Variable._data
112
+ if isinstance (array , Variable ):
113
+ array = array ._data
114
+ return np .ravel (to_duck_array (array ))[- n_desired :]
97
115
98
116
99
117
def last_item (array ):
@@ -103,7 +121,8 @@ def last_item(array):
103
121
return []
104
122
105
123
indexer = (slice (- 1 , None ),) * array .ndim
106
- return np .ravel (np .asarray (array [indexer ])).tolist ()
124
+ # to_numpy since dask doesn't support tolist
125
+ return np .ravel (to_numpy (array [indexer ])).tolist ()
107
126
108
127
109
128
def calc_max_rows_first (max_rows : int ) -> int :
@@ -171,10 +190,10 @@ def format_item(x, timedelta_format=None, quote_strings=True):
171
190
172
191
def format_items (x ):
173
192
"""Returns a succinct summaries of all items in a sequence as strings"""
174
- x = np . asarray (x )
193
+ x = to_duck_array (x )
175
194
timedelta_format = "datetime"
176
195
if np .issubdtype (x .dtype , np .timedelta64 ):
177
- x = np . asarray (x , dtype = "timedelta64[ns]" )
196
+ x = astype (x , dtype = "timedelta64[ns]" )
178
197
day_part = x [~ pd .isnull (x )].astype ("timedelta64[D]" ).astype ("timedelta64[ns]" )
179
198
time_needed = x [~ pd .isnull (x )] != day_part
180
199
day_needed = day_part != np .timedelta64 (0 , "ns" )
@@ -584,12 +603,9 @@ def limit_lines(string: str, *, limit: int):
584
603
def short_array_repr (array ):
585
604
from xarray .core .common import AbstractArray
586
605
587
- if isinstance (array , ExplicitlyIndexed ):
588
- array = array .get_duck_array ()
589
- elif isinstance (array , AbstractArray ):
606
+ if isinstance (array , AbstractArray ):
590
607
array = array .data
591
- if not is_duck_array (array ):
592
- array = np .asarray (array )
608
+ array = to_duck_array (array )
593
609
594
610
# default to lower precision so a full (abbreviated) line can fit on
595
611
# one line with the default display_width
0 commit comments