3
3
from datetime import datetime , timedelta
4
4
from functools import partial
5
5
6
- from pandas . compat import range , lrange , zip , product , OrderedDict
6
+ import nose
7
7
import numpy as np
8
8
9
+ import pandas as pd
10
+ import pandas .tseries .offsets as offsets
11
+ import pandas .util .testing as tm
9
12
from pandas import (Series , DataFrame , Panel , Index , isnull ,
10
13
notnull , Timestamp )
11
-
14
+ from pandas .compat import range , lrange , zip , product , OrderedDict
15
+ from pandas .core .base import SpecificationError
16
+ from pandas .core .common import ABCSeries , ABCDataFrame
12
17
from pandas .core .groupby import DataError
18
+ from pandas .tseries .frequencies import MONTHS , DAYS
13
19
from pandas .tseries .index import date_range
14
- from pandas .tseries .tdi import timedelta_range
15
20
from pandas .tseries .offsets import Minute , BDay
16
21
from pandas .tseries .period import period_range , PeriodIndex , Period
17
22
from pandas .tseries .resample import (DatetimeIndex , TimeGrouper ,
18
23
DatetimeIndexResampler )
19
- from pandas .tseries .frequencies import MONTHS , DAYS
20
- from pandas .core .common import ABCSeries , ABCDataFrame
21
- from pandas .core .base import SpecificationError
22
-
23
- import pandas .tseries .offsets as offsets
24
- import pandas as pd
25
-
26
- import nose
27
-
24
+ from pandas .tseries .tdi import timedelta_range
28
25
from pandas .util .testing import (assert_series_equal , assert_almost_equal ,
29
26
assert_frame_equal )
30
- import pandas .util .testing as tm
31
27
32
28
bday = BDay ()
29
+ downsample_methods = ['min' , 'max' , 'first' , 'last' , 'sum' , 'mean' , 'sem' ,
30
+ 'median' , 'prod' , 'ohlc' ]
31
+ upsample_methods = ['count' , 'size' ]
32
+ series_methods = ['nunique' ]
33
+ resample_methods = downsample_methods + upsample_methods + series_methods
33
34
34
35
35
36
class TestResampleAPI (tm .TestCase ):
@@ -95,12 +96,13 @@ def test_api_changes_v018(self):
95
96
self .assertRaises (ValueError , lambda : r .iat [0 ])
96
97
self .assertRaises (ValueError , lambda : r .ix [0 ])
97
98
self .assertRaises (ValueError , lambda : r .loc [
98
- Timestamp ('2013-01-01 00:00:00' , offset = 'H' )])
99
+ Timestamp ('2013-01-01 00:00:00' , offset = 'H' )])
99
100
self .assertRaises (ValueError , lambda : r .at [
100
- Timestamp ('2013-01-01 00:00:00' , offset = 'H' )])
101
+ Timestamp ('2013-01-01 00:00:00' , offset = 'H' )])
101
102
102
103
def f ():
103
104
r [0 ] = 5
105
+
104
106
self .assertRaises (ValueError , f )
105
107
106
108
# str/repr
@@ -144,7 +146,6 @@ def f():
144
146
145
147
# comparison ops
146
148
for op in ['__lt__' , '__le__' , '__gt__' , '__ge__' , '__eq__' , '__ne__' ]:
147
-
148
149
r = self .series .resample ('H' )
149
150
150
151
with tm .assert_produces_warning (FutureWarning ,
@@ -259,6 +260,7 @@ def test_attribute_access(self):
259
260
# setting
260
261
def f ():
261
262
r .F = 'bah'
263
+
262
264
self .assertRaises (ValueError , f )
263
265
264
266
def test_api_compat_before_use (self ):
@@ -509,10 +511,10 @@ def test_agg_misc(self):
509
511
# errors
510
512
# invalid names in the agg specification
511
513
for t in [r , g ]:
512
-
513
514
def f ():
514
515
r [['A' ]].agg ({'A' : ['sum' , 'std' ],
515
516
'B' : ['mean' , 'std' ]})
517
+
516
518
self .assertRaises (SpecificationError , f )
517
519
518
520
def test_agg_nested_dicts (self ):
@@ -679,7 +681,7 @@ def _ohlc(group):
679
681
assert_series_equal (result , expected )
680
682
except BaseException as exc :
681
683
682
- exc .args += ('how=%s' % arg , )
684
+ exc .args += ('how=%s' % arg ,)
683
685
raise
684
686
685
687
def test_resample_how_callables (self ):
@@ -692,7 +694,6 @@ def fn(x, a=1):
692
694
return str (type (x ))
693
695
694
696
class fn_class :
695
-
696
697
def __call__ (self , x ):
697
698
return str (type (x ))
698
699
@@ -768,7 +769,7 @@ def test_resample_rounding(self):
768
769
769
770
from pandas .compat import StringIO
770
771
df = pd .read_csv (StringIO (data ), parse_dates = {'timestamp' : [
771
- 'date' , 'time' ]}, index_col = 'timestamp' )
772
+ 'date' , 'time' ]}, index_col = 'timestamp' )
772
773
df .index .name = None
773
774
result = df .resample ('6s' ).sum ()
774
775
expected = DataFrame ({'value' : [
@@ -1061,10 +1062,10 @@ def test_resample_ohlc_dataframe(self):
1061
1062
1062
1063
df .columns = [['a' , 'b' ], ['c' , 'd' ]]
1063
1064
res = df .resample ('H' ).ohlc ()
1064
- exp .columns = pd .MultiIndex .from_tuples ([( 'a' , 'c' , 'open' ), (
1065
- 'a' , 'c' , 'high ' ), ('a' , 'c' , 'low ' ), ('a' , 'c' , 'close ' ), (
1066
- 'b ' , 'd ' , 'open ' ), ('b' , 'd' , 'high ' ), ('b' , 'd' , 'low ' ), (
1067
- 'b' , 'd' , 'close' )])
1065
+ exp .columns = pd .MultiIndex .from_tuples ([
1066
+ ( 'a' , 'c' , 'open ' ), ('a' , 'c' , 'high ' ), ('a' , 'c' , 'low ' ),
1067
+ ( 'a ' , 'c ' , 'close ' ), ('b' , 'd' , 'open ' ), ('b' , 'd' , 'high ' ),
1068
+ ( 'b' , 'd' , 'low' ), ( 'b' , 'd' , 'close' )])
1068
1069
assert_frame_equal (exp , res )
1069
1070
1070
1071
# dupe columns fail atm
@@ -1449,11 +1450,12 @@ def test_resample_anchored_multiday(self):
1449
1450
#
1450
1451
# See: https://github.com/pydata/pandas/issues/8683
1451
1452
1452
- s = pd .Series (np .random .randn (5 ),
1453
- index = pd .date_range ('2014-10-14 23:06:23.206' ,
1454
- periods = 3 , freq = '400L' ) |
1455
- pd .date_range ('2014-10-15 23:00:00' ,
1456
- periods = 2 , freq = '2200L' ))
1453
+ index = pd .date_range (
1454
+ '2014-10-14 23:06:23.206' , periods = 3 , freq = '400L'
1455
+ ) | pd .date_range (
1456
+ '2014-10-15 23:00:00' , periods = 2 , freq = '2200L' )
1457
+
1458
+ s = pd .Series (np .random .randn (5 ), index = index )
1457
1459
1458
1460
# Ensure left closing works
1459
1461
result = s .resample ('2200L' ).mean ()
@@ -1763,7 +1765,6 @@ def _simple_pts(start, end, freq='D'):
1763
1765
1764
1766
1765
1767
class TestResamplePeriodIndex (tm .TestCase ):
1766
-
1767
1768
_multiprocess_can_split_ = True
1768
1769
1769
1770
def test_annual_upsample_D_s_f (self ):
@@ -1907,16 +1908,40 @@ def test_resample_basic(self):
1907
1908
1908
1909
def test_resample_empty (self ):
1909
1910
1910
- # GH12771
1911
+ # GH12771 & GH12868
1911
1912
index = PeriodIndex (start = '2000' , periods = 0 , freq = 'D' , name = 'idx' )
1912
1913
s = Series (index = index )
1913
- result = s .resample ('M' ).sum ()
1914
1914
1915
- # after GH12774 is resolved, this should be a PeriodIndex
1916
- expected_index = DatetimeIndex ([], name = 'idx' )
1915
+ expected_index = PeriodIndex ([], name = 'idx' , freq = 'M' )
1917
1916
expected = Series (index = expected_index )
1917
+
1918
+ for method in resample_methods :
1919
+ result = getattr (s .resample ('M' ), method )()
1920
+ assert_series_equal (result , expected )
1921
+
1922
+ def test_resample_count (self ):
1923
+
1924
+ # GH12774
1925
+ series = pd .Series (1 , index = pd .period_range (start = '2000' ,
1926
+ periods = 100 ))
1927
+ result = series .resample ('M' ).count ()
1928
+
1929
+ expected_index = pd .period_range (start = '2000' , freq = 'M' , periods = 4 )
1930
+ expected = pd .Series ([31 , 29 , 31 , 9 ], index = expected_index )
1931
+
1918
1932
assert_series_equal (result , expected )
1919
1933
1934
+ def test_resample_same_freq (self ):
1935
+
1936
+ # GH12770
1937
+ series = pd .Series (range (3 ), index = pd .period_range (
1938
+ start = '2000' , periods = 3 , freq = 'M' ))
1939
+ expected = series
1940
+
1941
+ for method in resample_methods :
1942
+ result = getattr (series .resample ('M' ), method )()
1943
+ assert_series_equal (result , expected )
1944
+
1920
1945
def test_with_local_timezone_pytz (self ):
1921
1946
# GH5430
1922
1947
tm ._skip_if_no_pytz ()
@@ -2493,8 +2518,8 @@ def test_aggregate_with_nat(self):
2493
2518
# GH 9925
2494
2519
self .assertEqual (dt_result .index .name , 'key' )
2495
2520
2496
- # if NaT is included, 'var', 'std', 'mean', 'first','last' and 'nth '
2497
- # doesn't work yet
2521
+ # if NaT is included, 'var', 'std', 'mean', 'first','last'
2522
+ # and 'nth' doesn't work yet
2498
2523
2499
2524
2500
2525
if __name__ == '__main__' :
0 commit comments