test_floating.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.core.dtypes.common import is_extension_array_dtype
  16. import pandas as pd
  17. import pandas._testing as tm
  18. from pandas.api.types import is_float_dtype
  19. from pandas.core.arrays.floating import (
  20. Float32Dtype,
  21. Float64Dtype,
  22. )
  23. from pandas.tests.extension import base
  24. def make_data():
  25. return (
  26. list(np.arange(0.1, 0.9, 0.1))
  27. + [pd.NA]
  28. + list(np.arange(1, 9.8, 0.1))
  29. + [pd.NA]
  30. + [9.9, 10.0]
  31. )
  32. @pytest.fixture(params=[Float32Dtype, Float64Dtype])
  33. def dtype(request):
  34. return request.param()
  35. @pytest.fixture
  36. def data(dtype):
  37. return pd.array(make_data(), dtype=dtype)
  38. @pytest.fixture
  39. def data_for_twos(dtype):
  40. return pd.array(np.ones(100) * 2, dtype=dtype)
  41. @pytest.fixture
  42. def data_missing(dtype):
  43. return pd.array([pd.NA, 0.1], dtype=dtype)
  44. @pytest.fixture
  45. def data_for_sorting(dtype):
  46. return pd.array([0.1, 0.2, 0.0], dtype=dtype)
  47. @pytest.fixture
  48. def data_missing_for_sorting(dtype):
  49. return pd.array([0.1, pd.NA, 0.0], dtype=dtype)
  50. @pytest.fixture
  51. def na_cmp():
  52. # we are pd.NA
  53. return lambda x, y: x is pd.NA and y is pd.NA
  54. @pytest.fixture
  55. def na_value():
  56. return pd.NA
  57. @pytest.fixture
  58. def data_for_grouping(dtype):
  59. b = 0.1
  60. a = 0.0
  61. c = 0.2
  62. na = pd.NA
  63. return pd.array([b, b, na, na, a, a, b, c], dtype=dtype)
  64. class TestDtype(base.BaseDtypeTests):
  65. pass
  66. class TestArithmeticOps(base.BaseArithmeticOpsTests):
  67. def check_opname(self, s, op_name, other, exc=None):
  68. # overwriting to indicate ops don't raise an error
  69. super().check_opname(s, op_name, other, exc=None)
  70. def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
  71. if exc is None:
  72. sdtype = tm.get_dtype(s)
  73. if (
  74. hasattr(other, "dtype")
  75. and not is_extension_array_dtype(other.dtype)
  76. and is_float_dtype(other.dtype)
  77. ):
  78. # other is np.float64 and would therefore always result in
  79. # upcasting, so keeping other as same numpy_dtype
  80. other = other.astype(sdtype.numpy_dtype)
  81. result = op(s, other)
  82. expected = self._combine(s, other, op)
  83. # combine method result in 'biggest' (float64) dtype
  84. expected = expected.astype(sdtype)
  85. self.assert_equal(result, expected)
  86. else:
  87. with pytest.raises(exc):
  88. op(s, other)
  89. def _check_divmod_op(self, s, op, other, exc=None):
  90. super()._check_divmod_op(s, op, other, None)
  91. class TestComparisonOps(base.BaseComparisonOpsTests):
  92. # TODO: share with IntegerArray?
  93. def _check_op(self, s, op, other, op_name, exc=NotImplementedError):
  94. if exc is None:
  95. result = op(s, other)
  96. # Override to do the astype to boolean
  97. expected = s.combine(other, op).astype("boolean")
  98. self.assert_series_equal(result, expected)
  99. else:
  100. with pytest.raises(exc):
  101. op(s, other)
  102. def check_opname(self, s, op_name, other, exc=None):
  103. super().check_opname(s, op_name, other, exc=None)
  104. def _compare_other(self, s, data, op, other):
  105. op_name = f"__{op.__name__}__"
  106. self.check_opname(s, op_name, other)
  107. class TestInterface(base.BaseInterfaceTests):
  108. pass
  109. class TestConstructors(base.BaseConstructorsTests):
  110. pass
  111. class TestReshaping(base.BaseReshapingTests):
  112. pass
  113. class TestGetitem(base.BaseGetitemTests):
  114. pass
  115. class TestSetitem(base.BaseSetitemTests):
  116. pass
  117. class TestIndex(base.BaseIndexTests):
  118. pass
  119. class TestMissing(base.BaseMissingTests):
  120. pass
  121. class TestMethods(base.BaseMethodsTests):
  122. _combine_le_expected_dtype = object # TODO: can we make this boolean?
  123. class TestCasting(base.BaseCastingTests):
  124. pass
  125. class TestGroupby(base.BaseGroupbyTests):
  126. pass
  127. class TestNumericReduce(base.BaseNumericReduceTests):
  128. def check_reduce(self, s, op_name, skipna):
  129. # overwrite to ensure pd.NA is tested instead of np.nan
  130. # https://github.com/pandas-dev/pandas/issues/30958
  131. if op_name == "count":
  132. result = getattr(s, op_name)()
  133. expected = getattr(s.dropna().astype(s.dtype.numpy_dtype), op_name)()
  134. else:
  135. result = getattr(s, op_name)(skipna=skipna)
  136. expected = getattr(s.dropna().astype(s.dtype.numpy_dtype), op_name)(
  137. skipna=skipna
  138. )
  139. if not skipna and s.isna().any():
  140. expected = pd.NA
  141. tm.assert_almost_equal(result, expected)
  142. @pytest.mark.skip(reason="Tested in tests/reductions/test_reductions.py")
  143. class TestBooleanReduce(base.BaseBooleanReduceTests):
  144. pass
  145. class TestPrinting(base.BasePrintingTests):
  146. pass
  147. class TestParsing(base.BaseParsingTests):
  148. pass
  149. @pytest.mark.filterwarnings("ignore:overflow encountered in reduce:RuntimeWarning")
  150. class Test2DCompat(base.Dim2CompatTests):
  151. pass
  152. class TestAccumulation(base.BaseAccumulateTests):
  153. @pytest.mark.parametrize("skipna", [True, False])
  154. def test_accumulate_series_raises(self, data, all_numeric_accumulations, skipna):
  155. pass