|
| 1 | +from collections import OrderedDict |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | +from pandas import DataFrame, Index, MultiIndex, Series |
| 7 | +import pandas._testing as tm |
| 8 | +from pandas.core.construction import create_series_with_explicit_dtype |
| 9 | + |
| 10 | + |
| 11 | +class TestFromDict: |
| 12 | + # Note: these tests are specific to the from_dict method, not for |
| 13 | + # passing dictionaries to DataFrame.__init__ |
| 14 | + |
| 15 | + def test_from_dict_scalars_requires_index(self): |
| 16 | + msg = "If using all scalar values, you must pass an index" |
| 17 | + with pytest.raises(ValueError, match=msg): |
| 18 | + DataFrame.from_dict(OrderedDict([("b", 8), ("a", 5), ("a", 6)])) |
| 19 | + |
| 20 | + def test_constructor_list_of_odicts(self): |
| 21 | + data = [ |
| 22 | + OrderedDict([["a", 1.5], ["b", 3], ["c", 4], ["d", 6]]), |
| 23 | + OrderedDict([["a", 1.5], ["b", 3], ["d", 6]]), |
| 24 | + OrderedDict([["a", 1.5], ["d", 6]]), |
| 25 | + OrderedDict(), |
| 26 | + OrderedDict([["a", 1.5], ["b", 3], ["c", 4]]), |
| 27 | + OrderedDict([["b", 3], ["c", 4], ["d", 6]]), |
| 28 | + ] |
| 29 | + |
| 30 | + result = DataFrame(data) |
| 31 | + expected = DataFrame.from_dict( |
| 32 | + dict(zip(range(len(data)), data)), orient="index" |
| 33 | + ) |
| 34 | + tm.assert_frame_equal(result, expected.reindex(result.index)) |
| 35 | + |
| 36 | + def test_constructor_single_row(self): |
| 37 | + data = [OrderedDict([["a", 1.5], ["b", 3], ["c", 4], ["d", 6]])] |
| 38 | + |
| 39 | + result = DataFrame(data) |
| 40 | + expected = DataFrame.from_dict(dict(zip([0], data)), orient="index").reindex( |
| 41 | + result.index |
| 42 | + ) |
| 43 | + tm.assert_frame_equal(result, expected) |
| 44 | + |
| 45 | + def test_constructor_list_of_series(self): |
| 46 | + data = [ |
| 47 | + OrderedDict([["a", 1.5], ["b", 3.0], ["c", 4.0]]), |
| 48 | + OrderedDict([["a", 1.5], ["b", 3.0], ["c", 6.0]]), |
| 49 | + ] |
| 50 | + sdict = OrderedDict(zip(["x", "y"], data)) |
| 51 | + idx = Index(["a", "b", "c"]) |
| 52 | + |
| 53 | + # all named |
| 54 | + data2 = [ |
| 55 | + Series([1.5, 3, 4], idx, dtype="O", name="x"), |
| 56 | + Series([1.5, 3, 6], idx, name="y"), |
| 57 | + ] |
| 58 | + result = DataFrame(data2) |
| 59 | + expected = DataFrame.from_dict(sdict, orient="index") |
| 60 | + tm.assert_frame_equal(result, expected) |
| 61 | + |
| 62 | + # some unnamed |
| 63 | + data2 = [ |
| 64 | + Series([1.5, 3, 4], idx, dtype="O", name="x"), |
| 65 | + Series([1.5, 3, 6], idx), |
| 66 | + ] |
| 67 | + result = DataFrame(data2) |
| 68 | + |
| 69 | + sdict = OrderedDict(zip(["x", "Unnamed 0"], data)) |
| 70 | + expected = DataFrame.from_dict(sdict, orient="index") |
| 71 | + tm.assert_frame_equal(result, expected) |
| 72 | + |
| 73 | + # none named |
| 74 | + data = [ |
| 75 | + OrderedDict([["a", 1.5], ["b", 3], ["c", 4], ["d", 6]]), |
| 76 | + OrderedDict([["a", 1.5], ["b", 3], ["d", 6]]), |
| 77 | + OrderedDict([["a", 1.5], ["d", 6]]), |
| 78 | + OrderedDict(), |
| 79 | + OrderedDict([["a", 1.5], ["b", 3], ["c", 4]]), |
| 80 | + OrderedDict([["b", 3], ["c", 4], ["d", 6]]), |
| 81 | + ] |
| 82 | + data = [ |
| 83 | + create_series_with_explicit_dtype(d, dtype_if_empty=object) for d in data |
| 84 | + ] |
| 85 | + |
| 86 | + result = DataFrame(data) |
| 87 | + sdict = OrderedDict(zip(range(len(data)), data)) |
| 88 | + expected = DataFrame.from_dict(sdict, orient="index") |
| 89 | + tm.assert_frame_equal(result, expected.reindex(result.index)) |
| 90 | + |
| 91 | + result2 = DataFrame(data, index=np.arange(6)) |
| 92 | + tm.assert_frame_equal(result, result2) |
| 93 | + |
| 94 | + result = DataFrame([Series(dtype=object)]) |
| 95 | + expected = DataFrame(index=[0]) |
| 96 | + tm.assert_frame_equal(result, expected) |
| 97 | + |
| 98 | + data = [ |
| 99 | + OrderedDict([["a", 1.5], ["b", 3.0], ["c", 4.0]]), |
| 100 | + OrderedDict([["a", 1.5], ["b", 3.0], ["c", 6.0]]), |
| 101 | + ] |
| 102 | + sdict = OrderedDict(zip(range(len(data)), data)) |
| 103 | + |
| 104 | + idx = Index(["a", "b", "c"]) |
| 105 | + data2 = [Series([1.5, 3, 4], idx, dtype="O"), Series([1.5, 3, 6], idx)] |
| 106 | + result = DataFrame(data2) |
| 107 | + expected = DataFrame.from_dict(sdict, orient="index") |
| 108 | + tm.assert_frame_equal(result, expected) |
| 109 | + |
| 110 | + def test_constructor_orient(self, float_string_frame): |
| 111 | + data_dict = float_string_frame.T._series |
| 112 | + recons = DataFrame.from_dict(data_dict, orient="index") |
| 113 | + expected = float_string_frame.reindex(index=recons.index) |
| 114 | + tm.assert_frame_equal(recons, expected) |
| 115 | + |
| 116 | + # dict of sequence |
| 117 | + a = {"hi": [32, 3, 3], "there": [3, 5, 3]} |
| 118 | + rs = DataFrame.from_dict(a, orient="index") |
| 119 | + xp = DataFrame.from_dict(a).T.reindex(list(a.keys())) |
| 120 | + tm.assert_frame_equal(rs, xp) |
| 121 | + |
| 122 | + def test_constructor_from_ordered_dict(self): |
| 123 | + # GH#8425 |
| 124 | + a = OrderedDict( |
| 125 | + [ |
| 126 | + ("one", OrderedDict([("col_a", "foo1"), ("col_b", "bar1")])), |
| 127 | + ("two", OrderedDict([("col_a", "foo2"), ("col_b", "bar2")])), |
| 128 | + ("three", OrderedDict([("col_a", "foo3"), ("col_b", "bar3")])), |
| 129 | + ] |
| 130 | + ) |
| 131 | + expected = DataFrame.from_dict(a, orient="columns").T |
| 132 | + result = DataFrame.from_dict(a, orient="index") |
| 133 | + tm.assert_frame_equal(result, expected) |
| 134 | + |
| 135 | + def test_from_dict_columns_parameter(self): |
| 136 | + # GH#18529 |
| 137 | + # Test new columns parameter for from_dict that was added to make |
| 138 | + # from_items(..., orient='index', columns=[...]) easier to replicate |
| 139 | + result = DataFrame.from_dict( |
| 140 | + OrderedDict([("A", [1, 2]), ("B", [4, 5])]), |
| 141 | + orient="index", |
| 142 | + columns=["one", "two"], |
| 143 | + ) |
| 144 | + expected = DataFrame([[1, 2], [4, 5]], index=["A", "B"], columns=["one", "two"]) |
| 145 | + tm.assert_frame_equal(result, expected) |
| 146 | + |
| 147 | + msg = "cannot use columns parameter with orient='columns'" |
| 148 | + with pytest.raises(ValueError, match=msg): |
| 149 | + DataFrame.from_dict( |
| 150 | + {"A": [1, 2], "B": [4, 5]}, |
| 151 | + orient="columns", |
| 152 | + columns=["one", "two"], |
| 153 | + ) |
| 154 | + with pytest.raises(ValueError, match=msg): |
| 155 | + DataFrame.from_dict({"A": [1, 2], "B": [4, 5]}, columns=["one", "two"]) |
| 156 | + |
| 157 | + @pytest.mark.parametrize( |
| 158 | + "data_dict, keys, orient", |
| 159 | + [ |
| 160 | + ({}, [], "index"), |
| 161 | + ([{("a",): 1}, {("a",): 2}], [("a",)], "columns"), |
| 162 | + ([OrderedDict([(("a",), 1), (("b",), 2)])], [("a",), ("b",)], "columns"), |
| 163 | + ([{("a", "b"): 1}], [("a", "b")], "columns"), |
| 164 | + ], |
| 165 | + ) |
| 166 | + def test_constructor_from_dict_tuples(self, data_dict, keys, orient): |
| 167 | + # GH#16769 |
| 168 | + df = DataFrame.from_dict(data_dict, orient) |
| 169 | + |
| 170 | + result = df.columns |
| 171 | + expected = Index(keys, dtype="object", tupleize_cols=False) |
| 172 | + |
| 173 | + tm.assert_index_equal(result, expected) |
| 174 | + |
| 175 | + def test_frame_dict_constructor_empty_series(self): |
| 176 | + s1 = Series( |
| 177 | + [1, 2, 3, 4], index=MultiIndex.from_tuples([(1, 2), (1, 3), (2, 2), (2, 4)]) |
| 178 | + ) |
| 179 | + s2 = Series( |
| 180 | + [1, 2, 3, 4], index=MultiIndex.from_tuples([(1, 2), (1, 3), (3, 2), (3, 4)]) |
| 181 | + ) |
| 182 | + s3 = Series(dtype=object) |
| 183 | + |
| 184 | + # it works! |
| 185 | + DataFrame({"foo": s1, "bar": s2, "baz": s3}) |
| 186 | + DataFrame.from_dict({"foo": s1, "baz": s3, "bar": s2}) |
0 commit comments