@@ -1504,32 +1504,33 @@ def func(self, numeric_only=numeric_only, min_count=min_count):
1504
1504
1505
1505
return func
1506
1506
1507
- def first_compat (x , axis = 0 ):
1508
- def first (x ):
1509
- x = x .to_numpy ()
1510
-
1511
- x = x [notna (x )]
1507
+ def first_compat (obj : FrameOrSeries , axis : int = 0 ):
1508
+ def first (x : Series ):
1509
+ x = x .array [notna (x .array )]
1512
1510
if len (x ) == 0 :
1513
1511
return np .nan
1514
1512
return x [0 ]
1515
1513
1516
- if isinstance (x , DataFrame ):
1517
- return x .apply (first , axis = axis )
1514
+ if isinstance (obj , DataFrame ):
1515
+ return obj .apply (first , axis = axis )
1516
+ elif isinstance (obj , Series ):
1517
+ return first (obj )
1518
1518
else :
1519
- return first ( x )
1519
+ raise TypeError ( type ( obj ) )
1520
1520
1521
- def last_compat (x , axis = 0 ):
1522
- def last (x ):
1523
- x = x .to_numpy ()
1524
- x = x [notna (x )]
1521
+ def last_compat (obj : FrameOrSeries , axis : int = 0 ):
1522
+ def last (x : Series ):
1523
+ x = x .array [notna (x .array )]
1525
1524
if len (x ) == 0 :
1526
1525
return np .nan
1527
1526
return x [- 1 ]
1528
1527
1529
- if isinstance (x , DataFrame ):
1530
- return x .apply (last , axis = axis )
1528
+ if isinstance (obj , DataFrame ):
1529
+ return obj .apply (last , axis = axis )
1530
+ elif isinstance (obj , Series ):
1531
+ return last (obj )
1531
1532
else :
1532
- return last ( x )
1533
+ raise TypeError ( type ( obj ) )
1533
1534
1534
1535
cls .sum = groupby_function ("sum" , "add" , np .sum , min_count = 0 )
1535
1536
cls .prod = groupby_function ("prod" , "prod" , np .prod , min_count = 0 )
0 commit comments