|
5 | 5 | defaultdict,
|
6 | 6 | namedtuple,
|
7 | 7 | )
|
8 |
| -from collections.abc import Iterator |
| 8 | +from collections.abc import ( |
| 9 | + Iterator, |
| 10 | + Mapping, |
| 11 | +) |
9 | 12 | from dataclasses import make_dataclass
|
10 | 13 | from datetime import (
|
11 | 14 | date,
|
|
75 | 78 | ]
|
76 | 79 |
|
77 | 80 |
|
| 81 | +class DictWrapper(Mapping): |
| 82 | + _dict: dict |
| 83 | + |
| 84 | + def __init__(self, d: dict) -> None: |
| 85 | + self._dict = d |
| 86 | + |
| 87 | + def __getitem__(self, key): |
| 88 | + return self._dict[key] |
| 89 | + |
| 90 | + def __iter__(self): |
| 91 | + return self._dict.__iter__() |
| 92 | + |
| 93 | + def __len__(self): |
| 94 | + return self._dict.__len__() |
| 95 | + |
| 96 | + |
78 | 97 | class TestDataFrameConstructors:
|
79 | 98 | def test_constructor_from_ndarray_with_str_dtype(self):
|
80 | 99 | # If we don't ravel/reshape around ensure_str_array, we end up
|
@@ -2901,6 +2920,17 @@ def test_from_dict(self):
|
2901 | 2920 | tm.assert_series_equal(df["A"], Series(idx, name="A"))
|
2902 | 2921 | tm.assert_series_equal(df["B"], Series(dr, name="B"))
|
2903 | 2922 |
|
| 2923 | + def test_from_dict_with_mapping(self): |
| 2924 | + idx = Index(date_range("20130101", periods=3, tz="US/Eastern"), name="foo") |
| 2925 | + dr = date_range("20130110", periods=3) |
| 2926 | + |
| 2927 | + # construction |
| 2928 | + df = DataFrame(DictWrapper({"A": idx, "B": dr})) |
| 2929 | + assert df["A"].dtype, "M8[ns, US/Eastern" |
| 2930 | + assert df["A"].name == "A" |
| 2931 | + tm.assert_series_equal(df["A"], Series(idx, name="A")) |
| 2932 | + tm.assert_series_equal(df["B"], Series(dr, name="B")) |
| 2933 | + |
2904 | 2934 | def test_from_index(self):
|
2905 | 2935 | # from index
|
2906 | 2936 | idx2 = date_range("20130101", periods=3, tz="US/Eastern", name="foo")
|
|
0 commit comments