Skip to content

Commit

Permalink
Add test case for DataFrame.from_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkn committed May 26, 2024
1 parent 4bf6ad8 commit d1ab519
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
33 changes: 33 additions & 0 deletions pandas/tests/frame/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from __future__ import annotations

from collections.abc import (
Mapping,
Sequence,
)
from typing import TYPE_CHECKING

from pandas import (
Expand All @@ -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"}
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/frame/constructors/test_from_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
MultiIndex,
RangeIndex,
Series,
date_range,
)
import pandas._testing as tm
from pandas.tests.frame.common import DictWrapper


class TestFromDict:
Expand Down Expand Up @@ -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
Expand Down
41 changes: 6 additions & 35 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -65,6 +61,10 @@
SparseArray,
TimedeltaArray,
)
from pandas.tests.frame.common import (
DictWrapper,
ListWrapper,
)

MIXED_FLOAT_DTYPES = ["float16", "float32", "float64"]
MIXED_INT_DTYPES = [
Expand All @@ -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
Expand Down Expand 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)

Expand Down

0 comments on commit d1ab519

Please sign in to comment.