diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 251bc6587872d..f1bc84ccc3c83 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1293,7 +1293,8 @@ def from_records(cls, data, index=None, exclude=None, columns=None, if isinstance(data, dict): if columns is None: - columns = arr_columns = ensure_index(sorted(data)) + columns = arr_columns = ensure_index( + com.dict_keys_to_ordered_list(data)) arrays = [data[k] for k in columns] else: arrays = [] diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 6c84beb64e196..580f467872eed 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2104,6 +2104,15 @@ def test_from_records_dictlike(self): for r in results: tm.assert_frame_equal(r, df) + @pytest.mark.skipif(not PY36, reason='Insertion order for Python>=3.6') + def test_from_records_dict_order_insertion(self): + # GH 22687 + # initialization ordering: by insertion order if python>= 3.6 + d = {'b': self.ts2, 'a': self.ts1} + frame = DataFrame.from_records(data=d) + expected = DataFrame(data=d, columns=list('ba')) + tm.assert_frame_equal(frame, expected) + def test_from_records_with_index_data(self): df = DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])