@@ -1583,3 +1583,110 @@ def f_4(grp):
1583
1583
e .loc ["Pony" ] = np .nan
1584
1584
e .name = None
1585
1585
tm .assert_series_equal (result , e )
1586
+
1587
+
1588
+ # GH58291
1589
+ def test_apply_frame_not_as_index_returns_single_index ():
1590
+ df = DataFrame (
1591
+ [
1592
+ ["group_a" , 0 ],
1593
+ ["group_a" , 2 ],
1594
+ ["group_b" , 1 ],
1595
+ ["group_b" , 3 ],
1596
+ ["group_b" , 5 ],
1597
+ ],
1598
+ columns = ["group" , "value" ],
1599
+ )
1600
+ gb = df .groupby ("group" , as_index = False )[["group" , "value" ]]
1601
+
1602
+ def up_to_two_rows (df : DataFrame ) -> DataFrame :
1603
+ return df .head (2 )
1604
+
1605
+ result = gb .apply (up_to_two_rows )
1606
+
1607
+ expected = DataFrame (
1608
+ [["group_a" , 0 ], ["group_a" , 2 ], ["group_b" , 1 ], ["group_b" , 3 ]],
1609
+ columns = ["group" , "value" ],
1610
+ index = range (4 ),
1611
+ )
1612
+ tm .assert_frame_equal (result , expected )
1613
+
1614
+
1615
+ # GH58291
1616
+ def test_apply_column_groupby_frame_not_as_index_returns_single_index (df ):
1617
+ gb = df .groupby ("A" , as_index = False )[["A" , "C" , "D" ]]
1618
+ result = gb .apply (lambda x : x .min ())
1619
+ print (str (result ))
1620
+
1621
+ expected = DataFrame (
1622
+ [["bar" , - 2.441467 , - 2.441467 ], ["foo" , - 0.413064 , - 0.413064 ]],
1623
+ columns = ["A" , "C" , "D" ],
1624
+ index = range (2 ),
1625
+ )
1626
+ tm .assert_frame_equal (result , expected )
1627
+
1628
+
1629
+ # GH58291
1630
+ def test_apply_non_column_groupby_frame_not_as_index_returns_single_index (tsframe ):
1631
+ gb = tsframe .groupby (lambda x : x .month , as_index = False )
1632
+ result = gb .apply (lambda x : x .mean ())
1633
+ print (str (result ))
1634
+
1635
+ expected = DataFrame (
1636
+ [
1637
+ [1 , 0.115464 , 0.260960 , 0.187824 , - 0.411523 ],
1638
+ [2 , 0.047104 , - 0.183591 , 0.330640 , - 0.207427 ],
1639
+ ],
1640
+ columns = ["index" , "A" , "B" , "C" , "D" ],
1641
+ index = range (2 ),
1642
+ )
1643
+ tm .assert_frame_equal (result , expected )
1644
+
1645
+ result = gb .apply (lambda x : x .mean () + x .max ())
1646
+ print (str (result ))
1647
+
1648
+ expected = DataFrame (
1649
+ [
1650
+ [1 , 2.490351 , 2.062381 , 2.244527 , 0.912824 ],
1651
+ [2 , 1.918738 , 2.273746 , 1.759184 , 0.700975 ],
1652
+ ],
1653
+ columns = ["index" , "A" , "B" , "C" , "D" ],
1654
+ index = range (2 ),
1655
+ )
1656
+ tm .assert_frame_equal (result , expected )
1657
+
1658
+ result = gb .apply (lambda x : 1 )
1659
+ print (str (result ))
1660
+
1661
+ expected = DataFrame ([[1 , 1 ], [2 , 1 ]], columns = ["index" , None ], index = range (2 ))
1662
+ tm .assert_frame_equal (result , expected )
1663
+
1664
+ result = gb .apply (lambda x : x .quantile ([0.2 , 0.38 ]))
1665
+ print (str (result ))
1666
+
1667
+ expected = DataFrame (
1668
+ [
1669
+ [- 0.742155 , - 0.607186 , - 0.325423 , - 1.254187 ],
1670
+ [- 0.212477 , - 0.034416 , - 0.117452 , - 0.561536 ],
1671
+ [- 0.842537 , - 0.920719 , - 0.404496 , - 0.729036 ],
1672
+ [- 0.411161 , - 0.830757 , 0.321981 , - 0.444410 ],
1673
+ ],
1674
+ columns = ["A" , "B" , "C" , "D" ],
1675
+ index = [0.20 , 0.38 ] * 2 ,
1676
+ )
1677
+ tm .assert_frame_equal (result , expected )
1678
+
1679
+ result = gb .apply (DataFrame .quantile , [0.2 , 0.38 ])
1680
+ print (str (result ))
1681
+
1682
+ expected = DataFrame (
1683
+ [
1684
+ [- 0.742155 , - 0.607186 , - 0.325423 , - 1.254187 ],
1685
+ [- 0.212477 , - 0.034416 , - 0.117452 , - 0.561536 ],
1686
+ [- 0.842537 , - 0.920719 , - 0.404496 , - 0.729036 ],
1687
+ [- 0.411161 , - 0.830757 , 0.321981 , - 0.444410 ],
1688
+ ],
1689
+ columns = ["A" , "B" , "C" , "D" ],
1690
+ index = [0.20 , 0.38 ] * 2 ,
1691
+ )
1692
+ tm .assert_frame_equal (result , expected )
0 commit comments