Skip to content

Commit d1ab519

Browse files
committed
Add test case for DataFrame.from_dict
1 parent 4bf6ad8 commit d1ab519

File tree

3 files changed

+50
-35
lines changed

3 files changed

+50
-35
lines changed

pandas/tests/frame/common.py

+33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from __future__ import annotations
22

3+
from collections.abc import (
4+
Mapping,
5+
Sequence,
6+
)
37
from typing import TYPE_CHECKING
48

59
from pandas import (
@@ -11,6 +15,35 @@
1115
from pandas._typing import AxisInt
1216

1317

18+
class DictWrapper(Mapping):
19+
_dict: dict
20+
21+
def __init__(self, d: dict) -> None:
22+
self._dict = d
23+
24+
def __getitem__(self, key):
25+
return self._dict[key]
26+
27+
def __iter__(self):
28+
return self._dict.__iter__()
29+
30+
def __len__(self):
31+
return self._dict.__len__()
32+
33+
34+
class ListWrapper(Sequence):
35+
_list: list
36+
37+
def __init__(self, lst: list) -> None:
38+
self._list = lst
39+
40+
def __getitem__(self, i):
41+
return self._list[i]
42+
43+
def __len__(self):
44+
return self._list.__len__()
45+
46+
1447
def _check_mixed_float(df, dtype=None):
1548
# float16 are most likely to be upcasted to float32
1649
dtypes = {"A": "float32", "B": "float32", "C": "float16", "D": "float64"}

pandas/tests/frame/constructors/test_from_dict.py

+11
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
MultiIndex,
1212
RangeIndex,
1313
Series,
14+
date_range,
1415
)
1516
import pandas._testing as tm
17+
from pandas.tests.frame.common import DictWrapper
1618

1719

1820
class TestFromDict:
@@ -135,6 +137,15 @@ def test_constructor_from_ordered_dict(self):
135137
result = DataFrame.from_dict(a, orient="index")
136138
tm.assert_frame_equal(result, expected)
137139

140+
def test_constructor_from_mappinng(self):
141+
idx = Index(date_range("20130101", periods=3, tz="US/Eastern"), name="foo")
142+
dr = date_range("20130110", periods=3)
143+
144+
# construction
145+
expected = DataFrame(DictWrapper({"A": idx, "B": dr}))
146+
result = DataFrame.from_dict(DictWrapper({"A": idx, "B": dr}))
147+
tm.assert_frame_equal(result, expected)
148+
138149
def test_from_dict_columns_parameter(self):
139150
# GH#18529
140151
# Test new columns parameter for from_dict that was added to make

pandas/tests/frame/test_constructors.py

+6-35
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
defaultdict,
66
namedtuple,
77
)
8-
from collections.abc import (
9-
Iterator,
10-
Mapping,
11-
Sequence,
12-
)
8+
from collections.abc import Iterator
139
from dataclasses import make_dataclass
1410
from datetime import (
1511
date,
@@ -65,6 +61,10 @@
6561
SparseArray,
6662
TimedeltaArray,
6763
)
64+
from pandas.tests.frame.common import (
65+
DictWrapper,
66+
ListWrapper,
67+
)
6868

6969
MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"]
7070
MIXED_INT_DTYPES = [
@@ -79,35 +79,6 @@
7979
]
8080

8181

82-
class DictWrapper(Mapping):
83-
_dict: dict
84-
85-
def __init__(self, d: dict) -> None:
86-
self._dict = d
87-
88-
def __getitem__(self, key):
89-
return self._dict[key]
90-
91-
def __iter__(self):
92-
return self._dict.__iter__()
93-
94-
def __len__(self):
95-
return self._dict.__len__()
96-
97-
98-
class ListWrapper(Sequence):
99-
_list: list
100-
101-
def __init__(self, lst: list) -> None:
102-
self._list = lst
103-
104-
def __getitem__(self, i):
105-
return self._list[i]
106-
107-
def __len__(self):
108-
return self._list.__len__()
109-
110-
11182
class TestDataFrameConstructors:
11283
def test_constructor_from_ndarray_with_str_dtype(self):
11384
# If we don't ravel/reshape around ensure_str_array, we end up
@@ -2934,7 +2905,7 @@ def test_from_dict(self):
29342905
tm.assert_series_equal(df["A"], Series(idx, name="A"))
29352906
tm.assert_series_equal(df["B"], Series(dr, name="B"))
29362907

2937-
def test_from_dict_with_mapping(self):
2908+
def test_from_mapping(self):
29382909
idx = Index(date_range("20130101", periods=3, tz="US/Eastern"), name="foo")
29392910
dr = date_range("20130110", periods=3)
29402911

0 commit comments

Comments
 (0)