Skip to content

Commit

Permalink
Adding valueCounts implementation (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bidek56 authored Mar 7, 2024
1 parent 3e1bd99 commit ce3b2df
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 8 additions & 0 deletions __tests__/series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@ describe("series", () => {
});
expect(actual).toFrameEqual(expected);
});
it("series:valueCounts", () => {
const actual = pl.Series("a", [1, 2, 2, 3]).valueCounts();
const expected = pl.DataFrame({
a: [2, 1, 3],
count: [2, 1, 1],
});
expect(actual).toFrameEqual(expected);
});
it("set: expected matches actual", () => {
const expected = pl.Series([99, 2, 3]);
const mask = pl.Series([true, false, false]);
Expand Down
9 changes: 5 additions & 4 deletions polars/series/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,8 @@ export interface Series
unique(maintainOrder?: boolean | { maintainOrder: boolean }): Series;
/**
* __Count the unique values in a Series.__
* @param sort - Sort the output by count in descending order.
* If set to `False` (default), the order of the output is random.
* ___
* @example
* ```
Expand All @@ -1002,7 +1004,7 @@ export interface Series
* ╰─────┴────────╯
* ```
*/
valueCounts(): DataFrame;
valueCounts(sort?: boolean): DataFrame;
/**
* Where mask evaluates true, take values from self.
*
Expand Down Expand Up @@ -1727,7 +1729,6 @@ export function _Series(_s: any): Series {
if (args[0] === "") {
return _s.toJs();
}

return _s.serialize("json").toString();
},
toObject() {
Expand All @@ -1739,8 +1740,8 @@ export function _Series(_s: any): Series {
}
return wrap("unique");
},
valueCounts() {
return null as any;
valueCounts(sorted?) {
return _DataFrame(unwrap("valueCounts", sorted ?? false));
},
values() {
return this[Symbol.iterator]();
Expand Down

0 comments on commit ce3b2df

Please sign in to comment.