Skip to content

Commit e0d931e

Browse files
committed
implement object_codec
1 parent ffc42b7 commit e0d931e

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

zarr/storage.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def _require_parent_group(path, store, chunk_store, overwrite):
173173

174174
def init_array(store, shape, chunks=True, dtype=None, compressor='default',
175175
fill_value=None, order='C', overwrite=False, path=None,
176-
chunk_store=None, filters=None):
176+
chunk_store=None, filters=None, object_codec=None):
177177
"""Initialize an array store with the given configuration. Note that this is a low-level
178178
function and there should be no need to call this directly from user code.
179179
@@ -203,6 +203,8 @@ def init_array(store, shape, chunks=True, dtype=None, compressor='default',
203203
for storage of both chunks and metadata.
204204
filters : sequence, optional
205205
Sequence of filters to use to encode chunk data prior to compression.
206+
object_codec : Codec, optional
207+
A codec to encode object arrays, only needed if dtype=object.
206208
207209
Examples
208210
--------
@@ -284,12 +286,13 @@ def init_array(store, shape, chunks=True, dtype=None, compressor='default',
284286
_init_array_metadata(store, shape=shape, chunks=chunks, dtype=dtype,
285287
compressor=compressor, fill_value=fill_value,
286288
order=order, overwrite=overwrite, path=path,
287-
chunk_store=chunk_store, filters=filters)
289+
chunk_store=chunk_store, filters=filters,
290+
object_codec=object_codec)
288291

289292

290293
def _init_array_metadata(store, shape, chunks=None, dtype=None, compressor='default',
291294
fill_value=None, order='C', overwrite=False, path=None,
292-
chunk_store=None, filters=None):
295+
chunk_store=None, filters=None, object_codec=None):
293296

294297
# guard conditions
295298
if overwrite:
@@ -334,6 +337,19 @@ def _init_array_metadata(store, shape, chunks=None, dtype=None, compressor='defa
334337
if filters:
335338
filters_config = [f.get_config() for f in filters]
336339
else:
340+
filters_config = []
341+
342+
# deal with object encoding
343+
if dtype == object:
344+
if object_codec is None:
345+
raise ValueError('an object_codec is required for object arrays')
346+
else:
347+
filters_config.insert(0, object_codec.get_config())
348+
elif object_codec is not None:
349+
raise ValueError('an object_codec is only needed for object arrays')
350+
351+
# use null to indicate no filters
352+
if not filters_config:
337353
filters_config = None
338354

339355
# initialize metadata

0 commit comments

Comments
 (0)