Skip to content

Ensure that DataFrame constructed from dict follows insertion order #25915

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
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ Sparse
Other
^^^^^

-
- Ensured that ``DataFrame`` constructed from a ``dict`` with no index passed will follow the ``dict``'s insertion order (:issue:`25911`)
-
-

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def extract_index(data):
' an index')

if have_series or have_dicts:
index = _union_indexes(indexes)
index = _union_indexes(indexes, sort=False)

if have_raw_arrays:
lengths = list(set(raw_lengths))
Expand Down
17 changes: 14 additions & 3 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ def test_constructor_dict(self):
with pytest.raises(ValueError, match=msg):
DataFrame({'a': 0.7}, columns=['a'])

def test_constructor_dict_order(self):
data = {'B': {'b': 1, 'c': 2, 'a': 3},
'A': {'b': 3, 'c': 2, 'a': 1}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the order isn't the same across all dicts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would keep the order of the first dict. For example if the first dict is ordered b, c, a like in the above example and the second dict is ordered c, b, a - the resulting DataFrame would maintain the order b, c, a.

result = pd.DataFrame(data)

expected = pd.DataFrame([[1, 3], [2, 2], [3, 1]],
index=['b', 'c', 'a'],
columns=['B', 'A'])

tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("scalar", [2, np.nan, None, 'D'])
def test_constructor_invalid_items_unused(self, scalar):
# No error if invalid (scalar) value is in fact not used:
Expand Down Expand Up @@ -479,7 +490,7 @@ def test_constructor_subclass_dict(self):
dct.update(v.to_dict())
data[k] = dct
frame = DataFrame(data)
tm.assert_frame_equal(self.frame.sort_index(), frame)
tm.assert_frame_equal(self.frame, frame)

def test_constructor_dict_block(self):
expected = np.array([[4., 3., 2., 1.]])
Expand Down Expand Up @@ -1110,7 +1121,7 @@ def test_constructor_list_of_series(self):

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

# none named
data = [OrderedDict([['a', 1.5], ['b', 3], ['c', 4], ['d', 6]]),
Expand Down Expand Up @@ -1231,7 +1242,7 @@ def test_constructor_namedtuples(self):
def test_constructor_orient(self):
data_dict = self.mixed_frame.T._series
recons = DataFrame.from_dict(data_dict, orient='index')
expected = self.mixed_frame.sort_index()
expected = self.mixed_frame
tm.assert_frame_equal(recons, expected)

# dict of sequence
Expand Down