Skip to content

Commit

Permalink
feat: cast bools to strings if mixed types
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyWood committed Jul 17, 2024
1 parent 5c7c608 commit b2ee363
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
Binary file added python/tests/fixtures/sheet-bool.xlsx
Binary file not shown.
13 changes: 13 additions & 0 deletions python/tests/test_fastexcel.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,16 @@ def test_null_values_in_cells() -> None:
pd_expected = pd.DataFrame(expected)
pd_expected["Date"] = pd_expected["Date"].dt.as_unit("ms")
pd_assert_frame_equal(sheet.to_pandas(), pd_expected)


def test_bool_casting_to_string_for_polars() -> None:
excel_reader = fastexcel.read_excel(path_for_fixture("sheet-bool.xlsx"))

actual_polars_df = excel_reader.load_sheet(0, header_row=None, column_names=["0"]).to_polars()
expected_polars_df = pl.DataFrame(
{
"0": ["true", "false", "some string"],
}
)

pl_assert_frame_equal(actual_polars_df, expected_polars_df)
1 change: 1 addition & 0 deletions src/types/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ fn int_types() -> &'static HashSet<DType> {
fn string_types() -> &'static HashSet<DType> {
STRING_TYPES_CELL.get_or_init(|| {
HashSet::from([
DType::Bool,
DType::Int,
DType::Float,
DType::String,
Expand Down
2 changes: 2 additions & 0 deletions src/types/python/excelsheet/sheet_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ mod array_impls {
.map(|dt| dt.to_string())
} else if cell.is_datetime_iso() {
cell.get_datetime_iso().map(str::to_string)
} else if cell.is_bool() {
cell.get_bool().map(|v| v.to_string())
} else {
cell.as_string()
}
Expand Down

0 comments on commit b2ee363

Please sign in to comment.