Skip to content

Commit

Permalink
Check nullCount in Series.toTypedArray (#207)
Browse files Browse the repository at this point in the history
* check for empty validity mask in Series.toTypedArray

* add test for Series.toTypedArray null handling

* lint fix
  • Loading branch information
controversial committed Apr 29, 2024
1 parent 1a5c40e commit 5d05a19
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions __tests__/series.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,14 @@ describe("series", () => {
const actual = s.round({ decimals: 2 });
expect(actual).toSeriesEqual(expected);
});
test("toTypedArray handles nulls", () => {
const s = pl.Series("ints and nulls", [1, 2, 3, null, 5], pl.UInt8);
expect(() => s.toTypedArray()).toThrow();
expect(() => s.dropNulls().toTypedArray()).not.toThrow();
expect(s.dropNulls().toTypedArray()).toStrictEqual(
new Uint8Array([1, 2, 3, 5]),
);
});
});
describe("comparators & math", () => {
test("add/plus", () => {
Expand Down
2 changes: 1 addition & 1 deletion polars/series/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ export function _Series(_s: any): Series {
return _s.toArray();
},
toTypedArray() {
if (!this.hasValidity()) {
if (!this.hasValidity() || this.nullCount() === 0) {
return _s.toTypedArray();
}
throw new Error("data contains nulls, unable to convert to TypedArray");
Expand Down

0 comments on commit 5d05a19

Please sign in to comment.