Skip to content

Commit

Permalink
perf(python): Avoid dispatching Series.head/tail to the expression …
Browse files Browse the repository at this point in the history
…engine (#12946)
  • Loading branch information
mcrumiller committed Dec 11, 2023
1 parent 0c0786d commit 1d0e077
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2948,7 +2948,7 @@ def head(self, n: int = 10) -> Series:
"""
if n < 0:
n = max(0, self.len() + n)
return self.to_frame().select(F.col(self.name).head(n)).to_series()
return self._from_pyseries(self._s.head(n))

def tail(self, n: int = 10) -> Series:
"""
Expand Down Expand Up @@ -2989,7 +2989,7 @@ def tail(self, n: int = 10) -> Series:
"""
if n < 0:
n = max(0, self.len() + n)
return self.to_frame().select(F.col(self.name).tail(n)).to_series()
return self._from_pyseries(self._s.tail(n))

def limit(self, n: int = 10) -> Series:
"""
Expand Down
8 changes: 8 additions & 0 deletions py-polars/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,14 @@ impl PySeries {
self.series.clear().into()
}

fn head(&self, n: usize) -> Self {
self.series.head(Some(n)).into()
}

fn tail(&self, n: usize) -> Self {
self.series.tail(Some(n)).into()
}

fn hist(&self, bins: Option<Self>, bin_count: Option<usize>) -> PyResult<PyDataFrame> {
let bins = bins.map(|s| s.series);
let out = hist(&self.series, bins.as_ref(), bin_count).map_err(PyPolarsErr::from)?;
Expand Down

0 comments on commit 1d0e077

Please sign in to comment.