Take the last `n` rows of the last partition
| 2668 | |
| 2669 | |
| 2670 | class Tail(Expr): |
| 2671 | """Take the last `n` rows of the last partition""" |
| 2672 | |
| 2673 | _parameters = ["frame", "n"] |
| 2674 | _defaults = {"n": 5} |
| 2675 | |
| 2676 | @functools.cached_property |
| 2677 | def _meta(self): |
| 2678 | return self.frame._meta |
| 2679 | |
| 2680 | def _divisions(self): |
| 2681 | return self.frame.divisions[-2:] |
| 2682 | |
| 2683 | def _task(self, name: Key, index: int) -> Task: |
| 2684 | raise NotImplementedError() |
| 2685 | |
| 2686 | def _simplify_down(self): |
| 2687 | if isinstance(self.frame, Elemwise): |
| 2688 | operands = [ |
| 2689 | Tail(op, self.n) if isinstance(op, Expr) else op |
| 2690 | for op in self.frame.operands |
| 2691 | ] |
| 2692 | return type(self.frame)(*operands) |
| 2693 | if isinstance(self.frame, Tail): |
| 2694 | return Tail(self.frame.frame, min(self.n, self.frame.n)) |
| 2695 | |
| 2696 | def _simplify_up(self, parent, dependents): |
| 2697 | from dask.dataframe.dask_expr import Repartition |
| 2698 | |
| 2699 | if isinstance(parent, Repartition) and parent.new_partitions == 1: |
| 2700 | return self |
| 2701 | |
| 2702 | def _lower(self): |
| 2703 | if not isinstance(self, BlockwiseTail): |
| 2704 | # Lower to Blockwise |
| 2705 | if is_index_like(self._meta): |
| 2706 | return BlockwiseTailIndex( |
| 2707 | Partitions(self.frame, [self.frame.npartitions - 1]), self.n |
| 2708 | ) |
| 2709 | return BlockwiseTail( |
| 2710 | Partitions(self.frame, [self.frame.npartitions - 1]), self.n |
| 2711 | ) |
| 2712 | |
| 2713 | |
| 2714 | class BlockwiseTail(Tail, Blockwise): |