Skip to content

Commit 70204ab

Browse files
authored
Replacing groupBy count with len (#192)
1 parent 6c39439 commit 70204ab

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

__tests__/groupby.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ describe("groupby", () => {
4646
});
4747
expect(actual).toFrameEqual(expected);
4848
});
49-
50-
test("count", () => {
51-
const actual = df.groupBy("name").count().sort("name");
49+
test("len", () => {
50+
let actual = df.groupBy("name").len().sort("name");
5251
const expected = pl.DataFrame({
5352
name: ["a", "b", "c"],
54-
foo_count: [2, 2, 1],
55-
bar_count: [2, 2, 1],
53+
name_count: [2, 2, 1],
5654
});
5755
expect(actual).toFrameEqual(expected);
56+
// Test for single column DF
57+
actual = df.select("name").groupBy("name").len().sort("name");
58+
expect(actual).toFrameEqual(expected);
5859
});
5960
test("first", () => {
6061
const actual = df.groupBy("name").first().sort("name");

polars/groupby.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@ export interface GroupBy {
4646
agg(columns: Record<string, keyof Expr | (keyof Expr)[]>): DataFrame;
4747
/**
4848
* Count the number of values in each group.
49+
* @deprecated @since 0.10.0 @use {@link len}
4950
*/
5051
count(): DataFrame;
52+
/**
53+
* Return the number of rows in each group.
54+
*/
55+
len(): DataFrame;
5156
/**
5257
* Aggregate the first values in the group.
5358
*/
@@ -208,7 +213,10 @@ export function _GroupBy(df: any, by: string[], maintainOrder = false) {
208213
pivot,
209214
aggList: () => agg(exclude(by as any)),
210215
count() {
211-
return _DataFrame(df.groupby([by].flat(), null, "count"));
216+
return _DataFrame(df.groupby([by].flat(), by, "count"));
217+
},
218+
len() {
219+
return _DataFrame(df.groupby([by].flat(), by, "count"));
212220
},
213221
first: () => agg(exclude(by as any).first()),
214222
groups() {

0 commit comments

Comments
 (0)