Skip to content

Commit

Permalink
BUG: df.from_dict should respect OrderedDict 2517
Browse files Browse the repository at this point in the history
  • Loading branch information
y-p authored and wesm committed Dec 13, 2012
1 parent 8f784f1 commit 8553fcc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
7 changes: 4 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from pandas.core.internals import BlockManager, make_block, form_blocks
from pandas.core.series import Series, _radd_compat, _dtype_from_scalar
from pandas.compat.scipy import scoreatpercentile as _quantile
from pandas.util.compat import OrderedDict
from pandas.util import py3compat
from pandas.util.terminal import get_terminal_size
from pandas.util.decorators import deprecate, Appender, Substitution
Expand Down Expand Up @@ -468,7 +469,6 @@ def _init_dict(self, data, index, columns, dtype=None):
Segregate Series based on type and coerce into matrices.
Needs to handle a lot of exceptional cases.
"""
from pandas.util.compat import OrderedDict
if dtype is not None:
dtype = np.dtype(dtype)

Expand Down Expand Up @@ -883,14 +883,15 @@ def from_dict(cls, data, orient='columns', dtype=None):
-------
DataFrame
"""
from collections import defaultdict
from collections import OrderedDict

orient = orient.lower()
if orient == 'index':
# TODO: this should be seriously cythonized
new_data = defaultdict(dict)
new_data = OrderedDict()
for index, s in data.iteritems():
for col, v in s.iteritems():
new_data[col]= new_data.get(col,OrderedDict())
new_data[col][index] = v
data = new_data
elif orient != 'columns': # pragma: no cover
Expand Down
43 changes: 22 additions & 21 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
assert_series_equal,
assert_frame_equal)
from pandas.util import py3compat
from pandas.util.compat import OrderedDict

import pandas.util.testing as tm
import pandas.lib as lib
Expand Down Expand Up @@ -1799,7 +1800,6 @@ def test_is_mixed_type(self):
self.assert_(self.mixed_frame._is_mixed_type)

def test_constructor_ordereddict(self):
from pandas.util.compat import OrderedDict
import random
nitems = 100
nums = range(nitems)
Expand Down Expand Up @@ -2196,12 +2196,12 @@ def test_constructor_list_of_lists(self):
self.assert_(df['str'].dtype == np.object_)

def test_constructor_list_of_dicts(self):
data = [{'a': 1.5, 'b': 3, 'c':4, 'd':6},
{'a': 1.5, 'b': 3, 'd':6},
{'a': 1.5, 'd':6},
{},
{'a': 1.5, 'b': 3, 'c':4},
{'b': 3, 'c':4, 'd':6}]
data = [OrderedDict([['a', 1.5], ['b', 3], ['c',4], ['d',6]]),
OrderedDict([['a', 1.5], ['b', 3], ['d',6]]),
OrderedDict([['a', 1.5],[ 'd',6]]),
OrderedDict(),
OrderedDict([['a', 1.5], ['b', 3],[ 'c',4]]),
OrderedDict([['b', 3], ['c',4], ['d',6]])]

result = DataFrame(data)
expected = DataFrame.from_dict(dict(zip(range(len(data)), data)),
Expand All @@ -2213,9 +2213,9 @@ def test_constructor_list_of_dicts(self):
assert_frame_equal(result, expected)

def test_constructor_list_of_series(self):
data = [{'a': 1.5, 'b': 3.0, 'c':4.0},
{'a': 1.5, 'b': 3.0, 'c':6.0}]
sdict = dict(zip(['x', 'y'], data))
data = [OrderedDict([['a', 1.5],[ 'b', 3.0],[ 'c',4.0]]),
OrderedDict([['a', 1.5], ['b', 3.0],[ 'c',6.0]])]
sdict = OrderedDict(zip(['x', 'y'], data))
idx = Index(['a', 'b', 'c'])

# all named
Expand All @@ -2230,21 +2230,21 @@ def test_constructor_list_of_series(self):
Series([1.5, 3, 6], idx)]
result = DataFrame(data2)

sdict = dict(zip(['x', 'Unnamed 0'], data))
sdict = OrderedDict(zip(['x', 'Unnamed 0'], data))
expected = DataFrame.from_dict(sdict, orient='index')
assert_frame_equal(result.sort_index(), expected)

# none named
data = [{'a': 1.5, 'b': 3, 'c':4, 'd':6},
{'a': 1.5, 'b': 3, 'd':6},
{'a': 1.5, 'd':6},
{},
{'a': 1.5, 'b': 3, 'c':4},
{'b': 3, 'c':4, 'd':6}]
data = [OrderedDict([['a', 1.5], ['b', 3], ['c',4], ['d',6]]),
OrderedDict([['a', 1.5], ['b', 3], ['d',6]]),
OrderedDict([['a', 1.5],[ 'd',6]]),
OrderedDict(),
OrderedDict([['a', 1.5], ['b', 3],[ 'c',4]]),
OrderedDict([['b', 3], ['c',4], ['d',6]])]
data = [Series(d) for d in data]

result = DataFrame(data)
sdict = dict(zip(range(len(data)), data))
sdict = OrderedDict(zip(range(len(data)), data))
expected = DataFrame.from_dict(sdict, orient='index')
assert_frame_equal(result, expected.reindex(result.index))

Expand All @@ -2255,9 +2255,10 @@ def test_constructor_list_of_series(self):
expected = DataFrame(index=[0])
assert_frame_equal(result, expected)

data = [{'a': 1.5, 'b': 3.0, 'c':4.0},
{'a': 1.5, 'b': 3.0, 'c':6.0}]
sdict = dict(zip(range(len(data)), data))
data = [OrderedDict([['a', 1.5],[ 'b', 3.0],[ 'c',4.0]]),
OrderedDict([['a', 1.5], ['b', 3.0],[ 'c',6.0]])]
sdict = OrderedDict(zip(range(len(data)), data))

idx = Index(['a', 'b', 'c'])
data2 = [Series([1.5, 3, 4], idx, dtype='O'),
Series([1.5, 3, 6], idx)]
Expand Down

0 comments on commit 8553fcc

Please sign in to comment.