Skip to content

REF/TST: Add more pytest idiom to resample/test_datetime_index.py #24414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Dec 28, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c99d355
replace setup_method with fixtures
simonjayhawkins Dec 21, 2018
70021bb
use index fixture for test_custom_grouper
simonjayhawkins Dec 21, 2018
4062c25
use series fixture for test_resample_basic
simonjayhawkins Dec 21, 2018
b57ffae
use series fixture for test_resample_string_kwargs
simonjayhawkins Dec 21, 2018
a2049f6
parametrize test_resample_string_kwargs
simonjayhawkins Dec 21, 2018
1826669
use series fixture for test_resample_how
simonjayhawkins Dec 21, 2018
285aab2
use series fixture for test_resample_how_ohlc
simonjayhawkins Dec 21, 2018
731e399
parametrize test_resample_basic
simonjayhawkins Dec 21, 2018
2e3d4eb
parametrize test_numpy_compat
simonjayhawkins Dec 21, 2018
b957006
move timedelta tests to test_timedelta.py
simonjayhawkins Dec 21, 2018
525ec03
Merge remote-tracking branch 'upstream/master' into resample-dti
simonjayhawkins Dec 24, 2018
42da1c1
isort imports
simonjayhawkins Dec 24, 2018
df56265
add doc-strings to fixtures
simonjayhawkins Dec 24, 2018
238911e
suppress FutureWarning for Panel
simonjayhawkins Dec 24, 2018
55e8100
fix line too long warning
simonjayhawkins Dec 24, 2018
1b1aa87
remove class from test_datetime_index.py
simonjayhawkins Dec 25, 2018
3cb5982
remove class from test_timedelta.py
simonjayhawkins Dec 25, 2018
137f6e8
hoist imports in test_datetime_index.py
simonjayhawkins Dec 26, 2018
37d72a7
hoist import in test_timedelta.py
simonjayhawkins Dec 26, 2018
34ed69e
revert suppress Panel FutureWarning
simonjayhawkins Dec 26, 2018
4baba7c
Merge remote-tracking branch 'upstream/master' into resample-dti
simonjayhawkins Dec 26, 2018
4947d07
use pytest.raises as a context manager
simonjayhawkins Dec 26, 2018
d070899
Merge remote-tracking branch 'upstream/master' into resample-dti
simonjayhawkins Dec 27, 2018
55b3954
move test to test_timedelta.py
simonjayhawkins Dec 28, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions pandas/tests/resample/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,26 @@ def _index_freq():


@pytest.fixture
def index(_index_factory, _index_start, _index_end, _index_freq):
return _index_factory(_index_start, _index_end, freq=_index_freq)
def _index_name():
return None


@pytest.fixture
def index(_index_factory, _index_start, _index_end, _index_freq, _index_name):
return _index_factory(
_index_start, _index_end, freq=_index_freq, name=_index_name)


@pytest.fixture
def _static_values(index):
return np.arange(len(index))


@pytest.fixture
def _series_name():
return None


@pytest.fixture
def series(index, _series_name, _static_values):
return Series(_static_values, index=index, name=_series_name)
Expand Down
225 changes: 81 additions & 144 deletions pandas/tests/resample/test_datetime_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
from pandas.errors import UnsupportedFunctionCall

import pandas as pd
from pandas import (
DataFrame, Index, Panel, Series, Timedelta, Timestamp, isna, notna)
from pandas import DataFrame, Panel, Series, Timedelta, Timestamp, isna, notna
from pandas.core.indexes.datetimes import date_range
from pandas.core.indexes.period import Period, period_range
from pandas.core.indexes.timedeltas import timedelta_range
from pandas.core.resample import (
DatetimeIndex, TimeGrouper, _get_timestamp_range_edges)
import pandas.util.testing as tm
Expand All @@ -25,18 +23,25 @@
from pandas.tseries.offsets import BDay, Minute


class TestDatetimeIndex(object):
def setup_method(self, method):
dti = date_range(start=datetime(2005, 1, 1),
end=datetime(2005, 1, 10), freq='Min')
@pytest.fixture()
def _index_factory():
return date_range


@pytest.fixture
def _index_freq():
return 'Min'

self.series = Series(np.random.rand(len(dti)), dti)

def test_custom_grouper(self):
@pytest.fixture
def _static_values(index):
return np.random.rand(len(index))

dti = date_range(freq='Min', start=datetime(2005, 1, 1),
end=datetime(2005, 1, 10))

class TestDatetimeIndex(object):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do these actually to be class based at all?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for TDI?

def test_custom_grouper(self, index):

dti = index
s = Series(np.array([1] * len(dti)), index=dti, dtype='int64')

b = TimeGrouper(Minute(5))
Expand Down Expand Up @@ -74,55 +79,57 @@ def test_custom_grouper(self):
assert len(r.columns) == 10
assert len(r.index) == 2593

def test_resample_basic(self):
rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min',
name='index')
s = Series(np.random.randn(14), index=rng)

result = s.resample('5min', closed='right', label='right').mean()

