0
# Array Creation and Initialization
1
2
Functions for creating zarr arrays with various initialization patterns. These provide the primary entry points for creating new arrays with different fill patterns and from existing data sources.
3
4
## Capabilities
5
6
### Creating Arrays from Data
7
8
```python { .api }
9
def array(data: ArrayLike, **kwargs) -> Array
10
```
11
12
Create a zarr array from existing array-like data (numpy arrays, lists, etc.).
13
14
**Parameters:**
15
- `data`: Array-like data to convert to zarr array (numpy arrays, lists, etc.)
16
- `**kwargs`: Additional keyword arguments passed through to create()
17
18
```python { .api }
19
def from_array(
20
store: StoreLike,
21
*,
22
data: ArrayLike,
23
write_data: bool = True,
24
chunks: Literal["auto", "keep"] | tuple[int, ...] = "keep",
25
fill_value: Any = None,
26
order: MemoryOrder = None,
27
zarr_format: int = None,
28
overwrite: bool = False,
29
**kwargs
30
) -> Array
31
```
32
33
Create a zarr array from an existing array. Similar to `array()` but with different parameter semantics.
34
35
### Creating Empty Arrays
36
37
```python { .api }
38
def create(
39
shape: tuple[int, ...] | int,
40
*,
41
chunks: tuple[int, ...] | int | bool = None,
42
dtype: str | np.dtype = None,
43
compressor: CompressorLike = "auto",
44
fill_value: Any = None,
45
order: MemoryOrder = None,
46
store: StoreLike = None,
47
overwrite: bool = False,
48
path: str = None,
49
zarr_format: int = None,
50
**kwargs
51
) -> Array
52
```
53
54
Create a new zarr array with specified shape and properties.
55
56
**Parameters:**
57
- `shape`: Shape of the array as tuple of integers
58
- `chunks`: Chunk shape. If None, uses auto-chunking
59
- `dtype`: Data type (default: 'float64')
60
- `compressor`: Compression codec ('default' uses configured default)
61
- `fill_value`: Fill value for uninitialized elements
62
- `order`: Memory layout ('C' for row-major, 'F' for column-major)
63
64
```python { .api }
65
def empty(shape: tuple[int, ...], **kwargs) -> Array
66
```
67
68
Create an uninitialized array (contents are undefined).
69
70
```python { .api }
71
def create_array(
72
store: StoreLike,
73
*,
74
name: str = None,
75
shape: tuple[int, ...] = None,
76
dtype: str | np.dtype = None,
77
data: np.ndarray = None,
78
chunks: tuple[int, ...] | Literal[\"auto\"] = \"auto\",
79
fill_value: Any = None,
80
zarr_format: int = 3,
81
overwrite: bool = False,
82
**kwargs
83
) -> Array
84
```
85
86
Create a new array in a specific store with fine-grained control over storage parameters.
87
88
### Creating Initialized Arrays
89
90
```python { .api }
91
def zeros(shape: tuple[int, ...], **kwargs) -> Array
92
```
93
94
Create an array filled with zeros.
95
96
```python { .api }
97
def ones(shape: tuple[int, ...], **kwargs) -> Array
98
```
99
100
Create an array filled with ones.
101
102
```python { .api }
103
def full(shape: tuple[int, ...], fill_value: Any, **kwargs) -> Array
104
```
105
106
Create an array filled with a specified value.
107
108
**Parameters:**
109
- `shape`: Shape of the array
110
- `fill_value`: Value to fill the array with
111
- `dtype`: Data type (inferred from fill_value if None)
112
113
### Creating Arrays Like Existing Arrays
114
115
```python { .api }
116
def empty_like(a: ArrayLike, **kwargs) -> Array
117
```
118
119
Create an uninitialized array with the same shape and properties as an existing array.
120
121
```python { .api }
122
def zeros_like(a: ArrayLike, **kwargs) -> Array
123
```
124
125
Create a zeros array with the same shape and properties as an existing array.
126
127
```python { .api }
128
def ones_like(a: ArrayLike, **kwargs) -> Array
129
```
130
131
Create a ones array with the same shape and properties as an existing array.
132
133
```python { .api }
134
def full_like(a: ArrayLike, fill_value: Any, **kwargs) -> Array
135
```
136
137
Create an array filled with a specified value, using the same shape and properties as an existing array.
138
139
## Type Definitions
140
141
```python { .api }
142
ArrayLike = Union[np.ndarray, Array, list, tuple]
143
StoreLike = Union[str, os.PathLike, Store, MutableMapping]
144
CompressorLike = Union[str, dict, Codec]
145
MemoryOrder = Literal['C', 'F']
146
```
147
148
## Usage Examples
149
150
### Basic Array Creation
151
152
```python
153
import zarr
154
import numpy as np
155
156
# Create from numpy array
157
np_data = np.random.random((100, 100))
158
z1 = zarr.array(np_data, chunks=(50, 50))
159
160
# Create new array with specific initialization
161
z2 = zarr.zeros((1000, 1000), chunks=(100, 100), dtype='float32')
162
z3 = zarr.ones((500, 500), chunks=(100, 100))
163
z4 = zarr.full((200, 200), fill_value=3.14, chunks=(50, 50))
164
```
165
166
### Advanced Creation with Compression
167
168
```python
169
from zarr.codecs import BloscCodec
170
171
# Create with compression
172
z = zarr.create(
173
shape=(10000, 10000),
174
chunks=(1000, 1000),
175
dtype='float64',
176
compressor=BloscCodec(cname='zstd', clevel=3)
177
)
178
179
# Create with multiple filters
180
from zarr.codecs import BytesCodec
181
z = zarr.create(
182
shape=(5000, 5000),
183
chunks=(500, 500),
184
codecs=[
185
BloscCodec(cname='lz4', clevel=1),
186
BytesCodec()
187
]
188
)
189
```