Skip to content

Commit 32aada9

Browse files
Adding expr pl.len() (pola-rs#258)
Adding expr pl.len() to close pola-rs#257 --------- Co-authored-by: Cory Grinstead <[email protected]>
1 parent 1828912 commit 32aada9

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

__tests__/expr.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,26 @@ describe("expr", () => {
517517
const actual = df.select(col("a").last().as("last"));
518518
expect(actual).toFrameEqual(expected);
519519
});
520+
test("len", () => {
521+
const df = pl.DataFrame({
522+
a: [1, 2, 3, 3, 3],
523+
b: ["a", "a", "b", "a", "a"],
524+
});
525+
let actual = df.select(pl.len());
526+
let expected = pl.DataFrame({ len: [5] });
527+
expect(actual).toFrameEqual(expected);
528+
529+
actual = df.withColumn(pl.len());
530+
expected = df.withColumn(pl.lit(5).alias("len"));
531+
expect(actual).toFrameEqual(expected);
532+
533+
actual = df.withColumn(pl.intRange(pl.len()).alias("index"));
534+
expected = df.withColumn(pl.Series("index", [0, 1, 2, 3, 4], pl.Int64));
535+
expect(actual).toFrameEqual(expected);
536+
537+
actual = df.groupBy("b").agg(pl.len());
538+
expect(actual.shape).toEqual({ height: 2, width: 2 });
539+
});
520540
test("list", () => {
521541
const df = pl.DataFrame({
522542
a: ["a", "b", "c"],

biome.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "https://biomejs.dev/schemas/1.0.0/schema.json",
2+
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
33
"organizeImports": {
44
"enabled": false
55
},

polars/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export namespace pl {
7272
export import groups = lazy.groups;
7373
export import head = lazy.head;
7474
export import last = lazy.last;
75+
export import len = lazy.len;
7576
export import mean = lazy.mean;
7677
export import median = lazy.median;
7778
export import nUnique = lazy.nUnique;

polars/lazy/functions.ts

+50-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ export function intRange(
208208
if (typeof opts?.low === "number") {
209209
return intRange(opts.low, opts.high, opts.step, opts.dtype, opts.eager);
210210
}
211+
// if expression like pl.len() passed
212+
if (high === undefined || high === null) {
213+
high = opts;
214+
opts = 0;
215+
}
211216
const low = exprToLitOrExpr(opts, false);
212217
high = exprToLitOrExpr(high, false);
213218
if (eager) {
@@ -472,7 +477,51 @@ export function head(column: Series | ExprOrString, n?): Series | Expr {
472477
}
473478
return exprToLitOrExpr(column, false).head(n);
474479
}
475-
480+
/** Return the number of elements in the column.
481+
This is similar to `COUNT(*)` in SQL.
482+
483+
@return Expr - Expression of data type :class:`UInt32`.
484+
@example
485+
```
486+
>>> const df = pl.DataFrame(
487+
... {
488+
... "a": [1, 2, None],
489+
... "b": [3, None, None],
490+
... "c": ["foo", "bar", "foo"],
491+
... }
492+
... )
493+
>>> df.select(pl.len())
494+
shape: (1, 1)
495+
┌─────┐
496+
│ len │
497+
│ --- │
498+
│ u32 │
499+
╞═════╡
500+
│ 3 │
501+
└─────┘
502+
```
503+
Generate an index column by using `len` in conjunction with :func:`intRange`.
504+
505+
```
506+
>>> df.select(
507+
... pl.intRange(pl.len(), dtype=pl.UInt32).alias("index"),
508+
... pl.all(),
509+
... )
510+
shape: (3, 4)
511+
┌───────┬──────┬──────┬─────┐
512+
│ index ┆ a ┆ b ┆ c │
513+
│ --- ┆ --- ┆ --- ┆ --- │
514+
│ u32 ┆ i64 ┆ i64 ┆ str │
515+
╞═══════╪══════╪══════╪═════╡
516+
│ 0 ┆ 1 ┆ 3 ┆ foo │
517+
│ 1 ┆ 2 ┆ null ┆ bar │
518+
│ 2 ┆ null ┆ null ┆ foo │
519+
└───────┴──────┴──────┴─────┘
520+
```
521+
*/
522+
export function len(): any {
523+
return _Expr(pli.len());
524+
}
476525
/** Get the last value. */
477526
export function last(column: ExprOrString | Series): any {
478527
if (Series.isSeries(column)) {

src/lazy/dataframe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,9 @@ impl JsLazyFrame {
490490
.into()
491491
}
492492
#[napi(catch_unwind)]
493-
pub fn slice(&self, offset: i64, len: u32) -> JsLazyFrame {
493+
pub fn slice(&self, offset: i64, length: u32) -> JsLazyFrame {
494494
let ldf = self.ldf.clone();
495-
ldf.slice(offset, len).into()
495+
ldf.slice(offset, length).into()
496496
}
497497
#[napi(catch_unwind)]
498498
pub fn tail(&self, n: u32) -> JsLazyFrame {

src/lazy/dsl.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,11 @@ pub fn spearman_rank_corr(
17191719
polars::lazy::dsl::spearman_rank_corr(a.0, b.0, ddof, propagate_nans).into()
17201720
}
17211721

1722+
#[napi(catch_unwind)]
1723+
pub fn len() -> JsExpr {
1724+
polars::lazy::dsl::len().into()
1725+
}
1726+
17221727
#[napi(catch_unwind)]
17231728
pub fn cov(a: Wrap<Expr>, b: Wrap<Expr>, ddof: u8) -> JsExpr {
17241729
polars::lazy::dsl::cov(a.0, b.0, ddof).into()

0 commit comments

Comments
 (0)