exp_idx = date_range('1/1/2000', periods=4, freq='5min', name='index')
expected = Series([s[0], s[1:6].mean(), s[6:11].mean(), s[11:].mean()],
index=exp_idx)
assert_series_equal(result, expected)
assert result.index.name == 'index'

result = s.resample('5min', closed='left', label='right').mean()

exp_idx = date_range('1/1/2000 00:05', periods=3, freq='5min',
name='index')
expected = Series([s[:5].mean(), s[5:10].mean(),
s[10:].mean()], index=exp_idx)
@pytest.mark.parametrize(
'_index_start,_index_end,_index_name',
[('1/1/2000 00:00:00', '1/1/2000 00:13:00', 'index')])
@pytest.mark.parametrize('closed, expected', [
('right',
lambda s: Series(
[s[0], s[1:6].mean(), s[6:11].mean(), s[11:].mean()],
index=date_range(
'1/1/2000', periods=4, freq='5min', name='index'))),
('left',
lambda s: Series(
[s[:5].mean(), s[5:10].mean(), s[10:].mean()],
index=date_range(
'1/1/2000 00:05', periods=3, freq='5min', name='index'))
)
])
def test_resample_basic(self, series, closed, expected):
s = series
expected = expected(s)
result = s.resample('5min', closed=closed, label='right').mean()
assert_series_equal(result, expected)

s = self.series
def test_resample_basic_grouper(self, series):
s = series
result = s.resample('5Min').last()
grouper = TimeGrouper(Minute(5), closed='left', label='left')
expect = s.groupby(grouper).agg(lambda x: x[-1])
assert_series_equal(result, expect)

def test_resample_string_kwargs(self):
# Test for issue #19303
rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min',
name='index')
s = Series(np.random.randn(14), index=rng)
expected = s.groupby(grouper).agg(lambda x: x[-1])
assert_series_equal(result, expected)

@pytest.mark.parametrize(
'_index_start,_index_end,_index_name',
[('1/1/2000 00:00:00', '1/1/2000 00:13:00', 'index')])
@pytest.mark.parametrize('kwargs', [
dict(label='righttt'),
dict(closed='righttt'),
dict(convention='starttt')
])
def test_resample_string_kwargs(self, series, kwargs):
# see gh-19303
# Check that wrong keyword argument strings raise an error
with pytest.raises(ValueError):
s.resample('5min', label='righttt').mean()
with pytest.raises(ValueError):
s.resample('5min', closed='righttt').mean()
with pytest.raises(ValueError):
s.resample('5min', convention='starttt').mean()

def test_resample_how(self, downsample_method):
with pytest.raises(ValueError, match='Unsupported value'):
series.resample('5min', **kwargs)

@pytest.mark.parametrize(
'_index_start,_index_end,_index_name',
[('1/1/2000 00:00:00', '1/1/2000 00:13:00', 'index')])
def test_resample_how(self, series, downsample_method):
if downsample_method == 'ohlc':
pytest.skip('covered by test_resample_how_ohlc')

rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min',
name='index')
s = Series(np.random.randn(14), index=rng)

s = series
grouplist = np.ones_like(s)
grouplist[0] = 0
grouplist[1:6] = 1
Expand All @@ -134,14 +141,13 @@ def test_resample_how(self, downsample_method):

result = getattr(s.resample(
'5min', closed='right', label='right'), downsample_method)()

assert result.index.name == 'index' # redundant assert?
assert_series_equal(result, expected)

def test_resample_how_ohlc(self):
rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min',
name='index')
s = Series(np.random.randn(14), index=rng)
@pytest.mark.parametrize(
'_index_start,_index_end,_index_name',
[('1/1/2000 00:00:00', '1/1/2000 00:13:00', 'index')])
def test_resample_how_ohlc(self, series):
s = series
grouplist = np.ones_like(s)
grouplist[0] = 0
grouplist[1:6] = 1
Expand All @@ -153,31 +159,28 @@ def _ohlc(group):
return np.repeat(np.nan, 4)
return [group[0], group.max(), group.min(), group[-1]]

inds = date_range('1/1/2000', periods=4, freq='5min', name='index')
expected = s.groupby(grouplist).agg(_ohlc)
expected = DataFrame(expected.values.tolist(),
index=Index(inds, name='index'),
columns=['open', 'high', 'low', 'close'])
expected = DataFrame(
s.groupby(grouplist).agg(_ohlc).values.tolist(),
index=date_range('1/1/2000', periods=4, freq='5min', name='index'),
columns=['open', 'high', 'low', 'close'])

result = s.resample('5min', closed='right', label='right').ohlc()

assert result.index.name == 'index' # redundant assert?
assert_frame_equal(result, expected)

def test_numpy_compat(self):
@pytest.mark.parametrize(
'func', ['min', 'max', 'sum', 'prod', 'mean', 'var', 'std'])
def test_numpy_compat(self, func):
# see gh-12811
s = Series([1, 2, 3, 4, 5], index=date_range(
'20130101', periods=5, freq='s'))
r = s.resample('2s')

msg = "numpy operations are not valid with resample"

