From 434910bcbd9c5c83736eaa13b2268ae03d71cb40 Mon Sep 17 00:00:00 2001 From: Robert Wall Date: Thu, 13 Sep 2018 10:16:11 +0100 Subject: [PATCH 1/2] Only sort dict columns in from_records for py < 3.6 --- pandas/core/frame.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 = [] From ddd8c7fd4c15bb6dc982281166921ab4dd999873 Mon Sep 17 00:00:00 2001 From: Robert Wall Date: Tue, 9 Oct 2018 08:18:48 +0100 Subject: [PATCH 2/2] Test from_records insertion order PY36+ (#22687) --- pandas/tests/frame/test_constructors.py | 9 +++++++++ 1 file changed, 9 insertions(+) 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'])