test_repr.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. from pandas.core.arrays.integer import (
  5. Int8Dtype,
  6. Int16Dtype,
  7. Int32Dtype,
  8. Int64Dtype,
  9. UInt8Dtype,
  10. UInt16Dtype,
  11. UInt32Dtype,
  12. UInt64Dtype,
  13. )
  14. def test_dtypes(dtype):
  15. # smoke tests on auto dtype construction
  16. if dtype.is_signed_integer:
  17. assert np.dtype(dtype.type).kind == "i"
  18. else:
  19. assert np.dtype(dtype.type).kind == "u"
  20. assert dtype.name is not None
  21. @pytest.mark.parametrize(
  22. "dtype, expected",
  23. [
  24. (Int8Dtype(), "Int8Dtype()"),
  25. (Int16Dtype(), "Int16Dtype()"),
  26. (Int32Dtype(), "Int32Dtype()"),
  27. (Int64Dtype(), "Int64Dtype()"),
  28. (UInt8Dtype(), "UInt8Dtype()"),
  29. (UInt16Dtype(), "UInt16Dtype()"),
  30. (UInt32Dtype(), "UInt32Dtype()"),
  31. (UInt64Dtype(), "UInt64Dtype()"),
  32. ],
  33. )
  34. def test_repr_dtype(dtype, expected):
  35. assert repr(dtype) == expected
  36. def test_repr_array():
  37. result = repr(pd.array([1, None, 3]))
  38. expected = "<IntegerArray>\n[1, <NA>, 3]\nLength: 3, dtype: Int64"
  39. assert result == expected
  40. def test_repr_array_long():
  41. data = pd.array([1, 2, None] * 1000)
  42. expected = (
  43. "<IntegerArray>\n"
  44. "[ 1, 2, <NA>, 1, 2, <NA>, 1, 2, <NA>, 1,\n"
  45. " ...\n"
  46. " <NA>, 1, 2, <NA>, 1, 2, <NA>, 1, 2, <NA>]\n"
  47. "Length: 3000, dtype: Int64"
  48. )
  49. result = repr(data)
  50. assert result == expected
  51. def test_frame_repr(data_missing):
  52. df = pd.DataFrame({"A": data_missing})
  53. result = repr(df)
  54. expected = " A\n0 <NA>\n1 1"
  55. assert result == expected