You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
7.1 KiB

4 years ago
  1. import os
  2. import pytest
  3. import pandas
  4. import numpy as np
  5. import pandas as pd
  6. from pandas.compat import PY3
  7. import pandas.util._test_decorators as td
  8. def pytest_addoption(parser):
  9. parser.addoption("--skip-slow", action="store_true",
  10. help="skip slow tests")
  11. parser.addoption("--skip-network", action="store_true",
  12. help="skip network tests")
  13. parser.addoption("--run-high-memory", action="store_true",
  14. help="run high memory tests")
  15. parser.addoption("--only-slow", action="store_true",
  16. help="run only slow tests")
  17. parser.addoption("--strict-data-files", action="store_true",
  18. help="Fail if a test is skipped for missing data file.")
  19. def pytest_runtest_setup(item):
  20. if 'slow' in item.keywords and item.config.getoption("--skip-slow"):
  21. pytest.skip("skipping due to --skip-slow")
  22. if 'slow' not in item.keywords and item.config.getoption("--only-slow"):
  23. pytest.skip("skipping due to --only-slow")
  24. if 'network' in item.keywords and item.config.getoption("--skip-network"):
  25. pytest.skip("skipping due to --skip-network")
  26. if 'high_memory' in item.keywords and not item.config.getoption(
  27. "--run-high-memory"):
  28. pytest.skip(
  29. "skipping high memory test since --run-high-memory was not set")
  30. # Configurations for all tests and all test modules
  31. @pytest.fixture(autouse=True)
  32. def configure_tests():
  33. pd.set_option('chained_assignment', 'raise')
  34. # For running doctests: make np and pd names available
  35. @pytest.fixture(autouse=True)
  36. def add_imports(doctest_namespace):
  37. doctest_namespace['np'] = np
  38. doctest_namespace['pd'] = pd
  39. @pytest.fixture(params=['bsr', 'coo', 'csc', 'csr', 'dia', 'dok', 'lil'])
  40. def spmatrix(request):
  41. from scipy import sparse
  42. return getattr(sparse, request.param + '_matrix')
  43. @pytest.fixture
  44. def ip():
  45. """
  46. Get an instance of IPython.InteractiveShell.
  47. Will raise a skip if IPython is not installed.
  48. """
  49. pytest.importorskip('IPython', minversion="6.0.0")
  50. from IPython.core.interactiveshell import InteractiveShell
  51. return InteractiveShell()
  52. @pytest.fixture(params=[True, False, None])
  53. def observed(request):
  54. """ pass in the observed keyword to groupby for [True, False]
  55. This indicates whether categoricals should return values for
  56. values which are not in the grouper [False / None], or only values which
  57. appear in the grouper [True]. [None] is supported for future compatiblity
  58. if we decide to change the default (and would need to warn if this
  59. parameter is not passed)"""
  60. return request.param
  61. _all_arithmetic_operators = ['__add__', '__radd__',
  62. '__sub__', '__rsub__',
  63. '__mul__', '__rmul__',
  64. '__floordiv__', '__rfloordiv__',
  65. '__truediv__', '__rtruediv__',
  66. '__pow__', '__rpow__']
  67. if not PY3:
  68. _all_arithmetic_operators.extend(['__div__', '__rdiv__'])
  69. @pytest.fixture(params=_all_arithmetic_operators)
  70. def all_arithmetic_operators(request):
  71. """
  72. Fixture for dunder names for common arithmetic operations
  73. """
  74. return request.param
  75. @pytest.fixture(params=[None, 'gzip', 'bz2', 'zip',
  76. pytest.param('xz', marks=td.skip_if_no_lzma)])
  77. def compression(request):
  78. """
  79. Fixture for trying common compression types in compression tests
  80. """
  81. return request.param
  82. @pytest.fixture(params=['gzip', 'bz2', 'zip',
  83. pytest.param('xz', marks=td.skip_if_no_lzma)])
  84. def compression_only(request):
  85. """
  86. Fixture for trying common compression types in compression tests excluding
  87. uncompressed case
  88. """
  89. return request.param
  90. @pytest.fixture(scope='module')
  91. def datetime_tz_utc():
  92. from datetime import timezone
  93. return timezone.utc
  94. @pytest.fixture(params=['inner', 'outer', 'left', 'right'])
  95. def join_type(request):
  96. """
  97. Fixture for trying all types of join operations
  98. """
  99. return request.param
  100. @pytest.fixture
  101. def datapath(request):
  102. """Get the path to a data file.
  103. Parameters
  104. ----------
  105. path : str
  106. Path to the file, relative to ``pandas/tests/``
  107. Returns
  108. -------
  109. path : path including ``pandas/tests``.
  110. Raises
  111. ------
  112. ValueError
  113. If the path doesn't exist and the --strict-data-files option is set.
  114. """
  115. def deco(*args):
  116. path = os.path.join('pandas', 'tests', *args)
  117. if not os.path.exists(path):
  118. if request.config.getoption("--strict-data-files"):
  119. msg = "Could not find file {} and --strict-data-files is set."
  120. raise ValueError(msg.format(path))
  121. else:
  122. msg = "Could not find {}."
  123. pytest.skip(msg.format(path))
  124. return path
  125. return deco
  126. @pytest.fixture
  127. def iris(datapath):
  128. """The iris dataset as a DataFrame."""
  129. return pandas.read_csv(datapath('data', 'iris.csv'))
  130. @pytest.fixture(params=['nlargest', 'nsmallest'])
  131. def nselect_method(request):
  132. """
  133. Fixture for trying all nselect methods
  134. """
  135. return request.param
  136. @pytest.fixture(params=[None, np.nan, pd.NaT, float('nan'), np.float('NaN')])
  137. def nulls_fixture(request):
  138. """
  139. Fixture for each null type in pandas
  140. """
  141. return request.param
  142. nulls_fixture2 = nulls_fixture # Generate cartesian product of nulls_fixture
  143. TIMEZONES = [None, 'UTC', 'US/Eastern', 'Asia/Tokyo', 'dateutil/US/Pacific']
  144. @td.parametrize_fixture_doc(str(TIMEZONES))
  145. @pytest.fixture(params=TIMEZONES)
  146. def tz_naive_fixture(request):
  147. """
  148. Fixture for trying timezones including default (None): {0}
  149. """
  150. return request.param
  151. @td.parametrize_fixture_doc(str(TIMEZONES[1:]))
  152. @pytest.fixture(params=TIMEZONES[1:])
  153. def tz_aware_fixture(request):
  154. """
  155. Fixture for trying explicit timezones: {0}
  156. """
  157. return request.param
  158. @pytest.fixture(params=[str, 'str', 'U'])
  159. def string_dtype(request):
  160. """Parametrized fixture for string dtypes.
  161. * str
  162. * 'str'
  163. * 'U'
  164. """
  165. return request.param
  166. @pytest.fixture(params=["float32", "float64"])
  167. def float_dtype(request):
  168. """
  169. Parameterized fixture for float dtypes.
  170. * float32
  171. * float64
  172. """
  173. return request.param
  174. UNSIGNED_INT_DTYPES = ["uint8", "uint16", "uint32", "uint64"]
  175. SIGNED_INT_DTYPES = ["int8", "int16", "int32", "int64"]
  176. ALL_INT_DTYPES = UNSIGNED_INT_DTYPES + SIGNED_INT_DTYPES
  177. @pytest.fixture(params=SIGNED_INT_DTYPES)
  178. def sint_dtype(request):
  179. """
  180. Parameterized fixture for signed integer dtypes.
  181. * int8
  182. * int16
  183. * int32
  184. * int64
  185. """
  186. return request.param
  187. @pytest.fixture(params=UNSIGNED_INT_DTYPES)
  188. def uint_dtype(request):
  189. """
  190. Parameterized fixture for unsigned integer dtypes.
  191. * uint8
  192. * uint16
  193. * uint32
  194. * uint64
  195. """
  196. return request.param
  197. @pytest.fixture(params=ALL_INT_DTYPES)
  198. def any_int_dtype(request):
  199. """
  200. Parameterized fixture for any integer dtypes.
  201. * int8
  202. * uint8
  203. * int16
  204. * uint16
  205. * int32
  206. * uint32
  207. * int64
  208. * uint64
  209. """
  210. return request.param