conftest.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. from pandas import (
  5. Float64Index,
  6. Int64Index,
  7. RangeIndex,
  8. UInt64Index,
  9. )
  10. import pandas._testing as tm
  11. from pandas.core.computation import expressions as expr
  12. @pytest.fixture(
  13. autouse=True, scope="module", params=[0, 1000000], ids=["numexpr", "python"]
  14. )
  15. def switch_numexpr_min_elements(request):
  16. _MIN_ELEMENTS = expr._MIN_ELEMENTS
  17. expr._MIN_ELEMENTS = request.param
  18. yield request.param
  19. expr._MIN_ELEMENTS = _MIN_ELEMENTS
  20. # ------------------------------------------------------------------
  21. # Helper Functions
  22. def id_func(x):
  23. if isinstance(x, tuple):
  24. assert len(x) == 2
  25. return x[0].__name__ + "-" + str(x[1])
  26. else:
  27. return x.__name__
  28. # ------------------------------------------------------------------
  29. @pytest.fixture(params=[1, np.array(1, dtype=np.int64)])
  30. def one(request):
  31. """
  32. Several variants of integer value 1. The zero-dim integer array
  33. behaves like an integer.
  34. This fixture can be used to check that datetimelike indexes handle
  35. addition and subtraction of integers and zero-dimensional arrays
  36. of integers.
  37. Examples
  38. --------
  39. >>> dti = pd.date_range('2016-01-01', periods=2, freq='H')
  40. >>> dti
  41. DatetimeIndex(['2016-01-01 00:00:00', '2016-01-01 01:00:00'],
  42. dtype='datetime64[ns]', freq='H')
  43. >>> dti + one
  44. DatetimeIndex(['2016-01-01 01:00:00', '2016-01-01 02:00:00'],
  45. dtype='datetime64[ns]', freq='H')
  46. """
  47. return request.param
  48. zeros = [
  49. box_cls([0] * 5, dtype=dtype)
  50. for box_cls in [pd.Index, np.array, pd.array]
  51. for dtype in [np.int64, np.uint64, np.float64]
  52. ]
  53. zeros.extend(
  54. [box_cls([-0.0] * 5, dtype=np.float64) for box_cls in [pd.Index, np.array]]
  55. )
  56. zeros.extend([np.array(0, dtype=dtype) for dtype in [np.int64, np.uint64, np.float64]])
  57. zeros.extend([np.array(-0.0, dtype=np.float64)])
  58. zeros.extend([0, 0.0, -0.0])
  59. @pytest.fixture(params=zeros)
  60. def zero(request):
  61. """
  62. Several types of scalar zeros and length 5 vectors of zeros.
  63. This fixture can be used to check that numeric-dtype indexes handle
  64. division by any zero numeric-dtype.
  65. Uses vector of length 5 for broadcasting with `numeric_idx` fixture,
  66. which creates numeric-dtype vectors also of length 5.
  67. Examples
  68. --------
  69. >>> arr = RangeIndex(5)
  70. >>> arr / zeros
  71. Float64Index([nan, inf, inf, inf, inf], dtype='float64')
  72. """
  73. return request.param
  74. # ------------------------------------------------------------------
  75. # Vector Fixtures
  76. @pytest.fixture(
  77. params=[
  78. Float64Index(np.arange(5, dtype="float64")),
  79. Int64Index(np.arange(5, dtype="int64")),
  80. UInt64Index(np.arange(5, dtype="uint64")),
  81. RangeIndex(5),
  82. ],
  83. ids=lambda x: type(x).__name__,
  84. )
  85. def numeric_idx(request):
  86. """
  87. Several types of numeric-dtypes Index objects
  88. """
  89. return request.param
  90. # ------------------------------------------------------------------
  91. # Scalar Fixtures
  92. @pytest.fixture(
  93. params=[
  94. pd.Timedelta("5m4s").to_pytimedelta(),
  95. pd.Timedelta("5m4s"),
  96. pd.Timedelta("5m4s").to_timedelta64(),
  97. ],
  98. ids=lambda x: type(x).__name__,
  99. )
  100. def scalar_td(request):
  101. """
  102. Several variants of Timedelta scalars representing 5 minutes and 4 seconds
  103. """
  104. return request.param
  105. @pytest.fixture(
  106. params=[
  107. pd.offsets.Day(3),
  108. pd.offsets.Hour(72),
  109. pd.Timedelta(days=3).to_pytimedelta(),
  110. pd.Timedelta("72:00:00"),
  111. np.timedelta64(3, "D"),
  112. np.timedelta64(72, "h"),
  113. ],
  114. ids=lambda x: type(x).__name__,
  115. )
  116. def three_days(request):
  117. """
  118. Several timedelta-like and DateOffset objects that each represent
  119. a 3-day timedelta
  120. """
  121. return request.param
  122. @pytest.fixture(
  123. params=[
  124. pd.offsets.Hour(2),
  125. pd.offsets.Minute(120),
  126. pd.Timedelta(hours=2).to_pytimedelta(),
  127. pd.Timedelta(seconds=2 * 3600),
  128. np.timedelta64(2, "h"),
  129. np.timedelta64(120, "m"),
  130. ],
  131. ids=lambda x: type(x).__name__,
  132. )
  133. def two_hours(request):
  134. """
  135. Several timedelta-like and DateOffset objects that each represent
  136. a 2-hour timedelta
  137. """
  138. return request.param
  139. _common_mismatch = [
  140. pd.offsets.YearBegin(2),
  141. pd.offsets.MonthBegin(1),
  142. pd.offsets.Minute(),
  143. ]
  144. @pytest.fixture(
  145. params=[
  146. pd.Timedelta(minutes=30).to_pytimedelta(),
  147. np.timedelta64(30, "s"),
  148. pd.Timedelta(seconds=30),
  149. ]
  150. + _common_mismatch
  151. )
  152. def not_hourly(request):
  153. """
  154. Several timedelta-like and DateOffset instances that are _not_
  155. compatible with Hourly frequencies.
  156. """
  157. return request.param
  158. @pytest.fixture(
  159. params=[
  160. np.timedelta64(4, "h"),
  161. pd.Timedelta(hours=23).to_pytimedelta(),
  162. pd.Timedelta("23:00:00"),
  163. ]
  164. + _common_mismatch
  165. )
  166. def not_daily(request):
  167. """
  168. Several timedelta-like and DateOffset instances that are _not_
  169. compatible with Daily frequencies.
  170. """
  171. return request.param
  172. @pytest.fixture(
  173. params=[
  174. np.timedelta64(365, "D"),
  175. pd.Timedelta(days=365).to_pytimedelta(),
  176. pd.Timedelta(days=365),
  177. ]
  178. + _common_mismatch
  179. )
  180. def mismatched_freq(request):
  181. """
  182. Several timedelta-like and DateOffset instances that are _not_
  183. compatible with Monthly or Annual frequencies.
  184. """
  185. return request.param
  186. # ------------------------------------------------------------------
  187. @pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame, pd.array], ids=id_func)
  188. def box_with_array(request):
  189. """
  190. Fixture to test behavior for Index, Series, DataFrame, and pandas Array
  191. classes
  192. """
  193. return request.param
  194. @pytest.fixture(params=[pd.Index, pd.Series, tm.to_array, np.array, list], ids=id_func)
  195. def box_1d_array(request):
  196. """
  197. Fixture to test behavior for Index, Series, tm.to_array, numpy Array and list
  198. classes
  199. """
  200. return request.param
  201. # alias so we can use the same fixture for multiple parameters in a test
  202. box_with_array2 = box_with_array