Skip to content

TST: Added dataframe constructor tests confirming order is preserved with standard Python dicts #38206

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

Merged
merged 1 commit into from
Dec 2, 2020
Merged
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
14 changes: 8 additions & 6 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,32 +1238,34 @@ def test_constructor_single_row(self):
)
tm.assert_frame_equal(result, expected)

def test_constructor_ordered_dict_preserve_order(self):
@pytest.mark.parametrize("dict_type", [dict, OrderedDict])
def test_constructor_ordered_dict_preserve_order(self, dict_type):
# see gh-13304
expected = DataFrame([[2, 1]], columns=["b", "a"])

data = OrderedDict()
data = dict_type()
data["b"] = [2]
data["a"] = [1]

result = DataFrame(data)
tm.assert_frame_equal(result, expected)

data = OrderedDict()
data = dict_type()
data["b"] = 2
data["a"] = 1

result = DataFrame([data])
tm.assert_frame_equal(result, expected)

def test_constructor_ordered_dict_conflicting_orders(self):
@pytest.mark.parametrize("dict_type", [dict, OrderedDict])
def test_constructor_ordered_dict_conflicting_orders(self, dict_type):
# the first dict element sets the ordering for the DataFrame,
# even if there are conflicting orders from subsequent ones
row_one = OrderedDict()
row_one = dict_type()
row_one["b"] = 2
row_one["a"] = 1

row_two = OrderedDict()
row_two = dict_type()
row_two["a"] = 1
row_two["b"] = 2

Expand Down