conftest.py 4.2 KB

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