From d1ab519d62a1cff976daea5364b44e7fcd450371 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Sun, 26 May 2024 23:54:34 +0900 Subject: [PATCH] Add test case for DataFrame.from_dict --- pandas/tests/frame/common.py | 33 +++++++++++++++ .../frame/constructors/test_from_dict.py | 11 +++++ pandas/tests/frame/test_constructors.py | 41 +++---------------- 3 files changed, 50 insertions(+), 35 deletions(-) diff --git a/pandas/tests/frame/common.py b/pandas/tests/frame/common.py index fc41d7907a240f..361e426e1ef42a 100644 --- a/pandas/tests/frame/common.py +++ b/pandas/tests/frame/common.py @@ -1,5 +1,9 @@ from __future__ import annotations +from collections.abc import ( + Mapping, + Sequence, +) from typing import TYPE_CHECKING from pandas import ( @@ -11,6 +15,35 @@ from pandas._typing import AxisInt +class DictWrapper(Mapping): + _dict: dict + + def __init__(self, d: dict) -> None: + self._dict = d + + def __getitem__(self, key): + return self._dict[key] + + def __iter__(self): + return self._dict.__iter__() + + def __len__(self): + return self._dict.__len__() + + +class ListWrapper(Sequence): + _list: list + + def __init__(self, lst: list) -> None: + self._list = lst + + def __getitem__(self, i): + return self._list[i] + + def __len__(self): + return self._list.__len__() + + def _check_mixed_float(df, dtype=None): # float16 are most likely to be upcasted to float32 dtypes = {"A": "float32", "B": "float32", "C": "float16", "D": "float64"} diff --git a/pandas/tests/frame/constructors/test_from_dict.py b/pandas/tests/frame/constructors/test_from_dict.py index 60a8e688b3b8ad..5a6759b449fb35 100644 --- a/pandas/tests/frame/constructors/test_from_dict.py +++ b/pandas/tests/frame/constructors/test_from_dict.py @@ -11,8 +11,10 @@ MultiIndex, RangeIndex, Series, + date_range, ) import pandas._testing as tm +from pandas.tests.frame.common import DictWrapper class TestFromDict: @@ -135,6 +137,15 @@ def test_constructor_from_ordered_dict(self): result = DataFrame.from_dict(a, orient="index") tm.assert_frame_equal(result, expected) + def test_constructor_from_mappinng(self): + idx = Index(date_range("20130101", periods=3, tz="US/Eastern"), name="foo") + dr = date_range("20130110", periods=3) + + # construction + expected = DataFrame(DictWrapper({"A": idx, "B": dr})) + result = DataFrame.from_dict(DictWrapper({"A": idx, "B": dr})) + tm.assert_frame_equal(result, expected) + def test_from_dict_columns_parameter(self): # GH#18529 # Test new columns parameter for from_dict that was added to make diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index f856352fdd3292..b00554cd4f0019 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -5,11 +5,7 @@ defaultdict, namedtuple, ) -from collections.abc import ( - Iterator, - Mapping, - Sequence, -) +from collections.abc import Iterator from dataclasses import make_dataclass from datetime import ( date, @@ -65,6 +61,10 @@ SparseArray, TimedeltaArray, ) +from pandas.tests.frame.common import ( + DictWrapper, + ListWrapper, +) MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"] MIXED_INT_DTYPES = [ @@ -79,35 +79,6 @@ ] -class DictWrapper(Mapping): - _dict: dict - - def __init__(self, d: dict) -> None: - self._dict = d - - def __getitem__(self, key): - return self._dict[key] - - def __iter__(self): - return self._dict.__iter__() - - def __len__(self): - return self._dict.__len__() - - -class ListWrapper(Sequence): - _list: list - - def __init__(self, lst: list) -> None: - self._list = lst - - def __getitem__(self, i): - return self._list[i] - - def __len__(self): - return self._list.__len__() - - class TestDataFrameConstructors: def test_constructor_from_ndarray_with_str_dtype(self): # If we don't ravel/reshape around ensure_str_array, we end up @@ -2934,7 +2905,7 @@ def test_from_dict(self): tm.assert_series_equal(df["A"], Series(idx, name="A")) tm.assert_series_equal(df["B"], Series(dr, name="B")) - def test_from_dict_with_mapping(self): + def test_from_mapping(self): idx = Index(date_range("20130101", periods=3, tz="US/Eastern"), name="foo") dr = date_range("20130110", periods=3)