Skip to content

Commit 3929945

Browse files
committed
BUG: from_dict ignored order of OrderedDict (pandas-dev#8425)
1 parent 2d2606d commit 3929945

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ Indexing
633633
- Bug in which :meth:`DataFrame.to_csv` caused a segfault for a reindexed data frame, when the indices were single-level :class:`MultiIndex` (:issue:`26303`).
634634
- Fixed bug where assigning a :class:`arrays.PandasArray` to a :class:`pandas.core.frame.DataFrame` would raise error (:issue:`26390`)
635635
- Allow keyword arguments for callable local reference used in the :meth:`DataFrame.query` string (:issue:`26426`)
636+
- Bug in which :meth:`DataFrame.from_dict` ignored order of OrderedDict when orient='index' (:issue:`8425`).
636637

637638

638639
Missing

pandas/core/internals/construction.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,10 @@ def extract_index(data):
301301
raise ValueError('If using all scalar values, you must pass'
302302
' an index')
303303

304-
if have_series or have_dicts:
304+
if have_series:
305305
index = _union_indexes(indexes)
306+
elif have_dicts:
307+
index = _union_indexes(indexes, sort=False)
306308

307309
if have_raw_arrays:
308310
lengths = list(set(raw_lengths))

pandas/tests/frame/test_constructors.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def test_constructor_subclass_dict(self):
483483
dct.update(v.to_dict())
484484
data[k] = dct
485485
frame = DataFrame(data)
486-
tm.assert_frame_equal(self.frame.sort_index(), frame)
486+
tm.assert_frame_equal(self.frame, frame)
487487

488488
def test_constructor_dict_block(self):
489489
expected = np.array([[4., 3., 2., 1.]])
@@ -1153,7 +1153,7 @@ def test_constructor_list_of_series(self):
11531153

11541154
sdict = OrderedDict(zip(['x', 'Unnamed 0'], data))
11551155
expected = DataFrame.from_dict(sdict, orient='index')
1156-
tm.assert_frame_equal(result.sort_index(), expected)
1156+
tm.assert_frame_equal(result, expected)
11571157

11581158
# none named
11591159
data = [OrderedDict([['a', 1.5], ['b', 3], ['c', 4], ['d', 6]]),
@@ -1288,7 +1288,7 @@ def test_constructor_list_of_namedtuples(self):
12881288
def test_constructor_orient(self):
12891289
data_dict = self.mixed_frame.T._series
12901290
recons = DataFrame.from_dict(data_dict, orient='index')
1291-
expected = self.mixed_frame.sort_index()
1291+
expected = self.mixed_frame.reindex(recons.index)
12921292
tm.assert_frame_equal(recons, expected)
12931293

12941294
# dict of sequence
@@ -1298,6 +1298,16 @@ def test_constructor_orient(self):
12981298
xp = DataFrame.from_dict(a).T.reindex(list(a.keys()))
12991299
tm.assert_frame_equal(rs, xp)
13001300

1301+
def test_constructor_from_ordered_dict(self):
1302+
# GH8425
1303+
a = OrderedDict([
1304+
('one', OrderedDict([('col_a', 'foo1'), ('col_b', 'bar1')])),
1305+
('two', OrderedDict([('col_a', 'foo2'), ('col_b', 'bar2')])),
1306+
('three', OrderedDict([('col_a', 'foo3'), ('col_b', 'bar3')]))])
1307+
expected = DataFrame.from_dict(a, orient='columns').T
1308+
result = DataFrame.from_dict(a, orient='index')
1309+
tm.assert_frame_equal(result, expected)
1310+
13011311
def test_from_dict_columns_parameter(self):
13021312
# GH 18529
13031313
# Test new columns parameter for from_dict that was added to make

0 commit comments

Comments
 (0)