Skip to content

Commit 313040c

Browse files
committed
Merge remote branch 'neurodebian/master' into neurodebian-master
* neurodebian/master: ENH: assure elements of list are of base dict class while converting into a frame
2 parents 903af18 + dc0ae58 commit 313040c

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pandas/core/frame.py

+5
Original file line numberDiff line numberDiff line change
@@ -4072,6 +4072,11 @@ def _list_of_dict_to_sdict(data, columns, coerce_float=False):
40724072
gen = (x.keys() for x in data)
40734073
columns = lib.fast_unique_multiple_list_gen(gen)
40744074

4075+
# assure that they are of the base dict class and not of derived
4076+
# classes
4077+
data = [(type(d) is dict) and d or dict(d)
4078+
for d in data]
4079+
40754080
content = list(lib.dicts_to_array(data, list(columns)).T)
40764081
return _convert_object_array(content, columns,
40774082
coerce_float=coerce_float)

pandas/tests/test_frame.py

+12
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,18 @@ def test_constructor_list_of_series(self):
15921592
expected = DataFrame.from_dict(sdict, orient='index')
15931593
assert_frame_equal(result, expected)
15941594

1595+
def test_constructor_list_of_derived_dicts(self):
1596+
class CustomDict(dict):
1597+
pass
1598+
d = {'a': 1.5, 'b': 3}
1599+
1600+
data_custom = [CustomDict(d)]
1601+
data = [d]
1602+
1603+
result_custom = DataFrame(data_custom)
1604+
result = DataFrame(data)
1605+
assert_frame_equal(result, result_custom)
1606+
15951607
def test_constructor_ragged(self):
15961608
data = {'A' : randn(10),
15971609
'B' : randn(8)}

0 commit comments

Comments
 (0)