From 449eb46a44096e3454c3cd4905a4d14e9f7ddf69 Mon Sep 17 00:00:00 2001 From: James Spencer Date: Wed, 13 Sep 2023 22:06:29 +0100 Subject: [PATCH 1/7] Correct type annotation for to_dict. The `into` argument of DataFrame.to_dict and Series.to_dict can be either a class or instance of a class of dict; this is covariant - subclasses of dict can also be used. The argument was annotated as `type[dict]` though, so type checkers marked passing initialized objects (required for collections.defaultdict) as an incorrect argument type. Fix by annotating `into` to take either a subclass of dict or an initialized instance of a subclass of dict. --- pandas/core/frame.py | 6 +++--- pandas/core/methods/to_dict.py | 2 +- pandas/core/series.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8fcb91c846826..f2497f7c6eaf7 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1925,7 +1925,7 @@ def _create_data_for_split_and_tight_to_dict( def to_dict( self, orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., - into: type[dict] = ..., + into: type[dict] | dict = ..., index: bool = ..., ) -> dict: ... @@ -1934,7 +1934,7 @@ def to_dict( def to_dict( self, orient: Literal["records"], - into: type[dict] = ..., + into: type[dict] | dict = ..., index: bool = ..., ) -> list[dict]: ... @@ -1947,7 +1947,7 @@ def to_dict( orient: Literal[ "dict", "list", "series", "split", "tight", "records", "index" ] = "dict", - into: type[dict] = dict, + into: type[dict] | dict = dict, index: bool = True, ) -> dict | list[dict]: """ diff --git a/pandas/core/methods/to_dict.py b/pandas/core/methods/to_dict.py index f4e0dcddcd34a..c6ab28fbacbb3 100644 --- a/pandas/core/methods/to_dict.py +++ b/pandas/core/methods/to_dict.py @@ -24,7 +24,7 @@ def to_dict( orient: Literal[ "dict", "list", "series", "split", "tight", "records", "index" ] = "dict", - into: type[dict] = dict, + into: type[dict] | dict = dict, index: bool = True, ) -> dict | list[dict]: """ diff --git a/pandas/core/series.py b/pandas/core/series.py index 9b5c8829fd5ff..ed9247a5a926e 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1926,7 +1926,7 @@ def keys(self) -> Index: """ return self.index - def to_dict(self, into: type[dict] = dict) -> dict: + def to_dict(self, into: type[dict] | dict = dict) -> dict: """ Convert Series to {label -> value} dict or dict-like object. From a0f7b68d08ed208c251caf80efb4bc671f78cbbe Mon Sep 17 00:00:00 2001 From: James Spencer Date: Fri, 15 Sep 2023 17:43:42 +0100 Subject: [PATCH 2/7] Use generic MutableMapping type for to_dict method. Unfortunately a generic type annotation with a default triggers an existing mypy limitation (https://github.com/python/mypy/issues/3737). The current workaround is to use overloads and then not annotate the implementation containing the default parameter; this still enables mypy to deduce correct return types. Two overloads are added for Series.to_dict, even though they could be combined using a Union type, as at least two overloads are required for a single method. --- pandas/_typing.py | 2 ++ pandas/core/frame.py | 22 ++++++++++++---------- pandas/core/methods/to_dict.py | 31 ++++++++++++++++++++++++++----- pandas/core/series.py | 27 +++++++++++++++++++++------ 4 files changed, 61 insertions(+), 21 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index c2bbebfbe2857..02305e835fad5 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -4,6 +4,7 @@ Hashable, Iterator, Mapping, + MutableMapping, Sequence, ) from datetime import ( @@ -100,6 +101,7 @@ TypeGuard: Any = None HashableT = TypeVar("HashableT", bound=Hashable) +MutableMappingT = TypeVar("MutableMappingT", bound=MutableMapping) # array-like diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f2497f7c6eaf7..91ff1ab537713 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -229,6 +229,7 @@ Level, MergeHow, MergeValidate, + MutableMappingT, NaAction, NaPosition, NsmallestNlargestKeep, @@ -1925,18 +1926,18 @@ def _create_data_for_split_and_tight_to_dict( def to_dict( self, orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., - into: type[dict] | dict = ..., + into: type[MutableMappingT] | MutableMappingT = ..., index: bool = ..., - ) -> dict: + ) -> MutableMappingT: ... @overload def to_dict( self, orient: Literal["records"], - into: type[dict] | dict = ..., + into: type[MutableMappingT] | MutableMappingT = ..., index: bool = ..., - ) -> list[dict]: + ) -> list[MutableMappingT]: ... @deprecate_nonkeyword_arguments( @@ -1947,9 +1948,9 @@ def to_dict( orient: Literal[ "dict", "list", "series", "split", "tight", "records", "index" ] = "dict", - into: type[dict] | dict = dict, + into = dict, index: bool = True, - ) -> dict | list[dict]: + ): """ Convert the DataFrame to a dictionary. @@ -1977,7 +1978,7 @@ def to_dict( 'tight' as an allowed value for the ``orient`` argument into : class, default dict - The collections.abc.Mapping subclass used for all Mappings + The collections.abc.MutableMapping subclass used for all Mappings in the return value. Can be the actual class or an empty instance of the mapping type you want. If you want a collections.defaultdict, you must pass it initialized. @@ -1991,9 +1992,10 @@ def to_dict( Returns ------- - dict, list or collections.abc.Mapping - Return a collections.abc.Mapping object representing the DataFrame. - The resulting transformation depends on the `orient` parameter. + dict, list or collections.abc.MutableMapping + Return a collections.abc.MutableMapping object representing the + DataFrame. The resulting transformation depends on the `orient` + parameter. See Also -------- diff --git a/pandas/core/methods/to_dict.py b/pandas/core/methods/to_dict.py index c6ab28fbacbb3..bdc4a5f7065c8 100644 --- a/pandas/core/methods/to_dict.py +++ b/pandas/core/methods/to_dict.py @@ -3,6 +3,7 @@ from typing import ( TYPE_CHECKING, Literal, + overload, ) import warnings @@ -17,16 +18,36 @@ if TYPE_CHECKING: from pandas import DataFrame + from pandas._typing import MutableMappingT +@overload +def to_dict( + df: DataFrame, + orient: Literal[ + "dict", "list", "series", "split", "tight", "index"] = ..., + into: type[MutableMappingT] | MutableMappingT = ..., + index: bool = True, +) -> MutableMappingT: + ... + +@overload +def to_dict( + df: DataFrame, + orient: Literal["records"], + into: type[MutableMappingT] | MutableMappingT, + index: bool = True, +) -> list[MutableMappingT]: + ... + def to_dict( df: DataFrame, orient: Literal[ "dict", "list", "series", "split", "tight", "records", "index" ] = "dict", - into: type[dict] | dict = dict, + into = dict, index: bool = True, -) -> dict | list[dict]: +): """ Convert the DataFrame to a dictionary. @@ -54,7 +75,7 @@ def to_dict( 'tight' as an allowed value for the ``orient`` argument into : class, default dict - The collections.abc.Mapping subclass used for all Mappings + The collections.abc.MutableMapping subclass used for all Mappings in the return value. Can be the actual class or an empty instance of the mapping type you want. If you want a collections.defaultdict, you must pass it initialized. @@ -69,8 +90,8 @@ def to_dict( Returns ------- dict, list or collections.abc.Mapping - Return a collections.abc.Mapping object representing the DataFrame. - The resulting transformation depends on the `orient` parameter. + Return a collections.abc.MutableMapping object representing the + DataFrame. The resulting transformation depends on the `orient` parameter. """ if not df.columns.is_unique: warnings.warn( diff --git a/pandas/core/series.py b/pandas/core/series.py index ed9247a5a926e..6da9244fa17f7 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -171,6 +171,7 @@ IndexKeyFunc, IndexLabel, Level, + MutableMappingT, NaPosition, NumpySorter, NumpyValueArrayLike, @@ -1926,21 +1927,35 @@ def keys(self) -> Index: """ return self.index - def to_dict(self, into: type[dict] | dict = dict) -> dict: + @overload + def to_dict( + self, + into: type[MutableMappingT] = ..., + ) -> MutableMappingT: + ... + + @overload + def to_dict( + self, + into: MutableMappingT = ..., + ) -> MutableMappingT: + ... + + def to_dict(self, into = dict): """ Convert Series to {label -> value} dict or dict-like object. Parameters ---------- into : class, default dict - The collections.abc.Mapping subclass to use as the return - object. Can be the actual class or an empty - instance of the mapping type you want. If you want a - collections.defaultdict, you must pass it initialized. + The collections.abc.MutableMapping subclass to use as the return + object. Can be the actual class or an empty instance of the mapping + type you want. If you want a collections.defaultdict, you must + pass it initialized. Returns ------- - collections.abc.Mapping + collections.abc.MutableMapping Key-value representation of Series. Examples From a3361694f82f4ce736527f5e158a984e4c28acd2 Mon Sep 17 00:00:00 2001 From: James Spencer Date: Fri, 15 Sep 2023 18:58:22 +0100 Subject: [PATCH 3/7] Fix formatting --- pandas/core/frame.py | 2 +- pandas/core/methods/to_dict.py | 10 ++++++---- pandas/core/series.py | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 91ff1ab537713..4f986d5bafa87 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1948,7 +1948,7 @@ def to_dict( orient: Literal[ "dict", "list", "series", "split", "tight", "records", "index" ] = "dict", - into = dict, + into=dict, index: bool = True, ): """ diff --git a/pandas/core/methods/to_dict.py b/pandas/core/methods/to_dict.py index bdc4a5f7065c8..801c120fcf698 100644 --- a/pandas/core/methods/to_dict.py +++ b/pandas/core/methods/to_dict.py @@ -17,20 +17,21 @@ from pandas.core import common as com if TYPE_CHECKING: - from pandas import DataFrame from pandas._typing import MutableMappingT + from pandas import DataFrame + @overload def to_dict( df: DataFrame, - orient: Literal[ - "dict", "list", "series", "split", "tight", "index"] = ..., + orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., into: type[MutableMappingT] | MutableMappingT = ..., index: bool = True, ) -> MutableMappingT: ... + @overload def to_dict( df: DataFrame, @@ -40,12 +41,13 @@ def to_dict( ) -> list[MutableMappingT]: ... + def to_dict( df: DataFrame, orient: Literal[ "dict", "list", "series", "split", "tight", "records", "index" ] = "dict", - into = dict, + into=dict, index: bool = True, ): """ diff --git a/pandas/core/series.py b/pandas/core/series.py index 6da9244fa17f7..0a8d8d3c03bf4 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1941,7 +1941,7 @@ def to_dict( ) -> MutableMappingT: ... - def to_dict(self, into = dict): + def to_dict(self, into=dict): """ Convert Series to {label -> value} dict or dict-like object. From 7642da08d0970c43fa68a47c34c60a35543fcc45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 29 Sep 2023 09:56:56 -0400 Subject: [PATCH 4/7] return annotation for non-overload --- pandas/core/frame.py | 7 +++++-- pandas/core/methods/to_dict.py | 6 ++++-- pandas/core/series.py | 16 ++++------------ 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 4f986d5bafa87..21598238f856e 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1940,6 +1940,8 @@ def to_dict( ) -> list[MutableMappingT]: ... + # error: Incompatible default for argument "into" (default has type "type + # [dict[Any, Any]]", argument has type "type[MutableMappingT] | MutableMappingT") @deprecate_nonkeyword_arguments( version="3.0", allowed_args=["self", "orient"], name="to_dict" ) @@ -1948,9 +1950,10 @@ def to_dict( orient: Literal[ "dict", "list", "series", "split", "tight", "records", "index" ] = "dict", - into=dict, + into: type[MutableMappingT] + | MutableMappingT = dict, # type: ignore[assignment] index: bool = True, - ): + ) -> MutableMappingT | list[MutableMappingT]: """ Convert the DataFrame to a dictionary. diff --git a/pandas/core/methods/to_dict.py b/pandas/core/methods/to_dict.py index 801c120fcf698..915a9c463b1f3 100644 --- a/pandas/core/methods/to_dict.py +++ b/pandas/core/methods/to_dict.py @@ -42,14 +42,16 @@ def to_dict( ... +# error: Incompatible default for argument "into" (default has type "type[dict +# [Any, Any]]", argument has type "type[MutableMappingT] | MutableMappingT") def to_dict( df: DataFrame, orient: Literal[ "dict", "list", "series", "split", "tight", "records", "index" ] = "dict", - into=dict, + into: type[MutableMappingT] | MutableMappingT = dict, # type: ignore[assignment] index: bool = True, -): +) -> MutableMappingT | list[MutableMappingT]: """ Convert the DataFrame to a dictionary. diff --git a/pandas/core/series.py b/pandas/core/series.py index 0a8d8d3c03bf4..9eb7ac94f2b05 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1927,21 +1927,13 @@ def keys(self) -> Index: """ return self.index - @overload + # error: Incompatible default for argument "into" (default has type "type[ + # dict[Any, Any]]", argument has type "type[MutableMappingT] | MutableMappingT") def to_dict( self, - into: type[MutableMappingT] = ..., + into: type[MutableMappingT] + | MutableMappingT = dict, # type: ignore[assignment] ) -> MutableMappingT: - ... - - @overload - def to_dict( - self, - into: MutableMappingT = ..., - ) -> MutableMappingT: - ... - - def to_dict(self, into=dict): """ Convert Series to {label -> value} dict or dict-like object. From 6502bf736b2b1a7c861afa8c9fd7b53e8448fc14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 29 Sep 2023 10:29:05 -0400 Subject: [PATCH 5/7] no keyword should return dict --- pandas/core/frame.py | 28 +++++++++++++++++++++++++--- pandas/core/methods/to_dict.py | 33 +++++++++++++++++++++++++++++---- pandas/core/series.py | 14 ++++++++++++++ 3 files changed, 68 insertions(+), 7 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f5eb2517a9929..115404458e2bc 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1928,7 +1928,28 @@ def _create_data_for_split_and_tight_to_dict( def to_dict( self, orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., - into: type[MutableMappingT] | MutableMappingT = ..., + *, + into: type[dict] = ..., + index: bool = ..., + ) -> dict: + ... + + @overload + def to_dict( + self, + orient: Literal["records"], + *, + into: type[dict] = ..., + index: bool = ..., + ) -> list[dict]: + ... + + @overload + def to_dict( + self, + orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., + *, + into: type[MutableMappingT] | MutableMappingT, index: bool = ..., ) -> MutableMappingT: ... @@ -1937,7 +1958,8 @@ def to_dict( def to_dict( self, orient: Literal["records"], - into: type[MutableMappingT] | MutableMappingT = ..., + *, + into: type[MutableMappingT] | MutableMappingT, index: bool = ..., ) -> list[MutableMappingT]: ... @@ -2059,7 +2081,7 @@ def to_dict( """ from pandas.core.methods.to_dict import to_dict - return to_dict(self, orient, into, index) + return to_dict(self, orient, into=into, index=index) @deprecate_nonkeyword_arguments( version="3.0", allowed_args=["self", "destination_table"], name="to_gbq" diff --git a/pandas/core/methods/to_dict.py b/pandas/core/methods/to_dict.py index 915a9c463b1f3..4beb4651d4550 100644 --- a/pandas/core/methods/to_dict.py +++ b/pandas/core/methods/to_dict.py @@ -26,8 +26,31 @@ def to_dict( df: DataFrame, orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., - into: type[MutableMappingT] | MutableMappingT = ..., - index: bool = True, + *, + into: type[dict] = ..., + index: bool = ..., +) -> dict: + ... + + +@overload +def to_dict( + df: DataFrame, + orient: Literal["records"], + *, + into: type[dict] = ..., + index: bool = ..., +) -> list[dict]: + ... + + +@overload +def to_dict( + df: DataFrame, + orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., + *, + into: type[MutableMappingT] | MutableMappingT, + index: bool = ..., ) -> MutableMappingT: ... @@ -36,8 +59,9 @@ def to_dict( def to_dict( df: DataFrame, orient: Literal["records"], + *, into: type[MutableMappingT] | MutableMappingT, - index: bool = True, + index: bool = ..., ) -> list[MutableMappingT]: ... @@ -49,6 +73,7 @@ def to_dict( orient: Literal[ "dict", "list", "series", "split", "tight", "records", "index" ] = "dict", + *, into: type[MutableMappingT] | MutableMappingT = dict, # type: ignore[assignment] index: bool = True, ) -> MutableMappingT | list[MutableMappingT]: @@ -128,7 +153,7 @@ def to_dict( are_all_object_dtype_cols = len(box_native_indices) == len(df.dtypes) if orient == "dict": - return into_c((k, v.to_dict(into)) for k, v in df.items()) + return into_c((k, v.to_dict(into=into)) for k, v in df.items()) elif orient == "list": object_dtype_indices_as_set: set[int] = set(box_native_indices) diff --git a/pandas/core/series.py b/pandas/core/series.py index 2c1f8a8253ecc..08da5f4d56cd3 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -48,6 +48,7 @@ from pandas.util._decorators import ( Appender, Substitution, + deprecate_nonkeyword_arguments, doc, ) from pandas.util._exceptions import find_stack_level @@ -1923,8 +1924,21 @@ def keys(self) -> Index: """ return self.index + @overload + def to_dict(self, *, into: type[dict] = ...) -> dict: + ... + + @overload + def to_dict( + self, *, into: type[MutableMappingT] | MutableMappingT + ) -> MutableMappingT: + ... + # error: Incompatible default for argument "into" (default has type "type[ # dict[Any, Any]]", argument has type "type[MutableMappingT] | MutableMappingT") + @deprecate_nonkeyword_arguments( + version="3.0", allowed_args=["self"], name="to_dict" + ) def to_dict( self, into: type[MutableMappingT] From 0d0e6147ab23e816a650363319b3a8e15f82fac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 29 Sep 2023 10:40:12 -0400 Subject: [PATCH 6/7] swap overload order to work for dict subclasses that are passed as keywords --- pandas/core/frame.py | 16 ++++++++-------- pandas/core/methods/to_dict.py | 16 ++++++++-------- pandas/core/series.py | 8 ++++---- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 115404458e2bc..51fceb1f09a62 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1929,9 +1929,9 @@ def to_dict( self, orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., *, - into: type[dict] = ..., + into: type[MutableMappingT] | MutableMappingT, index: bool = ..., - ) -> dict: + ) -> MutableMappingT: ... @overload @@ -1939,9 +1939,9 @@ def to_dict( self, orient: Literal["records"], *, - into: type[dict] = ..., + into: type[MutableMappingT] | MutableMappingT, index: bool = ..., - ) -> list[dict]: + ) -> list[MutableMappingT]: ... @overload @@ -1949,9 +1949,9 @@ def to_dict( self, orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., *, - into: type[MutableMappingT] | MutableMappingT, + into: type[dict] = ..., index: bool = ..., - ) -> MutableMappingT: + ) -> dict: ... @overload @@ -1959,9 +1959,9 @@ def to_dict( self, orient: Literal["records"], *, - into: type[MutableMappingT] | MutableMappingT, + into: type[dict] = ..., index: bool = ..., - ) -> list[MutableMappingT]: + ) -> list[dict]: ... # error: Incompatible default for argument "into" (default has type "type diff --git a/pandas/core/methods/to_dict.py b/pandas/core/methods/to_dict.py index 4beb4651d4550..3295c4741c03d 100644 --- a/pandas/core/methods/to_dict.py +++ b/pandas/core/methods/to_dict.py @@ -27,9 +27,9 @@ def to_dict( df: DataFrame, orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., *, - into: type[dict] = ..., + into: type[MutableMappingT] | MutableMappingT, index: bool = ..., -) -> dict: +) -> MutableMappingT: ... @@ -38,9 +38,9 @@ def to_dict( df: DataFrame, orient: Literal["records"], *, - into: type[dict] = ..., + into: type[MutableMappingT] | MutableMappingT, index: bool = ..., -) -> list[dict]: +) -> list[MutableMappingT]: ... @@ -49,9 +49,9 @@ def to_dict( df: DataFrame, orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., *, - into: type[MutableMappingT] | MutableMappingT, + into: type[dict] = ..., index: bool = ..., -) -> MutableMappingT: +) -> dict: ... @@ -60,9 +60,9 @@ def to_dict( df: DataFrame, orient: Literal["records"], *, - into: type[MutableMappingT] | MutableMappingT, + into: type[dict] = ..., index: bool = ..., -) -> list[MutableMappingT]: +) -> list[dict]: ... diff --git a/pandas/core/series.py b/pandas/core/series.py index 08da5f4d56cd3..4979ac01f1c43 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1924,16 +1924,16 @@ def keys(self) -> Index: """ return self.index - @overload - def to_dict(self, *, into: type[dict] = ...) -> dict: - ... - @overload def to_dict( self, *, into: type[MutableMappingT] | MutableMappingT ) -> MutableMappingT: ... + @overload + def to_dict(self, *, into: type[dict] = ...) -> dict: + ... + # error: Incompatible default for argument "into" (default has type "type[ # dict[Any, Any]]", argument has type "type[MutableMappingT] | MutableMappingT") @deprecate_nonkeyword_arguments( From c1dbf336f4eff1d393929988c2f76124be64bd8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Fri, 29 Sep 2023 14:09:43 -0400 Subject: [PATCH 7/7] fix tests --- pandas/core/series.py | 4 ++-- pandas/tests/series/methods/test_to_dict.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 4979ac01f1c43..d5785a2171cb3 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1966,10 +1966,10 @@ def to_dict( >>> s.to_dict() {0: 1, 1: 2, 2: 3, 3: 4} >>> from collections import OrderedDict, defaultdict - >>> s.to_dict(OrderedDict) + >>> s.to_dict(into=OrderedDict) OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)]) >>> dd = defaultdict(list) - >>> s.to_dict(dd) + >>> s.to_dict(into=dd) defaultdict(, {0: 1, 1: 2, 2: 3, 3: 4}) """ # GH16122 diff --git a/pandas/tests/series/methods/test_to_dict.py b/pandas/tests/series/methods/test_to_dict.py index 4c3d9592eebe3..41c01f4537f23 100644 --- a/pandas/tests/series/methods/test_to_dict.py +++ b/pandas/tests/series/methods/test_to_dict.py @@ -13,12 +13,12 @@ class TestSeriesToDict: ) def test_to_dict(self, mapping, datetime_series): # GH#16122 - result = Series(datetime_series.to_dict(mapping), name="ts") + result = Series(datetime_series.to_dict(into=mapping), name="ts") expected = datetime_series.copy() expected.index = expected.index._with_freq(None) tm.assert_series_equal(result, expected) - from_method = Series(datetime_series.to_dict(collections.Counter)) + from_method = Series(datetime_series.to_dict(into=collections.Counter)) from_constructor = Series(collections.Counter(datetime_series.items())) tm.assert_series_equal(from_method, from_constructor)