6
6
import pytest
7
7
import pytz
8
8
9
+ from pandas ._libs import lib
9
10
from pandas .errors import UnsupportedFunctionCall
10
11
11
12
import pandas as pd
@@ -62,6 +63,7 @@ def test_custom_grouper(index):
62
63
arr = [1 ] + [5 ] * 2592
63
64
idx = dti [0 :- 1 :5 ]
64
65
idx = idx .append (dti [- 1 :])
66
+ idx = pd .DatetimeIndex (idx , freq = "5T" )
65
67
expect = Series (arr , index = idx )
66
68
67
69
# GH2763 - return in put dtype if we can
@@ -502,15 +504,18 @@ def test_resample_how_method():
502
504
)
503
505
expected = Series (
504
506
[11 , np .NaN , np .NaN , np .NaN , np .NaN , np .NaN , 22 ],
505
- index = [
506
- Timestamp ("2015-03-31 21:48:50" ),
507
- Timestamp ("2015-03-31 21:49:00" ),
508
- Timestamp ("2015-03-31 21:49:10" ),
509
- Timestamp ("2015-03-31 21:49:20" ),
510
- Timestamp ("2015-03-31 21:49:30" ),
511
- Timestamp ("2015-03-31 21:49:40" ),
512
- Timestamp ("2015-03-31 21:49:50" ),
513
- ],
507
+ index = pd .DatetimeIndex (
508
+ [
509
+ Timestamp ("2015-03-31 21:48:50" ),
510
+ Timestamp ("2015-03-31 21:49:00" ),
511
+ Timestamp ("2015-03-31 21:49:10" ),
512
+ Timestamp ("2015-03-31 21:49:20" ),
513
+ Timestamp ("2015-03-31 21:49:30" ),
514
+ Timestamp ("2015-03-31 21:49:40" ),
515
+ Timestamp ("2015-03-31 21:49:50" ),
516
+ ],
517
+ freq = "10s" ,
518
+ ),
514
519
)
515
520
tm .assert_series_equal (s .resample ("10S" ).mean (), expected )
516
521
@@ -778,7 +783,7 @@ def test_resample_single_group():
778
783
[30.1 , 31.6 ],
779
784
index = [Timestamp ("20070915 15:30:00" ), Timestamp ("20070915 15:40:00" )],
780
785
)
781
- expected = Series ([0.75 ], index = [Timestamp ("20070915" )])
786
+ expected = Series ([0.75 ], index = pd . DatetimeIndex ( [Timestamp ("20070915" )], freq = "D" ) )
782
787
result = s .resample ("D" ).apply (lambda x : np .std (x ))
783
788
tm .assert_series_equal (result , expected )
784
789
@@ -801,7 +806,9 @@ def test_resample_float_base():
801
806
802
807
base = 17 + 43.51 / 60
803
808
result = s .resample ("3min" , base = base ).size ()
804
- expected = Series (3 , index = pd .DatetimeIndex (["2018-11-26 16:17:43.51" ]))
809
+ expected = Series (
810
+ 3 , index = pd .DatetimeIndex (["2018-11-26 16:17:43.51" ], freq = "3min" )
811
+ )
805
812
tm .assert_series_equal (result , expected )
806
813
807
814
@@ -938,13 +945,17 @@ def test_resample_anchored_intraday(simple_date_range_series):
938
945
result = df .resample ("M" ).mean ()
939
946
expected = df .resample ("M" , kind = "period" ).mean ().to_timestamp (how = "end" )
940
947
expected .index += Timedelta (1 , "ns" ) - Timedelta (1 , "D" )
948
+ expected .index = expected .index ._with_freq ("infer" )
949
+ assert expected .index .freq == "M"
941
950
tm .assert_frame_equal (result , expected )
942
951
943
952
result = df .resample ("M" , closed = "left" ).mean ()
944
953
exp = df .tshift (1 , freq = "D" ).resample ("M" , kind = "period" ).mean ()
945
954
exp = exp .to_timestamp (how = "end" )
946
955
947
956
exp .index = exp .index + Timedelta (1 , "ns" ) - Timedelta (1 , "D" )
957
+ exp .index = exp .index ._with_freq ("infer" )
958
+ assert exp .index .freq == "M"
948
959
tm .assert_frame_equal (result , exp )
949
960
950
961
rng = date_range ("1/1/2012" , "4/1/2012" , freq = "100min" )
@@ -953,12 +964,16 @@ def test_resample_anchored_intraday(simple_date_range_series):
953
964
result = df .resample ("Q" ).mean ()
954
965
expected = df .resample ("Q" , kind = "period" ).mean ().to_timestamp (how = "end" )
955
966
expected .index += Timedelta (1 , "ns" ) - Timedelta (1 , "D" )
967
+ expected .index ._data .freq = "Q"
968
+ expected .index ._freq = lib .no_default
956
969
tm .assert_frame_equal (result , expected )
957
970
958
971
result = df .resample ("Q" , closed = "left" ).mean ()
959
972
expected = df .tshift (1 , freq = "D" ).resample ("Q" , kind = "period" , closed = "left" ).mean ()
960
973
expected = expected .to_timestamp (how = "end" )
961
974
expected .index += Timedelta (1 , "ns" ) - Timedelta (1 , "D" )
975
+ expected .index ._data .freq = "Q"
976
+ expected .index ._freq = lib .no_default
962
977
tm .assert_frame_equal (result , expected )
963
978
964
979
ts = simple_date_range_series ("2012-04-29 23:00" , "2012-04-30 5:00" , freq = "h" )
@@ -1151,6 +1166,8 @@ def test_resample_timegrouper():
1151
1166
name = "A" ,
1152
1167
)
1153
1168
expected = DataFrame ({"B" : [1 , 0 , 2 , 2 , 1 ]}, index = exp_idx )
1169
+ if df ["A" ].isna ().any ():
1170
+ expected .index = expected .index ._with_freq (None )
1154
1171
tm .assert_frame_equal (result , expected )
1155
1172
1156
1173
result = df .groupby (pd .Grouper (freq = "M" , key = "A" )).count ()
@@ -1163,6 +1180,8 @@ def test_resample_timegrouper():
1163
1180
index = exp_idx ,
1164
1181
columns = ["B" , "C" ],
1165
1182
)
1183
+ if df ["A" ].isna ().any ():
1184
+ expected .index = expected .index ._with_freq (None )
1166
1185
tm .assert_frame_equal (result , expected )
1167
1186
1168
1187
result = df .groupby (pd .Grouper (freq = "M" , key = "A" )).count ()
@@ -1291,7 +1310,8 @@ def test_resample_across_dst():
1291
1310
dti2 = DatetimeIndex (
1292
1311
pd .to_datetime (df2 .ts , unit = "s" )
1293
1312
.dt .tz_localize ("UTC" )
1294
- .dt .tz_convert ("Europe/Madrid" )
1313
+ .dt .tz_convert ("Europe/Madrid" ),
1314
+ freq = "H" ,
1295
1315
)
1296
1316
df = DataFrame ([5 , 5 ], index = dti1 )
1297
1317
@@ -1322,13 +1342,17 @@ def test_resample_dst_anchor():
1322
1342
# 5172
1323
1343
dti = DatetimeIndex ([datetime (2012 , 11 , 4 , 23 )], tz = "US/Eastern" )
1324
1344
df = DataFrame ([5 ], index = dti )
1325
- tm .assert_frame_equal (
1326
- df .resample (rule = "D" ).sum (), DataFrame ([5 ], index = df .index .normalize ())
1327
- )
1345
+
1346
+ dti = DatetimeIndex (df .index .normalize (), freq = "D" )
1347
+ expected = DataFrame ([5 ], index = dti )
1348
+ tm .assert_frame_equal (df .resample (rule = "D" ).sum (), expected )
1328
1349
df .resample (rule = "MS" ).sum ()
1329
1350
tm .assert_frame_equal (
1330
1351
df .resample (rule = "MS" ).sum (),
1331
- DataFrame ([5 ], index = DatetimeIndex ([datetime (2012 , 11 , 1 )], tz = "US/Eastern" )),
1352
+ DataFrame (
1353
+ [5 ],
1354
+ index = DatetimeIndex ([datetime (2012 , 11 , 1 )], tz = "US/Eastern" , freq = "MS" ),
1355
+ ),
1332
1356
)
1333
1357
1334
1358
dti = date_range ("2013-09-30" , "2013-11-02" , freq = "30Min" , tz = "Europe/Paris" )
@@ -1424,7 +1448,9 @@ def test_downsample_across_dst_weekly():
1424
1448
result = df .resample ("1W" ).sum ()
1425
1449
expected = DataFrame (
1426
1450
[23 , 42 ],
1427
- index = pd .DatetimeIndex (["2017-03-26" , "2017-04-02" ], tz = "Europe/Amsterdam" ),
1451
+ index = pd .DatetimeIndex (
1452
+ ["2017-03-26" , "2017-04-02" ], tz = "Europe/Amsterdam" , freq = "W"
1453
+ ),
1428
1454
)
1429
1455
tm .assert_frame_equal (result , expected )
1430
1456
@@ -1447,12 +1473,12 @@ def test_downsample_dst_at_midnight():
1447
1473
data = list (range (len (index )))
1448
1474
dataframe = pd .DataFrame (data , index = index )
1449
1475
result = dataframe .groupby (pd .Grouper (freq = "1D" )).mean ()
1450
- expected = DataFrame (
1451
- [7.5 , 28.0 , 44.5 ],
1452
- index = date_range ("2018-11-03" , periods = 3 ).tz_localize (
1453
- "America/Havana" , ambiguous = True
1454
- ),
1476
+
1477
+ dti = date_range ("2018-11-03" , periods = 3 ).tz_localize (
1478
+ "America/Havana" , ambiguous = True
1455
1479
)
1480
+ dti = pd .DatetimeIndex (dti , freq = "D" )
1481
+ expected = DataFrame ([7.5 , 28.0 , 44.5 ], index = dti ,)
1456
1482
tm .assert_frame_equal (result , expected )
1457
1483
1458
1484
0 commit comments