Skip to content

Only sort dict columns in from_records for py < 3.6 #22687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])

Expand Down