conftest.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import pytest
  4. from pandas.compat import long
  5. import pandas as pd
  6. import pandas.util.testing as tm
  7. # ------------------------------------------------------------------
  8. # Helper Functions
  9. def id_func(x):
  10. if isinstance(x, tuple):
  11. assert len(x) == 2
  12. return x[0].__name__ + '-' + str(x[1])
  13. else:
  14. return x.__name__
  15. # ------------------------------------------------------------------
  16. @pytest.fixture(params=[1, np.array(1, dtype=np.int64)])
  17. def one(request):
  18. # zero-dim integer array behaves like an integer
  19. return request.param
  20. zeros = [box_cls([0] * 5, dtype=dtype)
  21. for box_cls in [pd.Index, np.array]
  22. for dtype in [np.int64, np.uint64, np.float64]]
  23. zeros.extend([np.array(0, dtype=dtype)
  24. for dtype in [np.int64, np.uint64, np.float64]])
  25. zeros.extend([0, 0.0, long(0)])
  26. @pytest.fixture(params=zeros)
  27. def zero(request):
  28. # For testing division by (or of) zero for Index with length 5, this
  29. # gives several scalar-zeros and length-5 vector-zeros
  30. return request.param
  31. # ------------------------------------------------------------------
  32. # Vector Fixtures
  33. @pytest.fixture(params=[pd.Float64Index(np.arange(5, dtype='float64')),
  34. pd.Int64Index(np.arange(5, dtype='int64')),
  35. pd.UInt64Index(np.arange(5, dtype='uint64')),
  36. pd.RangeIndex(5)],
  37. ids=lambda x: type(x).__name__)
  38. def numeric_idx(request):
  39. """
  40. Several types of numeric-dtypes Index objects
  41. """
  42. return request.param
  43. # ------------------------------------------------------------------
  44. # Scalar Fixtures
  45. @pytest.fixture(params=[pd.Timedelta('5m4s').to_pytimedelta(),
  46. pd.Timedelta('5m4s'),
  47. pd.Timedelta('5m4s').to_timedelta64()],
  48. ids=lambda x: type(x).__name__)
  49. def scalar_td(request):
  50. """
  51. Several variants of Timedelta scalars representing 5 minutes and 4 seconds
  52. """
  53. return request.param
  54. @pytest.fixture(params=[pd.offsets.Day(3),
  55. pd.offsets.Hour(72),
  56. pd.Timedelta(days=3).to_pytimedelta(),
  57. pd.Timedelta('72:00:00'),
  58. np.timedelta64(3, 'D'),
  59. np.timedelta64(72, 'h')],
  60. ids=lambda x: type(x).__name__)
  61. def three_days(request):
  62. """
  63. Several timedelta-like and DateOffset objects that each represent
  64. a 3-day timedelta
  65. """
  66. return request.param
  67. @pytest.fixture(params=[pd.offsets.Hour(2),
  68. pd.offsets.Minute(120),
  69. pd.Timedelta(hours=2).to_pytimedelta(),
  70. pd.Timedelta(seconds=2 * 3600),
  71. np.timedelta64(2, 'h'),
  72. np.timedelta64(120, 'm')],
  73. ids=lambda x: type(x).__name__)
  74. def two_hours(request):
  75. """
  76. Several timedelta-like and DateOffset objects that each represent
  77. a 2-hour timedelta
  78. """
  79. return request.param
  80. _common_mismatch = [pd.offsets.YearBegin(2),
  81. pd.offsets.MonthBegin(1),
  82. pd.offsets.Minute()]
  83. @pytest.fixture(params=[pd.Timedelta(minutes=30).to_pytimedelta(),
  84. np.timedelta64(30, 's'),
  85. pd.Timedelta(seconds=30)] + _common_mismatch)
  86. def not_hourly(request):
  87. """
  88. Several timedelta-like and DateOffset instances that are _not_
  89. compatible with Hourly frequencies.
  90. """
  91. return request.param
  92. @pytest.fixture(params=[np.timedelta64(4, 'h'),
  93. pd.Timedelta(hours=23).to_pytimedelta(),
  94. pd.Timedelta('23:00:00')] + _common_mismatch)
  95. def not_daily(request):
  96. """
  97. Several timedelta-like and DateOffset instances that are _not_
  98. compatible with Daily frequencies.
  99. """
  100. return request.param
  101. @pytest.fixture(params=[np.timedelta64(365, 'D'),
  102. pd.Timedelta(days=365).to_pytimedelta(),
  103. pd.Timedelta(days=365)] + _common_mismatch)
  104. def mismatched_freq(request):
  105. """
  106. Several timedelta-like and DateOffset instances that are _not_
  107. compatible with Monthly or Annual frequencies.
  108. """
  109. return request.param
  110. # ------------------------------------------------------------------
  111. @pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame],
  112. ids=id_func)
  113. def box(request):
  114. """
  115. Several array-like containers that should have effectively identical
  116. behavior with respect to arithmetic operations.
  117. """
  118. return request.param
  119. @pytest.fixture(params=[pd.Index,
  120. pd.Series,
  121. pytest.param(pd.DataFrame,
  122. marks=pytest.mark.xfail)],
  123. ids=id_func)
  124. def box_df_fail(request):
  125. """
  126. Fixture equivalent to `box` fixture but xfailing the DataFrame case.
  127. """
  128. return request.param
  129. @pytest.fixture(params=[(pd.Index, False),
  130. (pd.Series, False),
  131. (pd.DataFrame, False),
  132. pytest.param((pd.DataFrame, True),
  133. marks=pytest.mark.xfail)],
  134. ids=id_func)
  135. def box_transpose_fail(request):
  136. """
  137. Fixture similar to `box` but testing both transpose cases for DataFrame,
  138. with the tranpose=True case xfailed.
  139. """
  140. # GH#23620
  141. return request.param
  142. @pytest.fixture(params=[pd.Index, pd.Series, pd.DataFrame, tm.to_array],
  143. ids=id_func)
  144. def box_with_array(request):
  145. """
  146. Fixture to test behavior for Index, Series, DataFrame, and pandas Array
  147. classes
  148. """
  149. return request.param
  150. # alias so we can use the same fixture for multiple parameters in a test
  151. box_with_array2 = box_with_array