Skip to content

Commit 086c598

Browse files
committed
ENH: Provide dict object for to_dict() pandas-dev#16122
1 parent 546816b commit 086c598

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

pandas/core/common.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,14 @@ def _dict_compat(d):
484484
def standardize_mapping(into):
485485
"""
486486
Helper function to standardize a supplied mapping.
487+
487488
.. versionadded:: 0.21.0
488489
489490
Parameters
490491
----------
491492
into : instance or subclass of collections.Mapping
492493
Must be a class, an initialized collections.defaultdict,
493-
or an empty instance of a collections.Mapping subclass.
494+
or an instance of a collections.Mapping subclass.
494495
495496
Returns
496497
-------

pandas/core/frame.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -887,8 +887,7 @@ def to_dict(self, orient='dict', into=dict):
887887
The collections.Mapping subclass used for all Mappings
888888
in the return value. Can be the actual class or an empty
889889
instance of the mapping type you want. If you want a
890-
collections.defaultdict, you must pass an initialized
891-
instance.
890+
collections.defaultdict, you must pass it initialized.
892891
893892
.. versionadded:: 0.21.0
894893

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ def to_dict(self, into=dict):
10841084
The collections.Mapping subclass to use as the return
10851085
object. Can be the actual class or an empty
10861086
instance of the mapping type you want. If you want a
1087-
collections.defaultdict, you must pass an initialized
1087+
collections.defaultdict, you must pass it initialized.
10881088
10891089
.. versionadded:: 0.21.0
10901090

pandas/tests/frame/test_convert_to.py

+10
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,16 @@ def test_to_dict(self, mapping):
206206
for k2, v2 in compat.iteritems(v):
207207
assert (v2 == recons_data[k2][k])
208208

209+
@pytest.mark.parametrize('mapping', [
210+
list,
211+
collections.defaultdict,
212+
[]])
213+
def test_to_dict_errors(self, mapping):
214+
# GH16122
215+
df = DataFrame(np.random.randn(3, 3))
216+
with pytest.raises(TypeError):
217+
df.to_dict(into=mapping)
218+
209219
@pytest.mark.parametrize('tz', ['UTC', 'GMT', 'US/Eastern'])
210220
def test_to_records_datetimeindex_with_tz(self, tz):
211221
# GH13937

0 commit comments

Comments
 (0)