io.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from io import StringIO
  2. import numpy as np
  3. import pytest
  4. import pandas as pd
  5. import pandas._testing as tm
  6. from pandas.core.arrays import ExtensionArray
  7. class BaseParsingTests:
  8. @pytest.mark.parametrize("engine", ["c", "python"])
  9. def test_EA_types(self, engine, data, request):
  10. if isinstance(data.dtype, pd.CategoricalDtype):
  11. # in parsers.pyx _convert_with_dtype there is special-casing for
  12. # Categorical that pre-empts _from_sequence_of_strings
  13. pass
  14. elif isinstance(data.dtype, pd.core.dtypes.dtypes.NumpyEADtype):
  15. # These get unwrapped internally so are treated as numpy dtypes
  16. # in the parsers.pyx code
  17. pass
  18. elif (
  19. type(data)._from_sequence_of_strings.__func__
  20. is ExtensionArray._from_sequence_of_strings.__func__
  21. ):
  22. # i.e. the EA hasn't overridden _from_sequence_of_strings
  23. mark = pytest.mark.xfail(
  24. reason="_from_sequence_of_strings not implemented",
  25. raises=NotImplementedError,
  26. )
  27. request.node.add_marker(mark)
  28. df = pd.DataFrame({"with_dtype": pd.Series(data, dtype=str(data.dtype))})
  29. csv_output = df.to_csv(index=False, na_rep=np.nan)
  30. result = pd.read_csv(
  31. StringIO(csv_output), dtype={"with_dtype": str(data.dtype)}, engine=engine
  32. )
  33. expected = df
  34. tm.assert_frame_equal(result, expected)