for func in ('min', 'max', 'sum', 'prod',
'mean', 'var', 'std'):
with pytest.raises(UnsupportedFunctionCall, match=msg):
getattr(r, func)(func, 1, 2, 3)
with pytest.raises(UnsupportedFunctionCall, match=msg):
getattr(r, func)(axis=1)
with pytest.raises(UnsupportedFunctionCall, match=msg):
getattr(r, func)(func, 1, 2, 3)
with pytest.raises(UnsupportedFunctionCall, match=msg):
getattr(r, func)(axis=1)

def test_resample_how_callables(self):
# GH#7929
Expand All @@ -204,40 +207,6 @@ def __call__(self, x):
assert_frame_equal(df_standard, df_partial2)
assert_frame_equal(df_standard, df_class)

def test_resample_with_timedeltas(self):

expected = DataFrame({'A': np.arange(1480)})
expected = expected.groupby(expected.index // 30).sum()
expected.index = pd.timedelta_range('0 days', freq='30T', periods=50)

df = DataFrame({'A': np.arange(1480)}, index=pd.to_timedelta(
np.arange(1480), unit='T'))
result = df.resample('30T').sum()

assert_frame_equal(result, expected)

s = df['A']
result = s.resample('30T').sum()
assert_series_equal(result, expected['A'])

def test_resample_single_period_timedelta(self):

s = Series(list(range(5)), index=pd.timedelta_range(
'1 day', freq='s', periods=5))
result = s.resample('2s').sum()
expected = Series([1, 5, 4], index=pd.timedelta_range(
'1 day', freq='2s', periods=3))
assert_series_equal(result, expected)

def test_resample_timedelta_idempotency(self):

# GH 12072
index = pd.timedelta_range('0', periods=9, freq='10L')
series = Series(range(9), index=index)
result = series.resample('10L').mean()
expected = series
assert_series_equal(result, expected)

def test_resample_rounding(self):
# GH 8371
# odd results when rounding is needed
Expand Down Expand Up @@ -519,8 +488,8 @@ def test_nearest_upsample_with_limit(self):
expected = ts.reindex(result.index, method='nearest', limit=2)
assert_series_equal(result, expected)

def test_resample_ohlc(self):
s = self.series
def test_resample_ohlc(self, series):
s = series

grouper = TimeGrouper(Minute(5))
expect = s.groupby(grouper).agg(lambda x: x[-1])
Expand Down Expand Up @@ -785,21 +754,6 @@ def test_resample_base(self):
freq='5min')
tm.assert_index_equal(resampled.index, exp_rng)

def test_resample_base_with_timedeltaindex(self):

# GH 10530
rng = timedelta_range(start='0s', periods=25, freq='s')
ts = Series(np.random.randn(len(rng)), index=rng)

with_base = ts.resample('2s', base=5).mean()
without_base = ts.resample('2s').mean()

exp_without_base = timedelta_range(start='0s', end='25s', freq='2s')
exp_with_base = timedelta_range(start='5s', end='29s', freq='2s')

tm.assert_index_equal(without_base.index, exp_without_base)
tm.assert_index_equal(with_base.index, exp_with_base)

def test_resample_categorical_data_with_timedeltaindex(self):
# GH #12169
df = DataFrame({'Group_obj': 'A'},
Expand Down Expand Up @@ -1401,23 +1355,6 @@ def test_resample_with_nat(self):

assert_frame_equal(frame.resample('60s').mean(), frame_3s)

def test_resample_timedelta_values(self):
# GH 13119
# check that timedelta dtype is preserved when NaT values are
# introduced by the resampling

times = timedelta_range('1 day', '4 day', freq='4D')
df = DataFrame({'time': times}, index=times)

times2 = timedelta_range('1 day', '4 day', freq='2D')
exp = Series(times2, index=times2, name='time')
exp.iloc[1] = pd.NaT

res = df.resample('2D').first()['time']
tm.assert_series_equal(res, exp)
res = df['time'].resample('2D').first()
tm.assert_series_equal(res, exp)

def test_resample_datetime_values(self):
# GH 13119
# check that datetime dtype is preserved when NaT values are
Expand All @@ -1435,19 +1372,19 @@ def test_resample_datetime_values(self):
res = df['timestamp'].resample('2D').first()
tm.assert_series_equal(res, exp)

def test_resample_apply_with_additional_args(self):
def test_resample_apply_with_additional_args(self, series):
# GH 14615
def f(data, add_arg):
return np.mean(data) * add_arg

multiplier = 10
result = self.series.resample('D').apply(f, multiplier)
expected = self.series.resample('D').mean().multiply(multiplier)
result = series.resample('D').apply(f, multiplier)
expected = series.resample('D').mean().multiply(multiplier)
tm.assert_series_equal(result, expected)

# Testing as kwarg
result = self.series.resample('D').apply(f, add_arg=multiplier)
expected = self.series.resample('D').mean().multiply(multiplier)
result = series.resample('D').apply(f, add_arg=multiplier)
expected = series.resample('D').mean().multiply(multiplier)
tm.assert_series_equal(result, expected)

# Testing dataframe
Expand Down
Loading