Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: interpret #N/A cells as null #180

Merged
merged 2 commits into from
Feb 14, 2024
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
Binary file added python/tests/fixtures/sheet-with-na.xlsx
Binary file not shown.
17 changes: 17 additions & 0 deletions python/tests/test_fastexcel.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,20 @@ def test_sheet_with_pagination_out_of_bound():
pl.col("Amazing").str.strptime(pl.Datetime, "%F %T").dt.cast_time_unit("ms")
),
)


def test_sheet_with_na():
"""Test reading a sheet with #N/A cells. For now, we consider them as null"""
excel_reader = fastexcel.read_excel(path_for_fixture("sheet-with-na.xlsx"))
sheet = excel_reader.load_sheet(0)

assert sheet.name == "Sheet1"
assert sheet.height == sheet.total_height == 2
assert sheet.width == 2

expected = {
"Title": ["A", "B"],
"Amount": [None, 100.0],
}
pd_assert_frame_equal(sheet.to_pandas(), pd.DataFrame(expected))
pl_assert_frame_equal(sheet.to_polars(), pl.DataFrame(expected))
38 changes: 19 additions & 19 deletions src/types/excelsheet.rs
PrettyWood marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -246,29 +246,29 @@ fn create_duration_array(
impl TryFrom<&ExcelSheet> for Schema {
type Error = anyhow::Error;

fn try_from(value: &ExcelSheet) -> Result<Self, Self::Error> {
fn try_from(sheet: &ExcelSheet) -> Result<Self, Self::Error> {
// Checking how many rows we want to use to determine the dtype for a column. If sample_rows is
// not provided, we sample limit rows, i.e on the entire column
let sample_rows = value.offset() + value.schema_sample_rows().unwrap_or(value.limit());
let sample_rows = sheet.offset() + sheet.schema_sample_rows().unwrap_or(sheet.limit());

arrow_schema_from_column_names_and_range(
value.data(),
&value.column_names(),
value.offset(),
sheet.data(),
&sheet.column_names(),
sheet.offset(),
// If sample_rows is higher than the sheet's limit, use the limit instead
std::cmp::min(sample_rows, value.limit()),
std::cmp::min(sample_rows, sheet.limit()),
)
}
}

impl TryFrom<&ExcelSheet> for RecordBatch {
type Error = anyhow::Error;

fn try_from(value: &ExcelSheet) -> Result<Self, Self::Error> {
let offset = value.offset();
let limit = value.limit();
let schema = Schema::try_from(value)
.with_context(|| format!("Could not build schema for sheet {}", value.name))?;
fn try_from(sheet: &ExcelSheet) -> Result<Self, Self::Error> {
let offset = sheet.offset();
let limit = sheet.limit();
let schema = Schema::try_from(sheet)
.with_context(|| format!("Could not build schema for sheet {}", sheet.name))?;
let mut iter = schema
.fields()
.iter()
Expand All @@ -278,25 +278,25 @@ impl TryFrom<&ExcelSheet> for RecordBatch {
field.name(),
match field.data_type() {
ArrowDataType::Boolean => {
create_boolean_array(value.data(), col_idx, offset, limit)
create_boolean_array(sheet.data(), col_idx, offset, limit)
}
ArrowDataType::Int64 => {
create_int_array(value.data(), col_idx, offset, limit)
create_int_array(sheet.data(), col_idx, offset, limit)
}
ArrowDataType::Float64 => {
create_float_array(value.data(), col_idx, offset, limit)
create_float_array(sheet.data(), col_idx, offset, limit)
}
ArrowDataType::Utf8 => {
create_string_array(value.data(), col_idx, offset, limit)
create_string_array(sheet.data(), col_idx, offset, limit)
}
ArrowDataType::Timestamp(TimeUnit::Millisecond, None) => {
create_datetime_array(value.data(), col_idx, offset, limit)
create_datetime_array(sheet.data(), col_idx, offset, limit)
}
ArrowDataType::Date32 => {
create_date_array(value.data(), col_idx, offset, limit)
create_date_array(sheet.data(), col_idx, offset, limit)
}
ArrowDataType::Duration(TimeUnit::Millisecond) => {
create_duration_array(value.data(), col_idx, offset, limit)
create_duration_array(sheet.data(), col_idx, offset, limit)
}
ArrowDataType::Null => Arc::new(NullArray::new(limit - offset)),
_ => unreachable!(),
Expand All @@ -309,7 +309,7 @@ impl TryFrom<&ExcelSheet> for RecordBatch {
Ok(RecordBatch::new_empty(Arc::new(schema)))
} else {
RecordBatch::try_from_iter(iter)
.with_context(|| format!("Could not convert sheet {} to RecordBatch", value.name))
.with_context(|| format!("Could not convert sheet {} to RecordBatch", sheet.name))
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/utils/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashSet, sync::OnceLock};

use anyhow::{anyhow, Context, Result};
use arrow::datatypes::{DataType as ArrowDataType, Field, Schema, TimeUnit};
use calamine::{Data as CalData, DataType, Range};
use calamine::{CellErrorType, Data as CalData, DataType, Range};

fn get_cell_type(data: &Range<CalData>, row: usize, col: usize) -> Result<ArrowDataType> {
let cell = data
Expand Down Expand Up @@ -31,7 +31,10 @@ fn get_cell_type(data: &Range<CalData>, row: usize, col: usize) -> Result<ArrowD
// A simple duration
CalData::DurationIso(_) => Ok(ArrowDataType::Duration(TimeUnit::Millisecond)),
// Errors and nulls
CalData::Error(err) => Err(anyhow!("Error in calamine cell: {err:?}")),
CalData::Error(err) => match err {
CellErrorType::NA => Ok(ArrowDataType::Null),
_ => Err(anyhow!("Error in calamine cell: {err:?}")),
},
CalData::Empty => Ok(ArrowDataType::Null),
}
}
Expand Down
Loading