conftest.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. from datetime import datetime
  2. import numpy as np
  3. import pytest
  4. from pandas import (
  5. DataFrame,
  6. Series,
  7. )
  8. from pandas.core.indexes.datetimes import date_range
  9. from pandas.core.indexes.period import period_range
  10. # The various methods we support
  11. downsample_methods = [
  12. "min",
  13. "max",
  14. "first",
  15. "last",
  16. "sum",
  17. "mean",
  18. "sem",
  19. "median",
  20. "prod",
  21. "var",
  22. "std",
  23. "ohlc",
  24. "quantile",
  25. ]
  26. upsample_methods = ["count", "size"]
  27. series_methods = ["nunique"]
  28. resample_methods = downsample_methods + upsample_methods + series_methods
  29. @pytest.fixture(params=downsample_methods)
  30. def downsample_method(request):
  31. """Fixture for parametrization of Grouper downsample methods."""
  32. return request.param
  33. @pytest.fixture(params=resample_methods)
  34. def resample_method(request):
  35. """Fixture for parametrization of Grouper resample methods."""
  36. return request.param
  37. @pytest.fixture
  38. def simple_date_range_series():
  39. """
  40. Series with date range index and random data for test purposes.
  41. """
  42. def _simple_date_range_series(start, end, freq="D"):
  43. rng = date_range(start, end, freq=freq)
  44. return Series(np.random.randn(len(rng)), index=rng)
  45. return _simple_date_range_series
  46. @pytest.fixture
  47. def simple_period_range_series():
  48. """
  49. Series with period range index and random data for test purposes.
  50. """
  51. def _simple_period_range_series(start, end, freq="D"):
  52. rng = period_range(start, end, freq=freq)
  53. return Series(np.random.randn(len(rng)), index=rng)
  54. return _simple_period_range_series
  55. @pytest.fixture
  56. def _index_start():
  57. """Fixture for parametrization of index, series and frame."""
  58. return datetime(2005, 1, 1)
  59. @pytest.fixture
  60. def _index_end():
  61. """Fixture for parametrization of index, series and frame."""
  62. return datetime(2005, 1, 10)
  63. @pytest.fixture
  64. def _index_freq():
  65. """Fixture for parametrization of index, series and frame."""
  66. return "D"
  67. @pytest.fixture
  68. def _index_name():
  69. """Fixture for parametrization of index, series and frame."""
  70. return None
  71. @pytest.fixture
  72. def index(_index_factory, _index_start, _index_end, _index_freq, _index_name):
  73. """
  74. Fixture for parametrization of date_range, period_range and
  75. timedelta_range indexes
  76. """
  77. return _index_factory(_index_start, _index_end, freq=_index_freq, name=_index_name)
  78. @pytest.fixture
  79. def _static_values(index):
  80. """
  81. Fixture for parametrization of values used in parametrization of
  82. Series and DataFrames with date_range, period_range and
  83. timedelta_range indexes
  84. """
  85. return np.arange(len(index))
  86. @pytest.fixture
  87. def _series_name():
  88. """
  89. Fixture for parametrization of Series name for Series used with
  90. date_range, period_range and timedelta_range indexes
  91. """
  92. return None
  93. @pytest.fixture
  94. def series(index, _series_name, _static_values):
  95. """
  96. Fixture for parametrization of Series with date_range, period_range and
  97. timedelta_range indexes
  98. """
  99. return Series(_static_values, index=index, name=_series_name)
  100. @pytest.fixture
  101. def empty_series_dti(series):
  102. """
  103. Fixture for parametrization of empty Series with date_range,
  104. period_range and timedelta_range indexes
  105. """
  106. return series[:0]
  107. @pytest.fixture
  108. def frame(index, _series_name, _static_values):
  109. """
  110. Fixture for parametrization of DataFrame with date_range, period_range
  111. and timedelta_range indexes
  112. """
  113. # _series_name is intentionally unused
  114. return DataFrame({"value": _static_values}, index=index)
  115. @pytest.fixture
  116. def empty_frame_dti(series):
  117. """
  118. Fixture for parametrization of empty DataFrame with date_range,
  119. period_range and timedelta_range indexes
  120. """
  121. index = series.index[:0]
  122. return DataFrame(index=index)
  123. @pytest.fixture(params=[Series, DataFrame])
  124. def series_and_frame(request, series, frame):
  125. """
  126. Fixture for parametrization of Series and DataFrame with date_range,
  127. period_range and timedelta_range indexes
  128. """
  129. if request.param == Series:
  130. return series
  131. if request.param == DataFrame:
  132. return frame