@@ -1119,7 +1119,7 @@ def test_nanosecond_resample_error():
1119
1119
tm .assert_series_equal (result , exp )
1120
1120
1121
1121
1122
- def test_resample_anchored_intraday (simple_date_range_series , unit ):
1122
+ def test_resample_anchored_intraday (unit ):
1123
1123
# #1471, #1458
1124
1124
1125
1125
rng = date_range ("1/1/2012" , "4/1/2012" , freq = "100min" ).as_unit (unit )
@@ -1141,6 +1141,8 @@ def test_resample_anchored_intraday(simple_date_range_series, unit):
1141
1141
assert exp .index .freq == "ME"
1142
1142
tm .assert_frame_equal (result , exp )
1143
1143
1144
+
1145
+ def test_resample_anchored_intraday2 (unit ):
1144
1146
rng = date_range ("1/1/2012" , "4/1/2012" , freq = "100min" ).as_unit (unit )
1145
1147
df = DataFrame (rng .month , index = rng )
1146
1148
@@ -1161,6 +1163,8 @@ def test_resample_anchored_intraday(simple_date_range_series, unit):
1161
1163
expected .index = expected .index .as_unit (unit )
1162
1164
tm .assert_frame_equal (result , expected )
1163
1165
1166
+
1167
+ def test_resample_anchored_intraday3 (simple_date_range_series , unit ):
1164
1168
ts = simple_date_range_series ("2012-04-29 23:00" , "2012-04-30 5:00" , freq = "h" )
1165
1169
ts .index = ts .index .as_unit (unit )
1166
1170
resampled = ts .resample ("ME" ).mean ()
@@ -1374,8 +1378,17 @@ def test_resample_timegrouper(dates):
1374
1378
result = df .groupby (Grouper (freq = "ME" , key = "A" )).count ()
1375
1379
tm .assert_frame_equal (result , expected )
1376
1380
1381
+
1382
+ @pytest .mark .parametrize ("dates" , [dates1 , dates2 , dates3 ])
1383
+ def test_resample_timegrouper2 (dates ):
1377
1384
df = DataFrame ({"A" : dates , "B" : np .arange (len (dates )), "C" : np .arange (len (dates ))})
1378
1385
result = df .set_index ("A" ).resample ("ME" ).count ()
1386
+
1387
+ exp_idx = DatetimeIndex (
1388
+ ["2014-07-31" , "2014-08-31" , "2014-09-30" , "2014-10-31" , "2014-11-30" ],
1389
+ freq = "ME" ,
1390
+ name = "A" ,
1391
+ )
1379
1392
expected = DataFrame (
1380
1393
{"B" : [1 , 0 , 2 , 2 , 1 ], "C" : [1 , 0 , 2 , 2 , 1 ]},
1381
1394
index = exp_idx ,
@@ -1574,80 +1587,95 @@ def test_resample_dst_anchor(unit):
1574
1587
),
1575
1588
)
1576
1589
1590
+
1591
+ def test_resample_dst_anchor2 (unit ):
1577
1592
dti = date_range (
1578
1593
"2013-09-30" , "2013-11-02" , freq = "30Min" , tz = "Europe/Paris"
1579
1594
).as_unit (unit )
1580
1595
values = range (dti .size )
1581
1596
df = DataFrame ({"a" : values , "b" : values , "c" : values }, index = dti , dtype = "int64" )
1582
1597
how = {"a" : "min" , "b" : "max" , "c" : "count" }
1583
1598
1599
+ rs = df .resample ("W-MON" )
1600
+ result = rs .agg (how )[["a" , "b" , "c" ]]
1601
+ expected = DataFrame (
1602
+ {
1603
+ "a" : [0 , 48 , 384 , 720 , 1056 , 1394 ],
1604
+ "b" : [47 , 383 , 719 , 1055 , 1393 , 1586 ],
1605
+ "c" : [48 , 336 , 336 , 336 , 338 , 193 ],
1606
+ },
1607
+ index = date_range (
1608
+ "9/30/2013" , "11/4/2013" , freq = "W-MON" , tz = "Europe/Paris"
1609
+ ).as_unit (unit ),
1610
+ )
1584
1611
tm .assert_frame_equal (
1585
- df .resample ("W-MON" ).agg (how )[["a" , "b" , "c" ]],
1586
- DataFrame (
1587
- {
1588
- "a" : [0 , 48 , 384 , 720 , 1056 , 1394 ],
1589
- "b" : [47 , 383 , 719 , 1055 , 1393 , 1586 ],
1590
- "c" : [48 , 336 , 336 , 336 , 338 , 193 ],
1591
- },
1592
- index = date_range (
1593
- "9/30/2013" , "11/4/2013" , freq = "W-MON" , tz = "Europe/Paris"
1594
- ).as_unit (unit ),
1595
- ),
1612
+ result ,
1613
+ expected ,
1596
1614
"W-MON Frequency" ,
1597
1615
)
1598
1616
1617
+ rs2 = df .resample ("2W-MON" )
1618
+ result2 = rs2 .agg (how )[["a" , "b" , "c" ]]
1619
+ expected2 = DataFrame (
1620
+ {
1621
+ "a" : [0 , 48 , 720 , 1394 ],
1622
+ "b" : [47 , 719 , 1393 , 1586 ],
1623
+ "c" : [48 , 672 , 674 , 193 ],
1624
+ },
1625
+ index = date_range (
1626
+ "9/30/2013" , "11/11/2013" , freq = "2W-MON" , tz = "Europe/Paris"
1627
+ ).as_unit (unit ),
1628
+ )
1599
1629
tm .assert_frame_equal (
1600
- df .resample ("2W-MON" ).agg (how )[["a" , "b" , "c" ]],
1601
- DataFrame (
1602
- {
1603
- "a" : [0 , 48 , 720 , 1394 ],
1604
- "b" : [47 , 719 , 1393 , 1586 ],
1605
- "c" : [48 , 672 , 674 , 193 ],
1606
- },
1607
- index = date_range (
1608
- "9/30/2013" , "11/11/2013" , freq = "2W-MON" , tz = "Europe/Paris"
1609
- ).as_unit (unit ),
1610
- ),
1630
+ result2 ,
1631
+ expected2 ,
1611
1632
"2W-MON Frequency" ,
1612
1633
)
1613
1634
1614
- tm .assert_frame_equal (
1615
- df .resample ("MS" ).agg (how )[["a" , "b" , "c" ]],
1616
- DataFrame (
1617
- {"a" : [0 , 48 , 1538 ], "b" : [47 , 1537 , 1586 ], "c" : [48 , 1490 , 49 ]},
1618
- index = date_range (
1619
- "9/1/2013" , "11/1/2013" , freq = "MS" , tz = "Europe/Paris"
1620
- ).as_unit (unit ),
1635
+ rs3 = df .resample ("MS" )
1636
+ result3 = rs3 .agg (how )[["a" , "b" , "c" ]]
1637
+ expected3 = DataFrame (
1638
+ {"a" : [0 , 48 , 1538 ], "b" : [47 , 1537 , 1586 ], "c" : [48 , 1490 , 49 ]},
1639
+ index = date_range ("9/1/2013" , "11/1/2013" , freq = "MS" , tz = "Europe/Paris" ).as_unit (
1640
+ unit
1621
1641
),
1642
+ )
1643
+ tm .assert_frame_equal (
1644
+ result3 ,
1645
+ expected3 ,
1622
1646
"MS Frequency" ,
1623
1647
)
1624
1648
1649
+ rs4 = df .resample ("2MS" )
1650
+ result4 = rs4 .agg (how )[["a" , "b" , "c" ]]
1651
+ expected4 = DataFrame (
1652
+ {"a" : [0 , 1538 ], "b" : [1537 , 1586 ], "c" : [1538 , 49 ]},
1653
+ index = date_range (
1654
+ "9/1/2013" , "11/1/2013" , freq = "2MS" , tz = "Europe/Paris"
1655
+ ).as_unit (unit ),
1656
+ )
1625
1657
tm .assert_frame_equal (
1626
- df .resample ("2MS" ).agg (how )[["a" , "b" , "c" ]],
1627
- DataFrame (
1628
- {"a" : [0 , 1538 ], "b" : [1537 , 1586 ], "c" : [1538 , 49 ]},
1629
- index = date_range (
1630
- "9/1/2013" , "11/1/2013" , freq = "2MS" , tz = "Europe/Paris"
1631
- ).as_unit (unit ),
1632
- ),
1658
+ result4 ,
1659
+ expected4 ,
1633
1660
"2MS Frequency" ,
1634
1661
)
1635
1662
1636
1663
df_daily = df ["10/26/2013" :"10/29/2013" ]
1664
+ rs_d = df_daily .resample ("D" )
1665
+ result_d = rs_d .agg ({"a" : "min" , "b" : "max" , "c" : "count" })[["a" , "b" , "c" ]]
1666
+ expected_d = DataFrame (
1667
+ {
1668
+ "a" : [1248 , 1296 , 1346 , 1394 ],
1669
+ "b" : [1295 , 1345 , 1393 , 1441 ],
1670
+ "c" : [48 , 50 , 48 , 48 ],
1671
+ },
1672
+ index = date_range (
1673
+ "10/26/2013" , "10/29/2013" , freq = "D" , tz = "Europe/Paris"
1674
+ ).as_unit (unit ),
1675
+ )
1637
1676
tm .assert_frame_equal (
1638
- df_daily .resample ("D" ).agg ({"a" : "min" , "b" : "max" , "c" : "count" })[
1639
- ["a" , "b" , "c" ]
1640
- ],
1641
- DataFrame (
1642
- {
1643
- "a" : [1248 , 1296 , 1346 , 1394 ],
1644
- "b" : [1295 , 1345 , 1393 , 1441 ],
1645
- "c" : [48 , 50 , 48 , 48 ],
1646
- },
1647
- index = date_range (
1648
- "10/26/2013" , "10/29/2013" , freq = "D" , tz = "Europe/Paris"
1649
- ).as_unit (unit ),
1650
- ),
1677
+ result_d ,
1678
+ expected_d ,
1651
1679
"D Frequency" ,
1652
1680
)
1653
1681
@@ -1728,9 +1756,8 @@ def test_resample_with_nat(unit):
1728
1756
"1970-01-01 00:00:01" ,
1729
1757
"1970-01-01 00:00:02" ,
1730
1758
]
1731
- )
1759
+ ). as_unit ( unit )
1732
1760
frame = DataFrame ([2 , 3 , 5 , 7 , 11 ], index = index )
1733
- frame .index = frame .index .as_unit (unit )
1734
1761
1735
1762
index_1s = DatetimeIndex (
1736
1763
["1970-01-01 00:00:00" , "1970-01-01 00:00:01" , "1970-01-01 00:00:02" ]
@@ -1789,7 +1816,14 @@ def f(data, add_arg):
1789
1816
expected = series .resample ("D" ).mean ().multiply (multiplier )
1790
1817
tm .assert_series_equal (result , expected )
1791
1818
1819
+
1820
+ def test_resample_apply_with_additional_args2 ():
1792
1821
# Testing dataframe
1822
+ def f (data , add_arg ):
1823
+ return np .mean (data ) * add_arg
1824
+
1825
+ multiplier = 10
1826
+
1793
1827
df = DataFrame ({"A" : 1 , "B" : 2 }, index = date_range ("2017" , periods = 10 ))
1794
1828
msg = "DataFrameGroupBy.resample operated on the grouping columns"
1795
1829
with tm .assert_produces_warning (FutureWarning , match = msg ):
0 commit comments