Skip to content

Commit 0b201dc

Browse files
committed
ENH: add DataFrame.from_dict with orient option
1 parent 43d895c commit 0b201dc

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pandas 0.5.1
4949
- Add matrix multiplication function `dot` to DataFrame (GH #65)
5050
- Add `orient` option to `Panel.from_dict` to ease creation of mixed-type
5151
Panels (GH #359)
52+
- Add `DataFrame.from_dict` with similar `orient` option
5253
5354
**Improvements to existing features**
5455

pandas/core/frame.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,44 @@ def dot(self, other):
397397
#----------------------------------------------------------------------
398398
# IO methods (to / from other formats)
399399

400+
@classmethod
401+
def from_dict(cls, data, orient='columns', dtype=None):
402+
"""
403+
Construct Panel from dict of DataFrame objects
404+
405+
Parameters
406+
----------
407+
data : dict
408+
{field : DataFrame}
409+
intersect : boolean
410+
Intersect indexes of input DataFrames
411+
orient : {'columns', 'index'}, default 'items'
412+
The "orientation" of the data. If the keys of the passed dict
413+
should be the items of the result panel, pass 'items'
414+
(default). Otherwise if the columns of the values of the passed
415+
DataFrame objects should be the items (which in the case of
416+
mixed-dtype data you should do), instead pass 'minor'
417+
418+
419+
Returns
420+
-------
421+
Panel
422+
"""
423+
from collections import defaultdict
424+
425+
orient = orient.lower()
426+
if orient == 'index':
427+
# TODO: this should be seriously cythonized
428+
new_data = defaultdict(dict)
429+
for index, s in data.iteritems():
430+
for col, v in s.iteritems():
431+
new_data[col][index] = v
432+
data = new_data
433+
elif orient != 'columns': # pragma: no cover
434+
raise ValueError('only recognize index or columns for orient')
435+
436+
return DataFrame(data, dtype=dtype)
437+
400438
def to_dict(self):
401439
"""
402440
Convert DataFrame to nested dictionary

pandas/core/panel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def shape(self):
269269
return len(self.items), len(self.major_axis), len(self.minor_axis)
270270

271271
@classmethod
272-
def from_dict(cls, data, intersect=False, orient='items', dtype=float):
272+
def from_dict(cls, data, intersect=False, orient='items', dtype=None):
273273
"""
274274
Construct Panel from dict of DataFrame objects
275275

pandas/tests/test_frame.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,12 @@ def test_constructor_tuples(self):
11241124
expected = DataFrame({'A': Series([(1, 2), (3, 4)])})
11251125
assert_frame_equal(result, expected)
11261126

1127+
def test_constructor_orient(self):
1128+
data_dict = self.mixed_frame.T._series
1129+
recons = DataFrame.from_dict(data_dict, orient='index')
1130+
expected = self.mixed_frame.sort_index()
1131+
assert_frame_equal(recons, expected)
1132+
11271133
def test_astype(self):
11281134
casted = self.frame.astype(int)
11291135
expected = DataFrame(self.frame.values.astype(int),

0 commit comments

Comments
 (0)