Take the first `n` rows of the first partition
| 2545 | |
| 2546 | |
| 2547 | class Head(Expr): |
| 2548 | """Take the first `n` rows of the first partition""" |
| 2549 | |
| 2550 | _parameters = ["frame", "n", "npartitions"] |
| 2551 | _defaults = {"n": 5, "npartitions": 1} |
| 2552 | |
| 2553 | @functools.cached_property |
| 2554 | def _meta(self): |
| 2555 | return self.frame._meta |
| 2556 | |
| 2557 | @functools.cached_property |
| 2558 | def npartitions(self): |
| 2559 | return 1 |
| 2560 | |
| 2561 | def _divisions(self): |
| 2562 | if self.operand("npartitions") <= -1: |
| 2563 | return self.frame.divisions[0], self.frame.divisions[-1] |
| 2564 | return ( |
| 2565 | self.frame.divisions[0], |
| 2566 | self.frame.divisions[self.operand("npartitions")], |
| 2567 | ) |
| 2568 | |
| 2569 | def _task(self, name: Key, index: int) -> Task: |
| 2570 | raise NotImplementedError() |
| 2571 | |
| 2572 | def _simplify_down(self): |
| 2573 | if isinstance(self.frame, Elemwise): |
| 2574 | operands = [ |
| 2575 | ( |
| 2576 | Head(op, self.n, self.operand("npartitions")) |
| 2577 | if isinstance(op, Expr) and not isinstance(op, _DelayedExpr) |
| 2578 | else op |
| 2579 | ) |
| 2580 | for op in self.frame.operands |
| 2581 | ] |
| 2582 | return type(self.frame)(*operands) |
| 2583 | if isinstance(self.frame, Head): |
| 2584 | return Head( |
| 2585 | self.frame.frame, min(self.n, self.frame.n), self.operand("npartitions") |
| 2586 | ) |
| 2587 | |
| 2588 | def _simplify_up(self, parent, dependents): |
| 2589 | from dask.dataframe.dask_expr import Repartition |
| 2590 | |
| 2591 | if isinstance(parent, Repartition) and parent.new_partitions == 1: |
| 2592 | return self |
| 2593 | |
| 2594 | def _lower(self): |
| 2595 | if not isinstance(self, BlockwiseHead): |
| 2596 | # Lower to Blockwise |
| 2597 | npartitions = self.operand("npartitions") |
| 2598 | if self.operand("npartitions") > self.frame.npartitions: |
| 2599 | raise ValueError( |
| 2600 | f"only {self.frame.npartitions} partitions, head received {npartitions}" |
| 2601 | ) |
| 2602 | partitions = self._partitions |
| 2603 | if is_index_like(self._meta): |
| 2604 | return BlockwiseHeadIndex( |
no outgoing calls
no test coverage detected