Skip to content

Commit

Permalink
Add test case for creation from records
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkn committed May 23, 2024
1 parent bf051f8 commit 097b0d2
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from collections.abc import (
Iterator,
Mapping,
Sequence,
)
from dataclasses import make_dataclass
from datetime import (
Expand Down Expand Up @@ -94,6 +95,19 @@ 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 @@ -2931,6 +2945,36 @@ def test_from_dict_with_mapping(self):
tm.assert_series_equal(df["A"], Series(idx, name="A"))
tm.assert_series_equal(df["B"], Series(dr, name="B"))

def test_from_mappiog_list(self):
idx = Index(date_range("20130101", periods=3, tz="US/Eastern"), name="foo")
dr = date_range("20130110", periods=3)
data = DataFrame({"A": idx, "B": dr})
mapping_list = [
DictWrapper(record) for record in data.to_dict(orient="records")
]

# construction
df = DataFrame(mapping_list)
assert df["A"].dtype, "M8[ns, US/Eastern"
assert df["A"].name == "A"
tm.assert_series_equal(df["A"], Series(idx, name="A"))
tm.assert_series_equal(df["B"], Series(dr, name="B"))

def test_from_mappiog_sequence(self):
idx = Index(date_range("20130101", periods=3, tz="US/Eastern"), name="foo")
dr = date_range("20130110", periods=3)
data = DataFrame({"A": idx, "B": dr})
mapping_list = ListWrapper(
[DictWrapper(record) for record in data.to_dict(orient="records")]
)

# construction
df = DataFrame(mapping_list)
assert df["A"].dtype, "M8[ns, US/Eastern"
assert df["A"].name == "A"
tm.assert_series_equal(df["A"], Series(idx, name="A"))
tm.assert_series_equal(df["B"], Series(dr, name="B"))

def test_from_index(self):
# from index
idx2 = date_range("20130101", periods=3, tz="US/Eastern", name="foo")
Expand Down

0 comments on commit 097b0d2

Please sign in to comment.