test_odf.py 2.0 KB

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