test_integer.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. """
  2. This file contains a minimal set of tests for compliance with the extension
  3. array interface test suite, and should contain no other tests.
  4. The test suite for the full functionality of the array is located in
  5. `pandas/tests/arrays/`.
  6. The tests in this file are inherited from the BaseExtensionTests, and only
  7. minimal tweaks should be applied to get the tests passing (by overwriting a
  8. parent method).
  9. Additional tests should either be added to one of the BaseExtensionTests
  10. classes (if they are relevant for the extension interface for all dtypes), or
  11. be added to the array-specific tests in `pandas/tests/arrays/`.
  12. """
  13. import numpy as np
  14. import pytest
  15. from pandas.compat import (
  16. IS64,
  17. is_platform_windows,
  18. )
  19. import pandas as pd
  20. import pandas._testing as tm
  21. from pandas.api.types import (
  22. is_extension_array_dtype,
  23. is_integer_dtype,
  24. )
  25. from pandas.core.arrays.integer import (
  26. Int8Dtype,
  27. Int16Dtype,
  28. Int32Dtype,
  29. Int64Dtype,
  30. UInt8Dtype,
  31. UInt16Dtype,
  32. UInt32Dtype,
  33. UInt64Dtype,
  34. )
  35. from pandas.tests.extension import base
  36. def make_data():
  37. return list(range(1, 9)) + [pd.NA] + list(range(10, 98)) + [pd.NA] + [99, 100]
  38. @pytest.fixture(
  39. params=[
  40. Int8Dtype,
  41. Int16Dtype,
  42. Int32Dtype,
  43. Int64Dtype,
  44. UInt8Dtype,
  45. UInt16Dtype,
  46. UInt32Dtype,
  47. UInt64Dtype,
  48. ]
  49. )
  50. def dtype(request):
  51. return request.param()
  52. @pytest.fixture
  53. def data(dtype):
  54. return pd.array(make_data(), dtype=dtype)
  55. @pytest.fixture
  56. def data_for_twos(dtype):
  57. return pd.array(np.ones(100) * 2, dtype=dtype)
  58. @pytest.fixture
  59. def data_missing(dtype):
  60. return pd.array([pd.NA, 1], dtype=dtype)
  61. @pytest.fixture
  62. def data_for_sorting(dtype):
  63. return pd.array([1, 2, 0], dtype=dtype)
  64. @pytest.fixture
  65. def data_missing_for_sorting(dtype):
  66. return pd.array([1, pd.NA, 0], dtype=dtype)
  67. @pytest.fixture
  68. def na_cmp():
  69. # we are pd.NA
  70. return lambda x, y: x is pd.NA and y is pd.NA
  71. @pytest.fixture
  72. def na_value():
  73. return pd.NA
  74. @pytest.fixture
  75. def data_for_grouping(dtype):
  76. b = 1
  77. a = 0
  78. c = 2
  79. na = pd.NA
  80. return pd.array([b, b, na, na, a, a, b, c], dtype=dtype)
  81. class TestDtype(base.BaseDtypeTests):
  82. pass
  83. class TestArithmeticOps(base.BaseArithmeticOpsTests):
  84. def check_opname(self, s, op_name, other, exc=None):
  85. # overwriting to indicate ops don't raise an error
  86. super().check_opname(s, op_name, other, exc=None)
  87. def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
  88. if exc is None:
  89. sdtype = tm.get_dtype(s)
  90. if (
  91. hasattr(other, "dtype")
  92. and not is_extension_array_dtype(other.dtype)
  93. and is_integer_dtype(other.dtype)
  94. and sdtype.is_unsigned_integer
  95. ):
  96. # TODO: comment below is inaccurate; other can be int8, int16, ...
  97. # and the trouble is that e.g. if s is UInt8 and other is int8,
  98. # then result is UInt16
  99. # other is np.int64 and would therefore always result in
  100. # upcasting, so keeping other as same numpy_dtype
  101. other = other.astype(sdtype.numpy_dtype)
  102. result = op(s, other)
  103. expected = self._combine(s, other, op)
  104. if op_name in ("__rtruediv__", "__truediv__", "__div__"):
  105. expected = expected.fillna(np.nan).astype("Float64")
  106. else:
  107. # combine method result in 'biggest' (int64) dtype
  108. expected = expected.astype(sdtype)
  109. self.assert_equal(result, expected)
  110. else:
  111. with pytest.raises(exc):
  112. op(s, other)
  113. def _check_divmod_op(self, s, op, other, exc=None):
  114. super()._check_divmod_op(s, op, other, None)
  115. class TestComparisonOps(base.BaseComparisonOpsTests):
  116. def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
  117. if exc is None:
  118. result = op(s, other)
  119. # Override to do the astype to boolean
  120. expected = s.combine(other, op).astype("boolean")
  121. self.assert_series_equal(result, expected)
  122. else:
  123. with pytest.raises(exc):
  124. op(s, other)
  125. def check_opname(self, s, op_name, other, exc=None):
  126. super().check_opname(s, op_name, other, exc=None)
  127. def _compare_other(self, s, data, op, other):
  128. op_name = f"__{op.__name__}__"
  129. self.check_opname(s, op_name, other)
  130. class TestInterface(base.BaseInterfaceTests):
  131. pass
  132. class TestConstructors(base.BaseConstructorsTests):
  133. pass
  134. class TestReshaping(base.BaseReshapingTests):
  135. pass
  136. # for test_concat_mixed_dtypes test
  137. # concat of an Integer and Int coerces to object dtype
  138. # TODO(jreback) once integrated this would
  139. class TestGetitem(base.BaseGetitemTests):
  140. pass
  141. class TestSetitem(base.BaseSetitemTests):
  142. pass
  143. class TestIndex(base.BaseIndexTests):
  144. pass
  145. class TestMissing(base.BaseMissingTests):
  146. pass
  147. class TestMethods(base.BaseMethodsTests):
  148. _combine_le_expected_dtype = object # TODO: can we make this boolean?
  149. class TestCasting(base.BaseCastingTests):
  150. pass
  151. class TestGroupby(base.BaseGroupbyTests):
  152. pass
  153. class TestNumericReduce(base.BaseNumericReduceTests):
  154. def check_reduce(self, s, op_name, skipna):
  155. # overwrite to ensure pd.NA is tested instead of np.nan
  156. # https://github.com/pandas-dev/pandas/issues/30958
  157. if op_name == "count":
  158. result = getattr(s, op_name)()
  159. expected = getattr(s.dropna().astype("int64"), op_name)()
  160. else:
  161. result = getattr(s, op_name)(skipna=skipna)
  162. expected = getattr(s.dropna().astype("int64"), op_name)(skipna=skipna)
  163. if not skipna and s.isna().any():
  164. expected = pd.NA
  165. tm.assert_almost_equal(result, expected)
  166. @pytest.mark.skip(reason="Tested in tests/reductions/test_reductions.py")
  167. class TestBooleanReduce(base.BaseBooleanReduceTests):
  168. pass
  169. class TestAccumulation(base.BaseAccumulateTests):
  170. def check_accumulate(self, s, op_name, skipna):
  171. # overwrite to ensure pd.NA is tested instead of np.nan
  172. # https://github.com/pandas-dev/pandas/issues/30958
  173. length = 64
  174. if not IS64 or is_platform_windows():
  175. if not s.dtype.itemsize == 8:
  176. length = 32
  177. if s.dtype.name.startswith("U"):
  178. expected_dtype = f"UInt{length}"
  179. else:
  180. expected_dtype = f"Int{length}"
  181. if op_name == "cumsum":
  182. result = getattr(s, op_name)(skipna=skipna)
  183. expected = pd.Series(
  184. pd.array(
  185. getattr(s.astype("float64"), op_name)(skipna=skipna),
  186. dtype=expected_dtype,
  187. )
  188. )
  189. tm.assert_series_equal(result, expected)
  190. elif op_name in ["cummax", "cummin"]:
  191. result = getattr(s, op_name)(skipna=skipna)
  192. expected = pd.Series(
  193. pd.array(
  194. getattr(s.astype("float64"), op_name)(skipna=skipna),
  195. dtype=s.dtype,
  196. )
  197. )
  198. tm.assert_series_equal(result, expected)
  199. elif op_name == "cumprod":
  200. result = getattr(s[:12], op_name)(skipna=skipna)
  201. expected = pd.Series(
  202. pd.array(
  203. getattr(s[:12].astype("float64"), op_name)(skipna=skipna),
  204. dtype=expected_dtype,
  205. )
  206. )
  207. tm.assert_series_equal(result, expected)
  208. else:
  209. raise NotImplementedError(f"{op_name} not supported")
  210. @pytest.mark.parametrize("skipna", [True, False])
  211. def test_accumulate_series_raises(self, data, all_numeric_accumulations, skipna):
  212. pass
  213. class TestPrinting(base.BasePrintingTests):
  214. pass
  215. class TestParsing(base.BaseParsingTests):
  216. pass
  217. class Test2DCompat(base.Dim2CompatTests):
  218. pass