@@ -417,3 +417,193 @@ def test_update_attrs(zarr_format: int) -> None:
417417
418418 arr2 = zarr .open_array (store = store , zarr_format = zarr_format )
419419 assert arr2 .attrs ["foo" ] == "bar"
420+
421+
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+ with pytest .warns (DeprecationWarning ):
457+ z .shape = new_shape
458+ assert new_shape == z .shape
459+ assert new_shape == z [:].shape
460+
461+
462+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
463+ @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
464+ def test_resize_2d (store : MemoryStore , zarr_format : int ) -> None :
465+ z = zarr .create (
466+ shape = (105 , 105 ),
467+ chunks = (10 , 10 ),
468+ dtype = "i4" ,
469+ fill_value = 0 ,
470+ store = store ,
471+ zarr_format = zarr_format ,
472+ )
473+ a = np .arange (105 * 105 , dtype = "i4" ).reshape ((105 , 105 ))
474+ z [:] = a
475+ assert (105 , 105 ) == z .shape
476+ assert (105 , 105 ) == z [:].shape
477+ assert np .dtype ("i4" ) == z .dtype
478+ assert np .dtype ("i4" ) == z [:].dtype
479+ assert (10 , 10 ) == z .chunks
480+ np .testing .assert_array_equal (a , z [:])
481+
482+ z .resize ((205 , 205 ))
483+ assert (205 , 205 ) == z .shape
484+ assert (205 , 205 ) == z [:].shape
485+ assert np .dtype ("i4" ) == z .dtype
486+ assert np .dtype ("i4" ) == z [:].dtype
487+ assert (10 , 10 ) == z .chunks
488+ np .testing .assert_array_equal (a , z [:105 , :105 ])
489+ np .testing .assert_array_equal (np .zeros ((100 , 205 ), dtype = "i4" ), z [105 :, :])
490+ np .testing .assert_array_equal (np .zeros ((205 , 100 ), dtype = "i4" ), z [:, 105 :])
491+
492+ z .resize ((55 , 55 ))
493+ assert (55 , 55 ) == z .shape
494+ assert (55 , 55 ) == z [:].shape
495+ assert np .dtype ("i4" ) == z .dtype
496+ assert np .dtype ("i4" ) == z [:].dtype
497+ assert (10 , 10 ) == z .chunks
498+ np .testing .assert_array_equal (a [:55 , :55 ], z [:])
499+
500+ z .resize ((55 , 1 ))
501+ assert (55 , 1 ) == z .shape
502+ assert (55 , 1 ) == z [:].shape
503+ assert np .dtype ("i4" ) == z .dtype
504+ assert np .dtype ("i4" ) == z [:].dtype
505+ assert (10 , 10 ) == z .chunks
506+ np .testing .assert_array_equal (a [:55 , :1 ], z [:])
507+
508+ z .resize ((1 , 55 ))
509+ assert (1 , 55 ) == z .shape
510+ assert (1 , 55 ) == z [:].shape
511+ assert np .dtype ("i4" ) == z .dtype
512+ assert np .dtype ("i4" ) == z [:].dtype
513+ assert (10 , 10 ) == z .chunks
514+ np .testing .assert_array_equal (a [:1 , :10 ], z [:, :10 ])
515+ np .testing .assert_array_equal (np .zeros ((1 , 55 - 10 ), dtype = "i4" ), z [:, 10 :55 ])
516+
517+ # via shape setter
518+ new_shape = (105 , 105 )
519+ with pytest .warns (DeprecationWarning ):
520+ z .shape = new_shape
521+ assert new_shape == z .shape
522+ assert new_shape == z [:].shape
523+
524+
525+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
526+ @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
527+ def test_append_1d (store : MemoryStore , zarr_format : int ) -> None :
528+ a = np .arange (105 )
529+ z = zarr .create (shape = a .shape , chunks = 10 , dtype = a .dtype , store = store , zarr_format = zarr_format )
530+ z [:] = a
531+ assert a .shape == z .shape
532+ assert a .dtype == z .dtype
533+ assert (10 ,) == z .chunks
534+ np .testing .assert_array_equal (a , z [:])
535+
536+ b = np .arange (105 , 205 )
537+ e = np .append (a , b )
538+ assert z .shape == (105 ,)
539+ z .append (b )
540+ assert e .shape == z .shape
541+ assert e .dtype == z .dtype
542+ assert (10 ,) == z .chunks
543+ np .testing .assert_array_equal (e , z [:])
544+
545+ # check append handles array-like
546+ c = [1 , 2 , 3 ]
547+ f = np .append (e , c )
548+ z .append (c )
549+ assert f .shape == z .shape
550+ assert f .dtype == z .dtype
551+ assert (10 ,) == z .chunks
552+ np .testing .assert_array_equal (f , z [:])
553+
554+
555+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
556+ @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
557+ def test_append_2d (store : MemoryStore , zarr_format : int ) -> None :
558+ a = np .arange (105 * 105 , dtype = "i4" ).reshape ((105 , 105 ))
559+ z = zarr .create (
560+ shape = a .shape , chunks = (10 , 10 ), dtype = a .dtype , store = store , zarr_format = zarr_format
561+ )
562+ z [:] = a
563+ assert a .shape == z .shape
564+ assert a .dtype == z .dtype
565+ assert (10 , 10 ) == z .chunks
566+ actual = z [:]
567+ np .testing .assert_array_equal (a , actual )
568+
569+ b = np .arange (105 * 105 , 2 * 105 * 105 , dtype = "i4" ).reshape ((105 , 105 ))
570+ e = np .append (a , b , axis = 0 )
571+ z .append (b )
572+ assert e .shape == z .shape
573+ assert e .dtype == z .dtype
574+ assert (10 , 10 ) == z .chunks
575+ actual = z [:]
576+ np .testing .assert_array_equal (e , actual )
577+
578+
579+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
580+ @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
581+ def test_append_2d_axis (store : MemoryStore , zarr_format : int ) -> None :
582+ a = np .arange (105 * 105 , dtype = "i4" ).reshape ((105 , 105 ))
583+ z = zarr .create (
584+ shape = a .shape , chunks = (10 , 10 ), dtype = a .dtype , store = store , zarr_format = zarr_format
585+ )
586+ z [:] = a
587+ assert a .shape == z .shape
588+ assert a .dtype == z .dtype
589+ assert (10 , 10 ) == z .chunks
590+ np .testing .assert_array_equal (a , z [:])
591+
592+ b = np .arange (105 * 105 , 2 * 105 * 105 , dtype = "i4" ).reshape ((105 , 105 ))
593+ e = np .append (a , b , axis = 1 )
594+ z .append (b , axis = 1 )
595+ assert e .shape == z .shape
596+ assert e .dtype == z .dtype
597+ assert (10 , 10 ) == z .chunks
598+ np .testing .assert_array_equal (e , z [:])
599+
600+
601+ @pytest .mark .parametrize ("store" , ["memory" ], indirect = True )
602+ @pytest .mark .parametrize ("zarr_format" , [2 , 3 ])
603+ def test_append_bad_shape (store : MemoryStore , zarr_format : int ) -> None :
604+ a = np .arange (100 )
605+ z = zarr .create (shape = a .shape , chunks = 10 , dtype = a .dtype , store = store , zarr_format = zarr_format )
606+ z [:] = a
607+ b = a .reshape (10 , 10 )
608+ with pytest .raises (ValueError ):
609+ z .append (b )
0 commit comments