Skip to content

Commit

Permalink
Replacing groupBy count with len (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bidek56 committed Apr 17, 2024
1 parent 6c39439 commit 70204ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 6 additions & 5 deletions __tests__/groupby.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ describe("groupby", () => {
});
expect(actual).toFrameEqual(expected);
});

test("count", () => {
const actual = df.groupBy("name").count().sort("name");
test("len", () => {
let actual = df.groupBy("name").len().sort("name");
const expected = pl.DataFrame({
name: ["a", "b", "c"],
foo_count: [2, 2, 1],
bar_count: [2, 2, 1],
name_count: [2, 2, 1],
});
expect(actual).toFrameEqual(expected);
// Test for single column DF
actual = df.select("name").groupBy("name").len().sort("name");
expect(actual).toFrameEqual(expected);
});
test("first", () => {
const actual = df.groupBy("name").first().sort("name");
Expand Down
10 changes: 9 additions & 1 deletion polars/groupby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ export interface GroupBy {
agg(columns: Record<string, keyof Expr | (keyof Expr)[]>): DataFrame;
/**
* Count the number of values in each group.
* @deprecated @since 0.10.0 @use {@link len}
*/
count(): DataFrame;
/**
* Return the number of rows in each group.
*/
len(): DataFrame;
/**
* Aggregate the first values in the group.
*/
Expand Down Expand Up @@ -208,7 +213,10 @@ export function _GroupBy(df: any, by: string[], maintainOrder = false) {
pivot,
aggList: () => agg(exclude(by as any)),
count() {
return _DataFrame(df.groupby([by].flat(), null, "count"));
return _DataFrame(df.groupby([by].flat(), by, "count"));
},
len() {
return _DataFrame(df.groupby([by].flat(), by, "count"));
},
first: () => agg(exclude(by as any).first()),
groups() {
Expand Down

0 comments on commit 70204ab

Please sign in to comment.