Skip to content

Commit 89865eb

Browse files
authored
fix(dtype): consider #DIV/0! cells to be null when guessing a column's dtype (#417)
* fix(dtype): consider #DIV/0! cells to be null when guessing a column's dtype Signed-off-by: Luka Peschke <luka.peschke@toucantoco.com> * missing fixture Signed-off-by: Luka Peschke <luka.peschke@toucantoco.com> --------- Signed-off-by: Luka Peschke <luka.peschke@toucantoco.com>
1 parent 647c92f commit 89865eb

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

python/tests/test_dtypes.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,44 @@ def item_to_polars(item: Any):
508508
assert cell_errors is not None
509509
error_positions = [err.offset_position for err in cell_errors.errors]
510510
assert error_positions == expected_error_positions
511+
512+
513+
def test_guess_dtypes_with_div0_error() -> None:
514+
excel_reader = fastexcel.read_excel(path_for_fixture("div0.xlsx"))
515+
sheet = excel_reader.load_sheet(0)
516+
assert sheet.available_columns() == [
517+
fastexcel.ColumnInfo(
518+
name="dividend",
519+
index=0,
520+
dtype="float",
521+
dtype_from="guessed",
522+
column_name_from="looked_up",
523+
),
524+
fastexcel.ColumnInfo(
525+
name="divisor",
526+
index=1,
527+
dtype="float",
528+
dtype_from="guessed",
529+
column_name_from="looked_up",
530+
),
531+
fastexcel.ColumnInfo(
532+
name="quotient",
533+
index=2,
534+
dtype="float",
535+
dtype_from="guessed",
536+
column_name_from="looked_up",
537+
),
538+
]
539+
expected_data = {
540+
"dividend": [42.0, 43.0, 44.0, 45.0],
541+
"divisor": [0.0, 1.0, 2.0, 3.0],
542+
"quotient": [None, 43.0, 22.0, 15.0],
543+
}
544+
545+
pd_df = sheet.to_pandas()
546+
pd_expected_data = pd.DataFrame(expected_data)
547+
pd_assert_frame_equal(pd_df, pd_expected_data)
548+
549+
pl_df = sheet.to_polars()
550+
pl_expected_data = pl.DataFrame(expected_data)
551+
pl_assert_frame_equal(pl_df, pl_expected_data)

src/types/dtype/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ fn get_cell_dtype<DT: CellType + Debug + DataType>(
175175
| CellErrorType::Value
176176
| CellErrorType::Null
177177
| CellErrorType::Ref
178-
| CellErrorType::Num,
178+
| CellErrorType::Num
179+
| CellErrorType::Div0,
179180
) => Ok(DType::Null),
180181
Some(err) => Err(FastExcelErrorKind::CalamineCellError(err.to_owned()).into()),
181182
None => Err(FastExcelErrorKind::Internal(format!(

tests/fixtures/div0.xlsx

5.64 KB
Binary file not shown.

0 commit comments

Comments
 (0)