Skip to content

Commit ce3b2df

Browse files
authored
Adding valueCounts implementation (#166)
1 parent 3e1bd99 commit ce3b2df

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

__tests__/series.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,14 @@ describe("series", () => {
574574
});
575575
expect(actual).toFrameEqual(expected);
576576
});
577+
it("series:valueCounts", () => {
578+
const actual = pl.Series("a", [1, 2, 2, 3]).valueCounts();
579+
const expected = pl.DataFrame({
580+
a: [2, 1, 3],
581+
count: [2, 1, 1],
582+
});
583+
expect(actual).toFrameEqual(expected);
584+
});
577585
it("set: expected matches actual", () => {
578586
const expected = pl.Series([99, 2, 3]);
579587
const mask = pl.Series([true, false, false]);

polars/series/index.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,8 @@ export interface Series
983983
unique(maintainOrder?: boolean | { maintainOrder: boolean }): Series;
984984
/**
985985
* __Count the unique values in a Series.__
986+
* @param sort - Sort the output by count in descending order.
987+
* If set to `False` (default), the order of the output is random.
986988
* ___
987989
* @example
988990
* ```
@@ -1002,7 +1004,7 @@ export interface Series
10021004
* ╰─────┴────────╯
10031005
* ```
10041006
*/
1005-
valueCounts(): DataFrame;
1007+
valueCounts(sort?: boolean): DataFrame;
10061008
/**
10071009
* Where mask evaluates true, take values from self.
10081010
*
@@ -1727,7 +1729,6 @@ export function _Series(_s: any): Series {
17271729
if (args[0] === "") {
17281730
return _s.toJs();
17291731
}
1730-
17311732
return _s.serialize("json").toString();
17321733
},
17331734
toObject() {
@@ -1739,8 +1740,8 @@ export function _Series(_s: any): Series {
17391740
}
17401741
return wrap("unique");
17411742
},
1742-
valueCounts() {
1743-
return null as any;
1743+
valueCounts(sorted?) {
1744+
return _DataFrame(unwrap("valueCounts", sorted ?? false));
17441745
},
17451746
values() {
17461747
return this[Symbol.iterator]();

0 commit comments

Comments
 (0)