@@ -1590,7 +1590,10 @@ def _chunk_getitem(self, chunk_coords, chunk_selection, out, out_selection,
1590
1590
if self ._compressor :
1591
1591
self ._compressor .decode (cdata , dest )
1592
1592
else :
1593
- chunk = np .frombuffer (cdata , dtype = self ._dtype )
1593
+ if isinstance (cdata , np .ndarray ):
1594
+ chunk = cdata .view (self ._dtype )
1595
+ else :
1596
+ chunk = np .frombuffer (cdata , dtype = self ._dtype )
1594
1597
chunk = chunk .reshape (self ._chunks , order = self ._order )
1595
1598
np .copyto (dest , chunk )
1596
1599
return
@@ -1736,7 +1739,7 @@ def _decode_chunk(self, cdata):
1736
1739
elif isinstance (chunk , np .ndarray ):
1737
1740
chunk = chunk .view (self ._dtype )
1738
1741
else :
1739
- chunk = np .frombuffer (chunk , self ._dtype )
1742
+ chunk = np .frombuffer (chunk , dtype = self ._dtype )
1740
1743
1741
1744
# reshape
1742
1745
chunk = chunk .reshape (self ._chunks , order = self ._order )
@@ -2087,15 +2090,15 @@ def view(self, shape=None, chunks=None, dtype=None,
2087
2090
>>> import zarr
2088
2091
>>> import numpy as np
2089
2092
>>> np.random.seed(42)
2090
- >>> labels = [b 'female', b 'male']
2093
+ >>> labels = ['female', 'male']
2091
2094
>>> data = np.random.choice(labels, size=10000)
2092
2095
>>> filters = [zarr.Categorize(labels=labels,
2093
- ... dtype=data.dtype,
2094
- ... astype='u1')]
2096
+ ... dtype=data.dtype,
2097
+ ... astype='u1')]
2095
2098
>>> a = zarr.array(data, chunks=1000, filters=filters)
2096
2099
>>> a[:]
2097
- array([b 'female', b 'male', b 'female', ..., b 'male', b 'male', b 'female'],
2098
- dtype='|S6 ')
2100
+ array(['female', 'male', 'female', ..., 'male', 'male', 'female'],
2101
+ dtype='<U6 ')
2099
2102
>>> v = a.view(dtype='u1', filters=[])
2100
2103
>>> v.is_view
2101
2104
True
@@ -2110,10 +2113,10 @@ def view(self, shape=None, chunks=None, dtype=None,
2110
2113
>>> v[:]
2111
2114
array([1, 1, 1, ..., 2, 2, 2], dtype=uint8)
2112
2115
>>> a[:]
2113
- array([b 'female', b 'female', b 'female', ..., b 'male', b 'male', b 'male'],
2114
- dtype='|S6 ')
2116
+ array(['female', 'female', 'female', ..., 'male', 'male', 'male'],
2117
+ dtype='<U6 ')
2115
2118
2116
- View as a different dtype with the same itemsize :
2119
+ View as a different dtype with the same item size :
2117
2120
2118
2121
>>> data = np.random.randint(0, 2, size=10000, dtype='u1')
2119
2122
>>> a = zarr.array(data, chunks=1000)
@@ -2125,7 +2128,7 @@ def view(self, shape=None, chunks=None, dtype=None,
2125
2128
>>> np.all(a[:].view(dtype=bool) == v[:])
2126
2129
True
2127
2130
2128
- An array can be viewed with a dtype with a different itemsize , however
2131
+ An array can be viewed with a dtype with a different item size , however
2129
2132
some care is needed to adjust the shape and chunk shape so that chunk
2130
2133
data is interpreted correctly:
2131
2134
0 commit comments