-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
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 70021bb
use index fixture for test_custom_grouper
simonjayhawkins 4062c25
use series fixture for test_resample_basic
simonjayhawkins b57ffae
use series fixture for test_resample_string_kwargs
simonjayhawkins a2049f6
parametrize test_resample_string_kwargs
simonjayhawkins 1826669
use series fixture for test_resample_how
simonjayhawkins 285aab2
use series fixture for test_resample_how_ohlc
simonjayhawkins 731e399
parametrize test_resample_basic
simonjayhawkins 2e3d4eb
parametrize test_numpy_compat
simonjayhawkins b957006
move timedelta tests to test_timedelta.py
simonjayhawkins 525ec03
Merge remote-tracking branch 'upstream/master' into resample-dti
simonjayhawkins 42da1c1
isort imports
simonjayhawkins df56265
add doc-strings to fixtures
simonjayhawkins 238911e
suppress FutureWarning for Panel
simonjayhawkins 55e8100
fix line too long warning
simonjayhawkins 1b1aa87
remove class from test_datetime_index.py
simonjayhawkins 3cb5982
remove class from test_timedelta.py
simonjayhawkins 137f6e8
hoist imports in test_datetime_index.py
simonjayhawkins 37d72a7
hoist import in test_timedelta.py
simonjayhawkins 34ed69e
revert suppress Panel FutureWarning
simonjayhawkins 4baba7c
Merge remote-tracking branch 'upstream/master' into resample-dti
simonjayhawkins 4947d07
use pytest.raises as a context manager
simonjayhawkins d070899
Merge remote-tracking branch 'upstream/master' into resample-dti
simonjayhawkins 55b3954
move test to test_timedelta.py
simonjayhawkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do these actually to be class based at all? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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]) | ||
|
@@ -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'}, | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.