@@ -419,6 +419,194 @@ def test_update_attrs(zarr_format: int) -> None:
419419 assert arr2 .attrs ["foo" ] == "bar"
420420
421421
422+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
423+ @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
424+ def test_resize_1d (store : MemoryStore , zarr_format : int ) -> None :
425+ z = zarr .create (
426+ shape = 105 , chunks = 10 , dtype = "i4" , fill_value = 0 , store = store , zarr_format = zarr_format
427+ )
428+ a = np .arange (105 , dtype = "i4" )
429+ z [:] = a
430+ assert (105 ,) == z .shape
431+ assert (105 ,) == z [:].shape
432+ assert np .dtype ("i4" ) == z .dtype
433+ assert np .dtype ("i4" ) == z [:].dtype
434+ assert (10 ,) == z .chunks
435+ np .testing .assert_array_equal (a , z [:])
436+
437+ z .resize (205 )
438+ assert (205 ,) == z .shape
439+ assert (205 ,) == z [:].shape
440+ assert np .dtype ("i4" ) == z .dtype
441+ assert np .dtype ("i4" ) == z [:].dtype
442+ assert (10 ,) == z .chunks
443+ np .testing .assert_array_equal (a , z [:105 ])
444+ np .testing .assert_array_equal (np .zeros (100 , dtype = "i4" ), z [105 :])
445+
446+ z .resize (55 )
447+ assert (55 ,) == z .shape
448+ assert (55 ,) == z [:].shape
449+ assert np .dtype ("i4" ) == z .dtype
450+ assert np .dtype ("i4" ) == z [:].dtype
451+ assert (10 ,) == z .chunks
452+ np .testing .assert_array_equal (a [:55 ], z [:])
453+
454+ # via shape setter
455+ new_shape = (105 ,)
456+ z .shape = new_shape
457+ assert new_shape == z .shape
458+ assert new_shape == z [:].shape
459+
460+
461+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
462+ @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
463+ def test_resize_2d (store : MemoryStore , zarr_format : int ) -> None :
464+ z = zarr .create (
465+ shape = (105 , 105 ),
466+ chunks = (10 , 10 ),
467+ dtype = "i4" ,
468+ fill_value = 0 ,
469+ store = store ,
470+ zarr_format = zarr_format ,
471+ )
472+ a = np .arange (105 * 105 , dtype = "i4" ).reshape ((105 , 105 ))
473+ z [:] = a
474+ assert (105 , 105 ) == z .shape
475+ assert (105 , 105 ) == z [:].shape
476+ assert np .dtype ("i4" ) == z .dtype
477+ assert np .dtype ("i4" ) == z [:].dtype
478+ assert (10 , 10 ) == z .chunks
479+ np .testing .assert_array_equal (a , z [:])
480+
481+ z .resize ((205 , 205 ))
482+ assert (205 , 205 ) == z .shape
483+ assert (205 , 205 ) == z [:].shape
484+ assert np .dtype ("i4" ) == z .dtype
485+ assert np .dtype ("i4" ) == z [:].dtype
486+ assert (10 , 10 ) == z .chunks
487+ np .testing .assert_array_equal (a , z [:105 , :105 ])
488+ np .testing .assert_array_equal (np .zeros ((100 , 205 ), dtype = "i4" ), z [105 :, :])
489+ np .testing .assert_array_equal (np .zeros ((205 , 100 ), dtype = "i4" ), z [:, 105 :])
490+
491+ z .resize ((55 , 55 ))
492+ assert (55 , 55 ) == z .shape
493+ assert (55 , 55 ) == z [:].shape
494+ assert np .dtype ("i4" ) == z .dtype
495+ assert np .dtype ("i4" ) == z [:].dtype
496+ assert (10 , 10 ) == z .chunks
497+ np .testing .assert_array_equal (a [:55 , :55 ], z [:])
498+
499+ z .resize ((55 , 1 ))
500+ assert (55 , 1 ) == z .shape
501+ assert (55 , 1 ) == z [:].shape
502+ assert np .dtype ("i4" ) == z .dtype
503+ assert np .dtype ("i4" ) == z [:].dtype
504+ assert (10 , 10 ) == z .chunks
505+ np .testing .assert_array_equal (a [:55 , :1 ], z [:])
506+
507+ z .resize ((1 , 55 ))
508+ assert (1 , 55 ) == z .shape
509+ assert (1 , 55 ) == z [:].shape
510+ assert np .dtype ("i4" ) == z .dtype
511+ assert np .dtype ("i4" ) == z [:].dtype
512+ assert (10 , 10 ) == z .chunks
513+ np .testing .assert_array_equal (a [:1 , :10 ], z [:, :10 ])
514+ np .testing .assert_array_equal (np .zeros ((1 , 55 - 10 ), dtype = "i4" ), z [:, 10 :55 ])
515+
516+ # via shape setter
517+ new_shape = (105 , 105 )
518+ z .shape = new_shape
519+ assert new_shape == z .shape
520+ assert new_shape == z [:].shape
521+
522+
523+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
524+ @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
525+ def test_append_1d (store : MemoryStore , zarr_format : int ) -> None :
526+ a = np .arange (105 )
527+ z = zarr .create (shape = a .shape , chunks = 10 , dtype = a .dtype , store = store , zarr_format = zarr_format )
528+ z [:] = a
529+ assert a .shape == z .shape
530+ assert a .dtype == z .dtype
531+ assert (10 ,) == z .chunks
532+ np .testing .assert_array_equal (a , z [:])
533+
534+ b = np .arange (105 , 205 )
535+ e = np .append (a , b )
536+ assert z .shape == (105 ,)
537+ z .append (b )
538+ assert e .shape == z .shape
539+ assert e .dtype == z .dtype
540+ assert (10 ,) == z .chunks
541+ np .testing .assert_array_equal (e , z [:])
542+
543+ # check append handles array-like
544+ c = [1 , 2 , 3 ]
545+ f = np .append (e , c )
546+ z .append (c )
547+ assert f .shape == z .shape
548+ assert f .dtype == z .dtype
549+ assert (10 ,) == z .chunks
550+ np .testing .assert_array_equal (f , z [:])
551+
552+
553+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
554+ @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
555+ def test_append_2d (store : MemoryStore , zarr_format : int ) -> None :
556+ a = np .arange (105 * 105 , dtype = "i4" ).reshape ((105 , 105 ))
557+ z = zarr .create (
558+ shape = a .shape , chunks = (10 , 10 ), dtype = a .dtype , store = store , zarr_format = zarr_format
559+ )
560+ z [:] = a
561+ assert a .shape == z .shape
562+ assert a .dtype == z .dtype
563+ assert (10 , 10 ) == z .chunks
564+ actual = z [:]
565+ np .testing .assert_array_equal (a , actual )
566+
567+ b = np .arange (105 * 105 , 2 * 105 * 105 , dtype = "i4" ).reshape ((105 , 105 ))
568+ e = np .append (a , b , axis = 0 )
569+ z .append (b )
570+ assert e .shape == z .shape
571+ assert e .dtype == z .dtype
572+ assert (10 , 10 ) == z .chunks
573+ actual = z [:]
574+ np .testing .assert_array_equal (e , actual )
575+
576+
577+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
578+ @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
579+ def test_append_2d_axis (store : MemoryStore , zarr_format : int ) -> None :
580+ a = np .arange (105 * 105 , dtype = "i4" ).reshape ((105 , 105 ))
581+ z = zarr .create (
582+ shape = a .shape , chunks = (10 , 10 ), dtype = a .dtype , store = store , zarr_format = zarr_format
583+ )
584+ z [:] = a
585+ assert a .shape == z .shape
586+ assert a .dtype == z .dtype
587+ assert (10 , 10 ) == z .chunks
588+ np .testing .assert_array_equal (a , z [:])
589+
590+ b = np .arange (105 * 105 , 2 * 105 * 105 , dtype = "i4" ).reshape ((105 , 105 ))
591+ e = np .append (a , b , axis = 1 )
592+ z .append (b , axis = 1 )
593+ assert e .shape == z .shape
594+ assert e .dtype == z .dtype
595+ assert (10 , 10 ) == z .chunks
596+ np .testing .assert_array_equal (e , z [:])
597+
598+
599+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
600+ @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
601+ def test_append_bad_shape (store : MemoryStore , zarr_format : int ) -> None :
602+ a = np .arange (100 )
603+ z = zarr .create (shape = a .shape , chunks = 10 , dtype = a .dtype , store = store , zarr_format = zarr_format )
604+ z [:] = a
605+ b = a .reshape (10 , 10 )
606+ with pytest .raises (ValueError ):
607+ z .append (b )
608+
609+
422610@pytest .mark .parametrize ("order" , ["C" , "F" , None ])
423611@pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
424612@pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
0 commit comments