conftest.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import numpy as np
  2. import pytest
  3. from pandas import (
  4. DataFrame,
  5. Index,
  6. Series,
  7. date_range,
  8. )
  9. from pandas.core.groupby.base import (
  10. reduction_kernels,
  11. transformation_kernels,
  12. )
  13. @pytest.fixture(params=[True, False])
  14. def sort(request):
  15. return request.param
  16. @pytest.fixture(params=[True, False])
  17. def as_index(request):
  18. return request.param
  19. @pytest.fixture(params=[True, False])
  20. def dropna(request):
  21. return request.param
  22. @pytest.fixture(params=[True, False])
  23. def observed(request):
  24. return request.param
  25. @pytest.fixture
  26. def df():
  27. return DataFrame(
  28. {
  29. "A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
  30. "B": ["one", "one", "two", "three", "two", "two", "one", "three"],
  31. "C": np.random.default_rng(2).standard_normal(8),
  32. "D": np.random.default_rng(2).standard_normal(8),
  33. }
  34. )
  35. @pytest.fixture
  36. def ts():
  37. return Series(
  38. np.random.default_rng(2).standard_normal(30),
  39. index=date_range("2000-01-01", periods=30, freq="B"),
  40. )
  41. @pytest.fixture
  42. def tsframe():
  43. return DataFrame(
  44. np.random.default_rng(2).standard_normal((30, 4)),
  45. columns=Index(list("ABCD"), dtype=object),
  46. index=date_range("2000-01-01", periods=30, freq="B"),
  47. )
  48. @pytest.fixture
  49. def three_group():
  50. return DataFrame(
  51. {
  52. "A": [
  53. "foo",
  54. "foo",
  55. "foo",
  56. "foo",
  57. "bar",
  58. "bar",
  59. "bar",
  60. "bar",
  61. "foo",
  62. "foo",
  63. "foo",
  64. ],
  65. "B": [
  66. "one",
  67. "one",
  68. "one",
  69. "two",
  70. "one",
  71. "one",
  72. "one",
  73. "two",
  74. "two",
  75. "two",
  76. "one",
  77. ],
  78. "C": [
  79. "dull",
  80. "dull",
  81. "shiny",
  82. "dull",
  83. "dull",
  84. "shiny",
  85. "shiny",
  86. "dull",
  87. "shiny",
  88. "shiny",
  89. "shiny",
  90. ],
  91. "D": np.random.default_rng(2).standard_normal(11),
  92. "E": np.random.default_rng(2).standard_normal(11),
  93. "F": np.random.default_rng(2).standard_normal(11),
  94. }
  95. )
  96. @pytest.fixture()
  97. def slice_test_df():
  98. data = [
  99. [0, "a", "a0_at_0"],
  100. [1, "b", "b0_at_1"],
  101. [2, "a", "a1_at_2"],
  102. [3, "b", "b1_at_3"],
  103. [4, "c", "c0_at_4"],
  104. [5, "a", "a2_at_5"],
  105. [6, "a", "a3_at_6"],
  106. [7, "a", "a4_at_7"],
  107. ]
  108. df = DataFrame(data, columns=["Index", "Group", "Value"])
  109. return df.set_index("Index")
  110. @pytest.fixture()
  111. def slice_test_grouped(slice_test_df):
  112. return slice_test_df.groupby("Group", as_index=False)
  113. @pytest.fixture(params=sorted(reduction_kernels))
  114. def reduction_func(request):
  115. """
  116. yields the string names of all groupby reduction functions, one at a time.
  117. """
  118. return request.param
  119. @pytest.fixture(params=sorted(transformation_kernels))
  120. def transformation_func(request):
  121. """yields the string names of all groupby transformation functions."""
  122. return request.param
  123. @pytest.fixture(params=sorted(reduction_kernels) + sorted(transformation_kernels))
  124. def groupby_func(request):
  125. """yields both aggregation and transformation functions."""
  126. return request.param
  127. @pytest.fixture(params=[True, False])
  128. def parallel(request):
  129. """parallel keyword argument for numba.jit"""
  130. return request.param
  131. # Can parameterize nogil & nopython over True | False, but limiting per
  132. # https://github.com/pandas-dev/pandas/pull/41971#issuecomment-860607472
  133. @pytest.fixture(params=[False])
  134. def nogil(request):
  135. """nogil keyword argument for numba.jit"""
  136. return request.param
  137. @pytest.fixture(params=[True])
  138. def nopython(request):
  139. """nopython keyword argument for numba.jit"""
  140. return request.param
  141. @pytest.fixture(
  142. params=[
  143. ("mean", {}),
  144. ("var", {"ddof": 1}),
  145. ("var", {"ddof": 0}),
  146. ("std", {"ddof": 1}),
  147. ("std", {"ddof": 0}),
  148. ("sum", {}),
  149. ("min", {}),
  150. ("max", {}),
  151. ("sum", {"min_count": 2}),
  152. ("min", {"min_count": 2}),
  153. ("max", {"min_count": 2}),
  154. ],
  155. ids=[
  156. "mean",
  157. "var_1",
  158. "var_0",
  159. "std_1",
  160. "std_0",
  161. "sum",
  162. "min",
  163. "max",
  164. "sum-min_count",
  165. "min-min_count",
  166. "max-min_count",
  167. ],
  168. )
  169. def numba_supported_reductions(request):
  170. """reductions supported with engine='numba'"""
  171. return request.param