File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -974,6 +974,18 @@ def head(self, **indexers_kwargs: int) -> Dataset:
974974 )
975975 return self ._head (** indexers_kwargs )
976976
977+ _tail = _whole_dataset_method ('tail' )
978+
979+ def tail (self , ** indexers_kwargs : int ) -> Dataset :
980+ """Return a Dataset with the last N elements of each dimension."""
981+ if not isinstance (self .ptransform , core .DatasetToChunks ):
982+ raise ValueError (
983+ 'tail() is only supported on untransformed datasets, with '
984+ 'ptransform=DatasetToChunks. This dataset has '
985+ f'ptransform={ self .ptransform } '
986+ )
987+ return self ._tail (** indexers_kwargs )
988+
977989 # thin wrappers around xarray methods
978990 __getitem__ = _whole_dataset_method ('__getitem__' )
979991 transpose = _whole_dataset_method ('transpose' )
Original file line number Diff line number Diff line change @@ -849,6 +849,17 @@ def test_head(self):
849849 ):
850850 beam_ds .map_blocks (lambda x : x ).head (x = 2 )
851851
852+ def test_tail (self ):
853+ ds = xarray .Dataset ({'foo' : ('x' , np .arange (10 ))})
854+ beam_ds = xbeam .Dataset .from_xarray (ds , {'x' : 5 })
855+
856+ tail_ds = beam_ds .tail (x = 2 )
857+ self .assertEqual (tail_ds .chunks , {'x' : 2 })
858+ self .assertRegex (tail_ds .ptransform .label , r'^from_xarray_\d+|tail_\d+$' )
859+ expected = ds .tail (x = 2 )
860+ actual = tail_ds .collect_with_direct_runner ()
861+ xarray .testing .assert_identical (expected , actual )
862+
852863 @parameterized .named_parameters (
853864 dict (
854865 testcase_name = 'no_chunking' ,
You can’t perform that action at this time.
0 commit comments