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