conftest.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import operator
  2. import pytest
  3. from pandas._config.config import _get_option
  4. from pandas import (
  5. Series,
  6. options,
  7. )
  8. @pytest.fixture
  9. def dtype():
  10. """A fixture providing the ExtensionDtype to validate."""
  11. raise NotImplementedError
  12. @pytest.fixture
  13. def data():
  14. """
  15. Length-100 array for this type.
  16. * data[0] and data[1] should both be non missing
  17. * data[0] and data[1] should not be equal
  18. """
  19. raise NotImplementedError
  20. @pytest.fixture
  21. def data_for_twos(dtype):
  22. """
  23. Length-100 array in which all the elements are two.
  24. Call pytest.skip in your fixture if the dtype does not support divmod.
  25. """
  26. if not (dtype._is_numeric or dtype.kind == "m"):
  27. # Object-dtypes may want to allow this, but for the most part
  28. # only numeric and timedelta-like dtypes will need to implement this.
  29. pytest.skip(f"{dtype} is not a numeric dtype")
  30. raise NotImplementedError
  31. @pytest.fixture
  32. def data_missing():
  33. """Length-2 array with [NA, Valid]"""
  34. raise NotImplementedError
  35. @pytest.fixture(params=["data", "data_missing"])
  36. def all_data(request, data, data_missing):
  37. """Parametrized fixture giving 'data' and 'data_missing'"""
  38. if request.param == "data":
  39. return data
  40. elif request.param == "data_missing":
  41. return data_missing
  42. @pytest.fixture
  43. def data_repeated(data):
  44. """
  45. Generate many datasets.
  46. Parameters
  47. ----------
  48. data : fixture implementing `data`
  49. Returns
  50. -------
  51. Callable[[int], Generator]:
  52. A callable that takes a `count` argument and
  53. returns a generator yielding `count` datasets.
  54. """
  55. def gen(count):
  56. for _ in range(count):
  57. yield data
  58. return gen
  59. @pytest.fixture
  60. def data_for_sorting():
  61. """
  62. Length-3 array with a known sort order.
  63. This should be three items [B, C, A] with
  64. A < B < C
  65. For boolean dtypes (for which there are only 2 values available),
  66. set B=C=True
  67. """
  68. raise NotImplementedError
  69. @pytest.fixture
  70. def data_missing_for_sorting():
  71. """
  72. Length-3 array with a known sort order.
  73. This should be three items [B, NA, A] with
  74. A < B and NA missing.
  75. """
  76. raise NotImplementedError
  77. @pytest.fixture
  78. def na_cmp():
  79. """
  80. Binary operator for comparing NA values.
  81. Should return a function of two arguments that returns
  82. True if both arguments are (scalar) NA for your type.
  83. By default, uses ``operator.is_``
  84. """
  85. return operator.is_
  86. @pytest.fixture
  87. def na_value(dtype):
  88. """
  89. The scalar missing value for this type. Default dtype.na_value.
  90. TODO: can be removed in 3.x (see https://github.com/pandas-dev/pandas/pull/54930)
  91. """
  92. return dtype.na_value
  93. @pytest.fixture
  94. def data_for_grouping():
  95. """
  96. Data for factorization, grouping, and unique tests.
  97. Expected to be like [B, B, NA, NA, A, A, B, C]
  98. Where A < B < C and NA is missing.
  99. If a dtype has _is_boolean = True, i.e. only 2 unique non-NA entries,
  100. then set C=B.
  101. """
  102. raise NotImplementedError
  103. @pytest.fixture(params=[True, False])
  104. def box_in_series(request):
  105. """Whether to box the data in a Series"""
  106. return request.param
  107. @pytest.fixture(
  108. params=[
  109. lambda x: 1,
  110. lambda x: [1] * len(x),
  111. lambda x: Series([1] * len(x)),
  112. lambda x: x,
  113. ],
  114. ids=["scalar", "list", "series", "object"],
  115. )
  116. def groupby_apply_op(request):
  117. """
  118. Functions to test groupby.apply().
  119. """
  120. return request.param
  121. @pytest.fixture(params=[True, False])
  122. def as_frame(request):
  123. """
  124. Boolean fixture to support Series and Series.to_frame() comparison testing.
  125. """
  126. return request.param
  127. @pytest.fixture(params=[True, False])
  128. def as_series(request):
  129. """
  130. Boolean fixture to support arr and Series(arr) comparison testing.
  131. """
  132. return request.param
  133. @pytest.fixture(params=[True, False])
  134. def use_numpy(request):
  135. """
  136. Boolean fixture to support comparison testing of ExtensionDtype array
  137. and numpy array.
  138. """
  139. return request.param
  140. @pytest.fixture(params=["ffill", "bfill"])
  141. def fillna_method(request):
  142. """
  143. Parametrized fixture giving method parameters 'ffill' and 'bfill' for
  144. Series.fillna(method=<method>) testing.
  145. """
  146. return request.param
  147. @pytest.fixture(params=[True, False])
  148. def as_array(request):
  149. """
  150. Boolean fixture to support ExtensionDtype _from_sequence method testing.
  151. """
  152. return request.param
  153. @pytest.fixture
  154. def invalid_scalar(data):
  155. """
  156. A scalar that *cannot* be held by this ExtensionArray.
  157. The default should work for most subclasses, but is not guaranteed.
  158. If the array can hold any item (i.e. object dtype), then use pytest.skip.
  159. """
  160. return object.__new__(object)
  161. @pytest.fixture
  162. def using_copy_on_write() -> bool:
  163. """
  164. Fixture to check if Copy-on-Write is enabled.
  165. """
  166. return (
  167. options.mode.copy_on_write is True
  168. and _get_option("mode.data_manager", silent=True) == "block"
  169. )