test_utils.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. from pandas.core.interchange.utils import dtype_to_arrow_c_fmt
  5. # TODO: use ArrowSchema to get reference C-string.
  6. # At the time, there is no way to access ArrowSchema holding a type format string
  7. # from python. The only way to access it is to export the structure to a C-pointer,
  8. # see DataType._export_to_c() method defined in
  9. # https://github.com/apache/arrow/blob/master/python/pyarrow/types.pxi
  10. @pytest.mark.parametrize(
  11. "pandas_dtype, c_string",
  12. [
  13. (np.dtype("bool"), "b"),
  14. (np.dtype("int8"), "c"),
  15. (np.dtype("uint8"), "C"),
  16. (np.dtype("int16"), "s"),
  17. (np.dtype("uint16"), "S"),
  18. (np.dtype("int32"), "i"),
  19. (np.dtype("uint32"), "I"),
  20. (np.dtype("int64"), "l"),
  21. (np.dtype("uint64"), "L"),
  22. (np.dtype("float16"), "e"),
  23. (np.dtype("float32"), "f"),
  24. (np.dtype("float64"), "g"),
  25. (pd.Series(["a"]).dtype, "u"),
  26. (
  27. pd.Series([0]).astype("datetime64[ns]").dtype,
  28. "tsn:",
  29. ),
  30. (pd.CategoricalDtype(["a"]), "l"),
  31. (np.dtype("O"), "u"),
  32. ],
  33. )
  34. def test_dtype_to_arrow_c_fmt(pandas_dtype, c_string): # PR01
  35. """Test ``dtype_to_arrow_c_fmt`` utility function."""
  36. assert dtype_to_arrow_c_fmt(pandas_dtype) == c_string