@@ -3638,33 +3638,83 @@ def test_to_records() -> None:
36383638 )
36393639
36403640
3641- def test_to_dict () -> None :
3641+ def test_to_dict_simple () -> None :
36423642 check (assert_type (DF .to_dict (), dict [Hashable , Any ]), dict )
36433643 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 )
36443659
3645- target : MutableMapping = defaultdict (list )
36463660 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 ,
36483663 )
3649- target = defaultdict (list )
36503664 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+ ),
36523675 defaultdict ,
36533676 )
3654- target = defaultdict (list )
3655- check (assert_type (DF .to_dict ("records" ), list [dict [Hashable , Any ]]), list )
36563677 check (
36573678 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 ] ]]
36593680 ),
36603681 list ,
36613682 )
3662- if TYPE_CHECKING_INVALID_USAGE :
36633683
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+ )
36683718
36693719
36703720def test_neg () -> None :
@@ -4111,7 +4161,10 @@ def test_to_dict_index() -> None:
41114161 assert_type (df .to_dict (orient = "series" , index = True ), dict [Hashable , Any ]), dict
41124162 )
41134163 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 ,
41154168 )
41164169 check (
41174170 assert_type (df .to_dict (orient = "split" , index = True ), dict [Hashable , Any ]), dict
0 commit comments