test_pandas.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import pandas as pd
  2. def test_pandas_csv(tmp_path, monkeypatch):
  3. monkeypatch.chdir(tmp_path)
  4. data = {"col1": [1, 2.2, -3.3, 4.0, 5], "col2": ["A", "B", "C", "D", "E"]}
  5. df1 = pd.DataFrame(data)
  6. # write to csv file
  7. csv_file_path = tmp_path.joinpath("example.csv")
  8. df1.to_csv(csv_file_path, index=False)
  9. # read from csv file
  10. df2 = pd.read_csv(csv_file_path, on_bad_lines="skip")
  11. assert df2[df2.columns[0]].to_list() == data["col1"]
  12. assert df2[df2.columns[1]].to_list() == data["col2"]
  13. def test_pandas_xlsx(tmp_path, monkeypatch):
  14. monkeypatch.chdir(tmp_path)
  15. data = {"col1": [1, 2.2, -3.3, 4.0, 5], "col2": ["A", "B", "C", "D", "E"]}
  16. df1 = pd.DataFrame(data)
  17. # write to xlsx file
  18. xlsx_file_path = tmp_path.joinpath("example.xlsx")
  19. df1.to_excel(xlsx_file_path, index=False)
  20. # read from xlsx file
  21. df2 = pd.read_excel(xlsx_file_path)
  22. assert df2[df2.columns[0]].to_list() == data["col1"]
  23. assert df2[df2.columns[1]].to_list() == data["col2"]
  24. def test_pandas_xlsx_with_sheets(tmp_path, monkeypatch):
  25. monkeypatch.chdir(tmp_path)
  26. data1 = {"col1": [1, 2, 3, 4, 5], "col2": ["A", "B", "C", "D", "E"]}
  27. df1 = pd.DataFrame(data1)
  28. data2 = {"col1": [6, 7, 8, 9, 10], "col2": ["F", "G", "H", "I", "J"]}
  29. df2 = pd.DataFrame(data2)
  30. # write to xlsx file with sheets
  31. xlsx_file_path = tmp_path.joinpath("example_with_sheets.xlsx")
  32. sheet1 = "Sheet1"
  33. sheet2 = "Sheet2"
  34. with pd.ExcelWriter(xlsx_file_path) as excel_writer:
  35. df1.to_excel(excel_writer, sheet_name=sheet1, index=False)
  36. df2.to_excel(excel_writer, sheet_name=sheet2, index=False)
  37. # read from xlsx file with sheets
  38. with pd.ExcelFile(xlsx_file_path) as excel_file:
  39. df1 = pd.read_excel(excel_file, sheet_name=sheet1)
  40. assert df1[df1.columns[0]].to_list() == data1["col1"]
  41. assert df1[df1.columns[1]].to_list() == data1["col2"]
  42. df2 = pd.read_excel(excel_file, sheet_name=sheet2)
  43. assert df2[df2.columns[0]].to_list() == data2["col1"]
  44. assert df2[df2.columns[1]].to_list() == data2["col2"]