Skip to content

Commit 232b4a1

Browse files
fix: fix DataFrame.from_records with data as an iterator (pandas-dev#1376)
* fix: fix DataFrame.from_records with data as an iterator * Remove tuple-based DataFrame test case (duplicate) Removed test case for list of tuples in DataFrame assertions. * Fix ty * PR Feedback * PR Feedback * Pr Feedback --------- Co-authored-by: Loic Diridollou <loic.diridollou@gmail.com>
1 parent 9436296 commit 232b4a1

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,10 +566,10 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
566566
cls,
567567
data: (
568568
np_2darray
569-
| Sequence[SequenceNotStr]
570-
| Sequence[Mapping[str, Any]]
571-
| Mapping[str, Any]
572-
| Mapping[str, SequenceNotStr[Any]]
569+
| Iterable[SequenceNotStr[Any]]
570+
| Iterable[Mapping[HashableT, Any]]
571+
| Mapping[HashableT, Any]
572+
| Mapping[HashableT, SequenceNotStr[Any]]
573573
),
574574
index: str | Axes | None = None,
575575
exclude: ListLike | None = None,

tests/test_frame.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4755,20 +4755,19 @@ def test_from_records() -> None:
47554755
pd.DataFrame,
47564756
)
47574757

4758-
# testing with pd.Index as columns parameter
4758+
# testing with an iterable of tuples
47594759
check(
47604760
assert_type(
4761-
pd.DataFrame.from_records(data_tuples, columns=pd.Index(["id", "name"])),
4761+
pd.DataFrame.from_records(iter(data_tuples), columns=["id", "name"]),
47624762
pd.DataFrame,
47634763
),
47644764
pd.DataFrame,
47654765
)
47664766

4767-
# Testing with list of tuples (instead of structured array for type compatibility)
4768-
data_array_tuples = [(1, "a"), (2, "b")]
4767+
# testing with pd.Index as columns parameter
47694768
check(
47704769
assert_type(
4771-
pd.DataFrame.from_records(data_array_tuples, columns=["id", "name"]),
4770+
pd.DataFrame.from_records(data_tuples, columns=pd.Index(["id", "name"])),
47724771
pd.DataFrame,
47734772
),
47744773
pd.DataFrame,
@@ -4784,6 +4783,15 @@ def test_from_records() -> None:
47844783
pd.DataFrame,
47854784
)
47864785

4786+
# test with an iterable of dictionaries
4787+
check(
4788+
assert_type(
4789+
pd.DataFrame.from_records(iter(data_dict_list), columns=["id", "name"]),
4790+
pd.DataFrame,
4791+
),
4792+
pd.DataFrame,
4793+
)
4794+
47874795
# test with single dictionary
47884796
data_single_dict = {"id": 1, "name": "a"}
47894797
check(

0 commit comments

Comments
 (0)