@@ -477,6 +477,9 @@ def returns_listlike_of_2(x: pd.Series) -> tuple[int, int]:
477
477
def returns_listlike_of_3 (x : pd .Series ) -> tuple [int , int , int ]:
478
478
return (7 , 8 , 9 )
479
479
480
+ def returns_dict (x : pd .Series ) -> dict [str , int ]:
481
+ return {"col4" : 7 , "col5" : 8 }
482
+
480
483
# Misc checks
481
484
check (assert_type (df .apply (np .exp ), pd .DataFrame ), pd .DataFrame )
482
485
check (assert_type (df .apply (str ), "pd.Series[str]" ), pd .Series , str )
@@ -489,22 +492,15 @@ def gethead(s: pd.Series, y: int) -> pd.Series:
489
492
490
493
# Check various return types for default result_type (None) with default axis (0)
491
494
check (assert_type (df .apply (returns_scalar ), "pd.Series[int]" ), pd .Series , int )
492
- check (
493
- assert_type (df .apply (returns_series ), pd .DataFrame ),
494
- pd .DataFrame ,
495
- )
496
- check (
497
- assert_type (df .apply (returns_listlike_of_3 ), pd .DataFrame ),
498
- pd .DataFrame ,
499
- )
495
+ check (assert_type (df .apply (returns_series ), pd .DataFrame ), pd .DataFrame )
496
+ check (assert_type (df .apply (returns_listlike_of_3 ), pd .DataFrame ), pd .DataFrame )
497
+ check (assert_type (df .apply (returns_dict ), pd .Series ), pd .Series )
500
498
501
499
# Check various return types for result_type="expand" with default axis (0)
502
500
check (
503
- assert_type (
504
- # Note that technically it does not make sense to pass a result_type of "expand" to a scalar return
505
- df .apply (returns_scalar , result_type = "expand" ),
506
- "pd.Series[int]" ,
507
- ),
501
+ # Note that technically it does not make sense
502
+ # to pass a result_type of "expand" to a scalar return
503
+ assert_type (df .apply (returns_scalar , result_type = "expand" ), "pd.Series[int]" ),
508
504
pd .Series ,
509
505
int ,
510
506
)
@@ -518,71 +514,47 @@ def gethead(s: pd.Series, y: int) -> pd.Series:
518
514
),
519
515
pd .DataFrame ,
520
516
)
517
+ check (
518
+ assert_type (df .apply (returns_dict , result_type = "expand" ), pd .DataFrame ),
519
+ pd .DataFrame ,
520
+ )
521
521
522
522
# Check various return types for result_type="reduce" with default axis (0)
523
523
check (
524
- assert_type (
525
- # Note that technically it does not make sense to pass a result_type of "reduce" to a scalar return
526
- df .apply (returns_scalar , result_type = "reduce" ),
527
- "pd.Series[int]" ,
528
- ),
524
+ # Note that technically it does not make sense
525
+ # to pass a result_type of "reduce" to a scalar return
526
+ assert_type (df .apply (returns_scalar , result_type = "reduce" ), "pd.Series[int]" ),
529
527
pd .Series ,
530
528
int ,
531
529
)
532
530
check (
533
- assert_type (
534
- # Note that technically it does not make sense to pass a result_type of "reduce" to a series return
535
- df .apply (returns_series , result_type = "reduce" ),
536
- pd .Series ,
537
- ),
531
+ # Note that technically it does not make sense
532
+ # to pass a result_type of "reduce" to a series return
533
+ assert_type (df .apply (returns_series , result_type = "reduce" ), pd .Series ),
538
534
pd .Series , # This technically returns a pd.Series[pd.Series], but typing does not support that
539
535
)
540
536
check (
541
537
assert_type (df .apply (returns_listlike_of_3 , result_type = "reduce" ), pd .Series ),
542
538
pd .Series ,
543
539
)
544
-
545
- # Check various return types for result_type="broadcast" with default axis (0)
546
540
check (
547
- assert_type (
548
- # Note that technically it does not make sense to pass a result_type of "broadcast" to a scalar return
549
- df .apply (returns_scalar , result_type = "broadcast" ),
550
- pd .DataFrame ,
551
- ),
552
- pd .DataFrame ,
553
- )
554
- check (
555
- assert_type (df .apply (returns_series , result_type = "broadcast" ), pd .DataFrame ),
556
- pd .DataFrame ,
557
- )
558
- check (
559
- assert_type (
560
- # Can only broadcast a list-like of 2 elements, not 3, because there are 2 rows
561
- df .apply (returns_listlike_of_2 , result_type = "broadcast" ),
562
- pd .DataFrame ,
563
- ),
564
- pd .DataFrame ,
541
+ assert_type (df .apply (returns_dict , result_type = "reduce" ), pd .Series ), pd .Series
565
542
)
566
543
567
544
# Check various return types for default result_type (None) with axis=1
568
545
check (
569
546
assert_type (df .apply (returns_scalar , axis = 1 ), "pd.Series[int]" ), pd .Series , int
570
547
)
571
- check (
572
- assert_type (df .apply (returns_series , axis = 1 ), pd .DataFrame ),
573
- pd .DataFrame ,
574
- )
575
- check (
576
- assert_type (df .apply (returns_listlike_of_3 , axis = 1 ), pd .Series ),
577
- pd .Series ,
578
- )
548
+ check (assert_type (df .apply (returns_series , axis = 1 ), pd .DataFrame ), pd .DataFrame )
549
+ check (assert_type (df .apply (returns_listlike_of_3 , axis = 1 ), pd .Series ), pd .Series )
550
+ check (assert_type (df .apply (returns_dict , axis = 1 ), pd .Series ), pd .Series )
579
551
580
552
# Check various return types for result_type="expand" with axis=1
581
553
check (
554
+ # Note that technically it does not make sense
555
+ # to pass a result_type of "expand" to a scalar return
582
556
assert_type (
583
- # Note that technically it does not make sense to pass a result_type of "expand" to a scalar return
584
- df .apply (returns_scalar , axis = 1 , result_type = "expand" ),
585
- "pd.Series[int]" ,
557
+ df .apply (returns_scalar , axis = 1 , result_type = "expand" ), "pd.Series[int]"
586
558
),
587
559
pd .Series ,
588
560
int ,
@@ -599,22 +571,26 @@ def gethead(s: pd.Series, y: int) -> pd.Series:
599
571
),
600
572
pd .DataFrame ,
601
573
)
574
+ check (
575
+ assert_type (df .apply (returns_dict , axis = 1 , result_type = "expand" ), pd .DataFrame ),
576
+ pd .DataFrame ,
577
+ )
602
578
603
579
# Check various return types for result_type="reduce" with axis=1
604
580
check (
581
+ # Note that technically it does not make sense
582
+ # to pass a result_type of "reduce" to a scalar return
605
583
assert_type (
606
- # Note that technically it does not make sense to pass a result_type of "reduce" to a scalar return
607
- df .apply (returns_scalar , axis = 1 , result_type = "reduce" ),
608
- "pd.Series[int]" ,
584
+ df .apply (returns_scalar , axis = 1 , result_type = "reduce" ), "pd.Series[int]"
609
585
),
610
586
pd .Series ,
611
587
int ,
612
588
)
613
589
check (
590
+ # Note that technically it does not make sense
591
+ # to pass a result_type of "reduce" to a series return
614
592
assert_type (
615
- # Note that technically it does not make sense to pass a result_type of "reduce" to a series return
616
- df .apply (returns_series , axis = 1 , result_type = "reduce" ),
617
- pd .DataFrame ,
593
+ df .apply (returns_series , axis = 1 , result_type = "reduce" ), pd .DataFrame
618
594
),
619
595
pd .DataFrame ,
620
596
)
@@ -624,13 +600,34 @@ def gethead(s: pd.Series, y: int) -> pd.Series:
624
600
),
625
601
pd .Series ,
626
602
)
603
+ check (
604
+ assert_type (df .apply (returns_dict , axis = 1 , result_type = "reduce" ), pd .Series ),
605
+ pd .Series ,
606
+ )
627
607
628
- # Check various return types for result_type="broadcast" with axis=1
608
+ # Check various return types for result_type="broadcast" with axis=0 and axis=1
609
+ check (
610
+ # Note that technically it does not make sense
611
+ # to pass a result_type of "broadcast" to a scalar return
612
+ assert_type (df .apply (returns_scalar , result_type = "broadcast" ), pd .DataFrame ),
613
+ pd .DataFrame ,
614
+ )
615
+ check (
616
+ assert_type (df .apply (returns_series , result_type = "broadcast" ), pd .DataFrame ),
617
+ pd .DataFrame ,
618
+ )
629
619
check (
620
+ # Can only broadcast a list-like of 2 elements, not 3, because there are 2 rows
630
621
assert_type (
631
- # Note that technicaly it does not make sense to pass a result_type of "broadcast" to a scalar return
632
- df .apply (returns_scalar , axis = 1 , result_type = "broadcast" ),
633
- pd .DataFrame ,
622
+ df .apply (returns_listlike_of_2 , result_type = "broadcast" ), pd .DataFrame
623
+ ),
624
+ pd .DataFrame ,
625
+ )
626
+ check (
627
+ # Note that technicaly it does not make sense
628
+ # to pass a result_type of "broadcast" to a scalar return
629
+ assert_type (
630
+ df .apply (returns_scalar , axis = 1 , result_type = "broadcast" ), pd .DataFrame
634
631
),
635
632
pd .DataFrame ,
636
633
)
@@ -642,14 +639,29 @@ def gethead(s: pd.Series, y: int) -> pd.Series:
642
639
)
643
640
check (
644
641
assert_type (
645
- # Can only broadcast a list-like of 3 elements, not 2, as there are 3 columns
642
+ # Can only broadcast a list-like of 3 elements, not 2,
643
+ # as there are 3 columns
646
644
df .apply (returns_listlike_of_3 , axis = 1 , result_type = "broadcast" ),
647
645
pd .DataFrame ,
648
646
),
649
647
pd .DataFrame ,
650
648
)
649
+ # Since dicts will be assigned to elements of np.ndarray inside broadcasting,
650
+ # we need to use a DataFrame with object dtype to make the assignment possible.
651
+ df2 = pd .DataFrame ({"col1" : ["a" , "b" ], "col2" : ["c" , "d" ]})
652
+ check (
653
+ assert_type (df2 .apply (returns_dict , result_type = "broadcast" ), pd .DataFrame ),
654
+ pd .DataFrame ,
655
+ )
656
+ check (
657
+ assert_type (
658
+ df2 .apply (returns_dict , axis = 1 , result_type = "broadcast" ), pd .DataFrame
659
+ ),
660
+ pd .DataFrame ,
661
+ )
651
662
652
- # Test various other positional/keyword argument combinations to ensure all overloads are supported
663
+ # Test various other positional/keyword argument combinations
664
+ # to ensure all overloads are supported
653
665
check (
654
666
assert_type (df .apply (returns_scalar , axis = 0 ), "pd.Series[int]" ), pd .Series , int
655
667
)
0 commit comments