Skip to content

Commit 5d05a19

Browse files
Check nullCount in Series.toTypedArray (#207)
* check for empty validity mask in Series.toTypedArray * add test for Series.toTypedArray null handling * lint fix
1 parent 1a5c40e commit 5d05a19

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

__tests__/series.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,14 @@ describe("series", () => {
656656
const actual = s.round({ decimals: 2 });
657657
expect(actual).toSeriesEqual(expected);
658658
});
659+
test("toTypedArray handles nulls", () => {
660+
const s = pl.Series("ints and nulls", [1, 2, 3, null, 5], pl.UInt8);
661+
expect(() => s.toTypedArray()).toThrow();
662+
expect(() => s.dropNulls().toTypedArray()).not.toThrow();
663+
expect(s.dropNulls().toTypedArray()).toStrictEqual(
664+
new Uint8Array([1, 2, 3, 5]),
665+
);
666+
});
659667
});
660668
describe("comparators & math", () => {
661669
test("add/plus", () => {

polars/series/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ export function _Series(_s: any): Series {
17101710
return _s.toArray();
17111711
},
17121712
toTypedArray() {
1713-
if (!this.hasValidity()) {
1713+
if (!this.hasValidity() || this.nullCount() === 0) {
17141714
return _s.toTypedArray();
17151715
}
17161716
throw new Error("data contains nulls, unable to convert to TypedArray");

0 commit comments

Comments
 (0)