test_odf.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import functools
  2. import numpy as np
  3. import pytest
  4. import pandas as pd
  5. import pandas._testing as tm
  6. pytest.importorskip("odf")
  7. @pytest.fixture(autouse=True)
  8. def cd_and_set_engine(monkeypatch, datapath):
  9. func = functools.partial(pd.read_excel, engine="odf")
  10. monkeypatch.setattr(pd, "read_excel", func)
  11. monkeypatch.chdir(datapath("io", "data", "excel"))
  12. def test_read_invalid_types_raises():
  13. # the invalid_value_type.ods required manually editing
  14. # of the included content.xml file
  15. with pytest.raises(ValueError, match="Unrecognized type awesome_new_type"):
  16. pd.read_excel("invalid_value_type.ods")
  17. def test_read_writer_table():
  18. # Also test reading tables from an text OpenDocument file
  19. # (.odt)
  20. index = pd.Index(["Row 1", "Row 2", "Row 3"], name="Header")
  21. expected = pd.DataFrame(
  22. [[1, np.nan, 7], [2, np.nan, 8], [3, np.nan, 9]],
  23. index=index,
  24. columns=["Column 1", "Unnamed: 2", "Column 3"],
  25. )
  26. result = pd.read_excel("writertable.odt", sheet_name="Table1", index_col=0)
  27. tm.assert_frame_equal(result, expected)
  28. def test_read_newlines_between_xml_elements_table():
  29. # GH#45598
  30. expected = pd.DataFrame(
  31. [[1.0, 4.0, 7], [np.nan, np.nan, 8], [3.0, 6.0, 9]],
  32. columns=["Column 1", "Column 2", "Column 3"],
  33. )
  34. result = pd.read_excel("test_newlines.ods")
  35. tm.assert_frame_equal(result, expected)