conftest.py 4.1 KB

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