test_pandas.py 2.1 KB

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