conftest.py 5.6 KB

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