Skip to content

Commit

Permalink
fix: consider cells with invalid values as nulls (#223)
Browse files Browse the repository at this point in the history
closes #220

Signed-off-by: Luka Peschke <[email protected]>
  • Loading branch information
lukapeschke authored Apr 5, 2024
1 parent 21f7f22 commit 55577dd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Binary file not shown.
18 changes: 18 additions & 0 deletions python/tests/test_fastexcel.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,3 +548,21 @@ def test_null_strings(excel_file: str):
pl.col("TIMESTAMPS_AND_NULLS").dt.cast_time_unit("ms"),
)
pl_assert_frame_equal(sheet.to_polars(), pl_df)


def test_null_values_in_cells() -> None:
excel_reader = fastexcel.read_excel(path_for_fixture("fixture-invalid-cell-value.xlsx"))
sheet = excel_reader.load_sheet(0)

expected = {
"Title": ["A", "B", "C", "D"],
"Date": [None, None, datetime(2021, 1, 1), datetime(2021, 5, 5)],
}

pl_assert_frame_equal(
sheet.to_polars(),
pl.DataFrame(expected).with_columns(pl.col("Date").dt.cast_time_unit("ms")),
)
pd_expected = pd.DataFrame(expected)
pd_expected["Date"] = pd_expected["Date"].dt.as_unit("ms")
pd_assert_frame_equal(sheet.to_pandas(), pd_expected)
2 changes: 1 addition & 1 deletion src/types/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn get_cell_dtype(data: &Range<CalData>, row: usize, col: usize) -> FastExcelRes
CalData::DurationIso(_) => Ok(DType::Duration),
// Errors and nulls
CalData::Error(err) => match err {
CellErrorType::NA => Ok(DType::Null),
CellErrorType::NA | CellErrorType::Value | CellErrorType::Null => Ok(DType::Null),
_ => Err(FastExcelErrorKind::CalamineCellError(err.to_owned()).into()),
},
CalData::Empty => Ok(DType::Null),
Expand Down

0 comments on commit 55577dd

Please sign in to comment.