diff --git a/__tests__/series.test.ts b/__tests__/series.test.ts index ab1c134df..7ac45aad2 100644 --- a/__tests__/series.test.ts +++ b/__tests__/series.test.ts @@ -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]); diff --git a/polars/series/index.ts b/polars/series/index.ts index 8a5438e34..1c6489ee9 100644 --- a/polars/series/index.ts +++ b/polars/series/index.ts @@ -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 * ``` @@ -1002,7 +1004,7 @@ export interface Series * ╰─────┴────────╯ * ``` */ - valueCounts(): DataFrame; + valueCounts(sort?: boolean): DataFrame; /** * Where mask evaluates true, take values from self. * @@ -1727,7 +1729,6 @@ export function _Series(_s: any): Series { if (args[0] === "") { return _s.toJs(); } - return _s.serialize("json").toString(); }, toObject() { @@ -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]();