conftest.py 4.6 KB

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