@@ -3638,33 +3638,83 @@ def test_to_records() -> None:
3638
3638
)
3639
3639
3640
3640
3641
- def test_to_dict () -> None :
3641
+ def test_to_dict_simple () -> None :
3642
3642
check (assert_type (DF .to_dict (), dict [Hashable , Any ]), dict )
3643
3643
check (assert_type (DF .to_dict ("split" ), dict [Hashable , Any ]), dict )
3644
+ check (assert_type (DF .to_dict ("records" ), list [dict [Hashable , Any ]]), list )
3645
+
3646
+ if TYPE_CHECKING_INVALID_USAGE :
3647
+
3648
+ def test (mapping : Mapping ) -> None : # pyright: ignore[reportUnusedFunction]
3649
+ DF .to_dict ( # type: ignore[call-overload]
3650
+ into = mapping # pyright: ignore[reportArgumentType,reportCallIssue]
3651
+ )
3652
+
3653
+
3654
+ def test_to_dict_into_defaultdict_any () -> None :
3655
+ """Test DataFrame.to_dict with `into=defaultdict[Any, list]`"""
3656
+
3657
+ data = pd .DataFrame ({("str" , "rts" ): [[1 , 2 , 4 ], [2 , 3 ], [3 ]]})
3658
+ target : defaultdict [Hashable , list [Any ]] = defaultdict (list )
3644
3659
3645
- target : MutableMapping = defaultdict (list )
3646
3660
check (
3647
- assert_type (DF .to_dict (into = target ), MutableMapping [Hashable , Any ]), defaultdict
3661
+ assert_type (data .to_dict (into = target ), defaultdict [Hashable , list [Any ]]),
3662
+ defaultdict ,
3648
3663
)
3649
- target = defaultdict (list )
3650
3664
check (
3651
- assert_type (DF .to_dict ("tight" , into = target ), MutableMapping [Hashable , Any ]),
3665
+ assert_type (
3666
+ data .to_dict ("index" , into = target ),
3667
+ MutableMapping [Hashable , defaultdict [Hashable , list [Any ]]],
3668
+ ),
3669
+ defaultdict ,
3670
+ )
3671
+ check (
3672
+ assert_type (
3673
+ data .to_dict ("tight" , into = target ), defaultdict [Hashable , list [Any ]]
3674
+ ),
3652
3675
defaultdict ,
3653
3676
)
3654
- target = defaultdict (list )
3655
- check (assert_type (DF .to_dict ("records" ), list [dict [Hashable , Any ]]), list )
3656
3677
check (
3657
3678
assert_type (
3658
- DF .to_dict ("records" , into = target ), list [MutableMapping [Hashable , Any ]]
3679
+ data .to_dict ("records" , into = target ), list [defaultdict [Hashable , list [ Any ] ]]
3659
3680
),
3660
3681
list ,
3661
3682
)
3662
- if TYPE_CHECKING_INVALID_USAGE :
3663
3683
3664
- def test (mapping : Mapping ) -> None : # pyright: ignore[reportUnusedFunction]
3665
- DF .to_dict ( # type: ignore[call-overload]
3666
- into = mapping # pyright: ignore[reportArgumentType,reportCallIssue]
3667
- )
3684
+
3685
+ def test_to_dict_into_defaultdict_typed () -> None :
3686
+ """Test DataFrame.to_dict with `into=defaultdict[tuple[str, str], list[int]]`"""
3687
+
3688
+ data = pd .DataFrame ({("str" , "rts" ): [[1 , 2 , 4 ], [2 , 3 ], [3 ]]})
3689
+ target : defaultdict [tuple [str , str ], list [int ]] = defaultdict (list )
3690
+ target [("str" , "rts" )].append (1 )
3691
+
3692
+ check (
3693
+ assert_type (data .to_dict (into = target ), defaultdict [tuple [str , str ], list [int ]]),
3694
+ defaultdict ,
3695
+ tuple ,
3696
+ )
3697
+ check (
3698
+ assert_type (
3699
+ data .to_dict ("index" , into = target ),
3700
+ MutableMapping [Hashable , defaultdict [tuple [str , str ], list [int ]]],
3701
+ ),
3702
+ defaultdict ,
3703
+ )
3704
+ check (
3705
+ assert_type (
3706
+ data .to_dict ("tight" , into = target ), defaultdict [tuple [str , str ], list [int ]]
3707
+ ),
3708
+ defaultdict ,
3709
+ )
3710
+ check (
3711
+ assert_type (
3712
+ data .to_dict ("records" , into = target ),
3713
+ list [defaultdict [tuple [str , str ], list [int ]]],
3714
+ ),
3715
+ list ,
3716
+ defaultdict ,
3717
+ )
3668
3718
3669
3719
3670
3720
def test_neg () -> None :
@@ -4111,7 +4161,10 @@ def test_to_dict_index() -> None:
4111
4161
assert_type (df .to_dict (orient = "series" , index = True ), dict [Hashable , Any ]), dict
4112
4162
)
4113
4163
check (
4114
- assert_type (df .to_dict (orient = "index" , index = True ), dict [Hashable , Any ]), dict
4164
+ assert_type (
4165
+ df .to_dict (orient = "index" , index = True ), dict [Hashable , dict [Hashable , Any ]]
4166
+ ),
4167
+ dict ,
4115
4168
)
4116
4169
check (
4117
4170
assert_type (df .to_dict (orient = "split" , index = True ), dict [Hashable , Any ]), dict
0 commit comments