@@ -641,6 +641,84 @@ orthogonal indexing is also available directly on the array:
641
641
>>> all (z.oindex[[0 , 2 ], :] == z[[0 , 2 ], :])
642
642
True
643
643
644
+ Block Indexing
645
+ ~~~~~~~~~~~~~~
646
+
647
+ As of version 2.16.0, Zarr also support block indexing, which allows
648
+ selections of whole chunks based on their logical indices along each dimension
649
+ of an array. For example, this allows selecting a subset of chunk aligned rows and/or
650
+ columns from a 2-dimensional array. E.g.::
651
+
652
+ >>> import zarr
653
+ >>> import numpy as np
654
+ >>> z = zarr.array(np.arange(100).reshape(10, 10), chunks=(3, 3))
655
+
656
+ Retrieve items by specifying their block coordinates::
657
+
658
+ >>> z.get_block_selection(1)
659
+ array([[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
660
+ [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
661
+ [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]])
662
+
663
+ Equivalent slicing::
664
+
665
+ >>> z[3:6]
666
+ array([[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
667
+ [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
668
+ [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]])
669
+
670
+
671
+ For convenience, the block selection functionality is also available via the
672
+ `blocks ` property, e.g.::
673
+
674
+ >>> z.blocks[1]
675
+ array([[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
676
+ [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
677
+ [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]])
678
+
679
+ Block index arrays may be multidimensional to index multidimensional arrays.
680
+ For example::
681
+
682
+ >>> z.blocks[0, 1:3]
683
+ array([[ 3, 4, 5, 6, 7, 8],
684
+ [13, 14, 15, 16, 17, 18],
685
+ [23, 24, 25, 26, 27, 28]])
686
+
687
+ Data can also be modified. Let's start by a simple 2D array::
688
+
689
+ >>> import zarr
690
+ >>> import numpy as np
691
+ >>> z = zarr.zeros((6, 6), dtype=int, chunks=2)
692
+
693
+ Set data for a selection of items::
694
+
695
+ >>> z.set_block_selection((1, 0), 1)
696
+ >>> z[...]
697
+ array([[0, 0, 0, 0, 0, 0],
698
+ [0, 0, 0, 0, 0, 0],
699
+ [1, 1, 0, 0, 0, 0],
700
+ [1, 1, 0, 0, 0, 0],
701
+ [0, 0, 0, 0, 0, 0],
702
+ [0, 0, 0, 0, 0, 0]])
703
+
704
+ For convenience, this functionality is also available via the ``blocks `` property.
705
+ E.g.::
706
+
707
+ >>> z.blocks[:, 2] = 7
708
+ >>> z[...]
709
+ array([[0, 0, 0, 0, 7, 7],
710
+ [0, 0, 0, 0, 7, 7],
711
+ [1, 1, 0, 0, 7, 7],
712
+ [1, 1, 0, 0, 7, 7],
713
+ [0, 0, 0, 0, 7, 7],
714
+ [0, 0, 0, 0, 7, 7]])
715
+
716
+ Any combination of integer and slice can be used for block indexing::
717
+
718
+ >>> z.blocks[2, 1:3]
719
+ array([[0, 0, 7, 7],
720
+ [0, 0, 7, 7]])
721
+
644
722
Indexing fields in structured arrays
645
723
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
646
724
0 commit comments