|
| 1 | +import datetime |
| 2 | + |
| 3 | +import fastexcel |
| 4 | +import polars as pl |
| 5 | +from pandas.testing import assert_frame_equal as pd_assert_frame_equal |
| 6 | +from polars.testing import assert_frame_equal as pl_assert_frame_equal |
| 7 | + |
| 8 | +from .utils import path_for_fixture |
| 9 | + |
| 10 | + |
| 11 | +def test_skip_tail_whitespace_rows() -> None: |
| 12 | + """Test that skip_whitespace_tail_rows option works correctly.""" |
| 13 | + excel_reader = fastexcel.read_excel(path_for_fixture("sheet-and-table-with-whitespace.xlsx")) |
| 14 | + |
| 15 | + # Expected data when NOT skipping whitespace tail rows |
| 16 | + expected_with_whitespace = pl.DataFrame( |
| 17 | + { |
| 18 | + "Column One": ["1", "2", "3", None, "5", None, None, None, None, " "], |
| 19 | + "Column Two": ["one", "two", None, "four", "five", None, None, "", None, None], |
| 20 | + "Column Three": [ |
| 21 | + datetime.datetime(2025, 11, 19, 14, 34, 2), |
| 22 | + datetime.datetime(2025, 11, 20, 14, 56, 34), |
| 23 | + datetime.datetime(2025, 11, 21, 15, 19, 6), |
| 24 | + None, |
| 25 | + datetime.datetime(2025, 11, 22, 15, 41, 38), |
| 26 | + datetime.datetime(2025, 11, 23, 16, 4, 10), |
| 27 | + None, |
| 28 | + None, |
| 29 | + None, |
| 30 | + None, |
| 31 | + ], |
| 32 | + } |
| 33 | + ).with_columns(pl.col("Column Three").dt.cast_time_unit("ms")) |
| 34 | + |
| 35 | + # Expected data when skipping whitespace tail rows |
| 36 | + expected_without_whitespace = pl.DataFrame( |
| 37 | + { |
| 38 | + "Column One": [1.0, 2.0, 3.0, None, 5.0, None], |
| 39 | + "Column Two": ["one", "two", None, "four", "five", None], |
| 40 | + "Column Three": [ |
| 41 | + datetime.datetime(2025, 11, 19, 14, 34, 2), |
| 42 | + datetime.datetime(2025, 11, 20, 14, 56, 34), |
| 43 | + datetime.datetime(2025, 11, 21, 15, 19, 6), |
| 44 | + None, |
| 45 | + datetime.datetime(2025, 11, 22, 15, 41, 38), |
| 46 | + datetime.datetime(2025, 11, 23, 16, 4, 10), |
| 47 | + ], |
| 48 | + } |
| 49 | + ).with_columns(pl.col("Column Three").dt.cast_time_unit("ms")) |
| 50 | + |
| 51 | + # Test sheet without skipping whitespace tail rows |
| 52 | + sheet_with_whitespace = excel_reader.load_sheet("Without Table") |
| 53 | + pl_assert_frame_equal(sheet_with_whitespace.to_polars(), expected_with_whitespace) |
| 54 | + |
| 55 | + # Test table without skipping whitespace tail rows |
| 56 | + table_with_whitespace = excel_reader.load_table("Table_with_whitespace") |
| 57 | + pl_assert_frame_equal(table_with_whitespace.to_polars(), expected_with_whitespace) |
| 58 | + |
| 59 | + # Test sheet with skipping whitespace tail rows |
| 60 | + sheet_without_whitespace = excel_reader.load_sheet( |
| 61 | + "Without Table", skip_whitespace_tail_rows=True |
| 62 | + ) |
| 63 | + pl_assert_frame_equal(sheet_without_whitespace.to_polars(), expected_without_whitespace) |
| 64 | + |
| 65 | + # Test table with skipping whitespace tail rows |
| 66 | + table_without_whitespace = excel_reader.load_table( |
| 67 | + "Table_with_whitespace", skip_whitespace_tail_rows=True |
| 68 | + ) |
| 69 | + pl_assert_frame_equal(table_without_whitespace.to_polars(), expected_without_whitespace) |
| 70 | + |
| 71 | + # Also verify pandas compatibility |
| 72 | + pd_assert_frame_equal( |
| 73 | + sheet_without_whitespace.to_pandas(), expected_without_whitespace.to_pandas() |
| 74 | + ) |
| 75 | + pd_assert_frame_equal( |
| 76 | + table_without_whitespace.to_pandas(), expected_without_whitespace.to_pandas() |
| 77 | + ) |
0 commit comments