Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ The DataFrame class supports also methods for filtering out or changing null val
const df = readCsv('test.csv')
df.dropNull() // drop null
df.fillNull() // fill null values with the zero value of their type
df.fillNull('a string', 0.5, 4, true) // fill null values with custom values
```

> NOTE: `fillNull` works with zero values, which means: `""` for string, `0` for integer and float, `false` for boolean

You can filter out and change NaN values as well (only applies if there are float-typed columns):

```typescript
Expand Down
20 changes: 19 additions & 1 deletion __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ test('Reading null values works correctly', (t) => {
t.deepEqual(col4, [true, false, true, false, null])
})

test('Filling null values works correctly', (t) => {
test('Filling null values with zero values works correctly', (t) => {
const df = readCsv('testfiles/with-nulls.csv')
df.fillNull()
t.is(df.len, 5)
Expand All @@ -245,6 +245,24 @@ test('Filling null values works correctly', (t) => {
t.deepEqual(col4, [true, false, true, false, false])
})

test('Filling null values with custom values works correctly', (t) => {
const df = readCsv('testfiles/with-nulls.csv')
df.fillNull('test', 99.3, 77, true)
t.is(df.len, 5)
const col1 = asStringArray(df.get('col1')!)!
t.is(col1.filter((x) => x === null).length, 0)
t.deepEqual(col1, ['hello', 'test', 'bye', 'test', 'ciao'])
const col2 = asIntArray(df.get('col2')!)!
t.is(col2.filter((x) => x === null).length, 0)
t.deepEqual(col2, [1, 2, 77, 3, 4])
const col3 = asFloatArray(df.get('col3')!)!
t.is(col3.filter((x) => x === null).length, 0)
t.deepEqual(col3, [0.1, 0.2, 0.3, 99.3, 0.4])
const col4 = asBooleanArray(df.get('col4')!)!
t.is(col4.filter((x) => x === null).length, 0)
t.deepEqual(col4, [true, false, true, false, true])
})

test('Dropping NaN values works correctly', (t) => {
const col1 = toStringColumn(['hello', 'world', null, 'bye', 'moon', 'hey'])
const col2 = toFloatColumn([1.2, 2.3, 0.3, NaN, 0.5, 8.9])
Expand Down
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ export declare class DataFrame {
dropNan(): void
fillNan(fillValue?: number | undefined | null): void
dropNull(): void
fillNull(): void
fillNull(
fillString?: string | undefined | null,
fillFloat?: number | undefined | null,
fillInt?: number | undefined | null,
fillBool?: boolean | undefined | null,
): void
writeCsv(path: string): void
}

Expand Down
20 changes: 15 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,34 +343,44 @@ impl DataFrame {
}

#[napi]
pub fn fill_null(&mut self) {
pub fn fill_null(
&mut self,
fill_string: Option<String>,
fill_float: Option<f64>,
fill_int: Option<i64>,
fill_bool: Option<bool>,
) {
let str_val = fill_string.unwrap_or_default();
let f64_val = fill_float.unwrap_or_default();
let i64_val = fill_int.unwrap_or_default();
let bool_val = fill_bool.unwrap_or_default();
for col in self.columns.values_mut() {
match col {
ColumnData::Boolean(b) => {
for item in b.iter_mut().take(self.len as usize) {
if item.is_none() {
*item = Some(false);
*item = Some(bool_val);
}
}
}
ColumnData::Float(f) => {
for item in f.iter_mut().take(self.len as usize) {
if item.is_none() {
*item = Some(0_f64);
*item = Some(f64_val);
}
}
}
ColumnData::String(s) => {
for item in s.iter_mut().take(self.len as usize) {
if item.is_none() {
*item = Some(String::new());
*item = Some(str_val.to_owned());
}
}
}
ColumnData::Integer(j) => {
for item in j.iter_mut().take(self.len as usize) {
if item.is_none() {
*item = Some(0_i64);
*item = Some(i64_val);
}
}
}
Expand Down