-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
ENH: Respect Key Ordering for OrderedDict List in DataFrame Init #13309
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -891,6 +891,45 @@ def test_constructor_list_of_dicts(self): | |
| expected = DataFrame(index=[0]) | ||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| def test_constructor_ordered_dict_preserve_order(self): | ||
| # see gh-13304 | ||
| expected = DataFrame([[2, 1]], columns=['b', 'a']) | ||
|
|
||
| data = OrderedDict() | ||
| data['b'] = [2] | ||
| data['a'] = [1] | ||
|
|
||
| result = DataFrame(data) | ||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| data = OrderedDict() | ||
| data['b'] = 2 | ||
| data['a'] = 1 | ||
|
|
||
| result = DataFrame([data]) | ||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| def test_constructor_ordered_dict_conflicting_orders(self): | ||
| # the first dict element sets the ordering for the DataFrame, | ||
| # even if there are conflicting orders from subsequent ones | ||
| row_one = OrderedDict() | ||
| row_one['b'] = 2 | ||
| row_one['a'] = 1 | ||
|
|
||
| row_two = OrderedDict() | ||
| row_two['a'] = 1 | ||
| row_two['b'] = 2 | ||
|
|
||
| row_three = {'b': 2, 'a': 1} | ||
|
|
||
| expected = DataFrame([[2, 1], [2, 1]], columns=['b', 'a']) | ||
|
||
| result = DataFrame([row_one, row_two]) | ||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| expected = DataFrame([[2, 1], [2, 1], [2, 1]], columns=['b', 'a']) | ||
| result = DataFrame([row_one, row_two, row_three]) | ||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| def test_constructor_list_of_series(self): | ||
| data = [OrderedDict([['a', 1.5], ['b', 3.0], ['c', 4.0]]), | ||
| OrderedDict([['a', 1.5], ['b', 3.0], ['c', 6.0]])] | ||
|
|
@@ -1870,3 +1909,9 @@ def test_from_index(self): | |
| tm.assert_series_equal(df2[0], Series(idx2, name=0)) | ||
| df2 = DataFrame(Series(idx2)) | ||
| tm.assert_series_equal(df2[0], Series(idx2, name=0)) | ||
|
|
||
| if __name__ == '__main__': | ||
| import nose # noqa | ||
|
|
||
| nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], | ||
| exit=False) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like this, but for example have a dict and OrderedDict in a list