Skip to content

Commit 9877d2a

Browse files
committed
feat: add type hints and tests for DataFrame.from_records method
1 parent 766cc1d commit 9877d2a

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,15 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
545545
@classmethod
546546
def from_records(
547547
cls,
548-
data,
549-
index=...,
550-
exclude: SequenceNotStr[str] | None = None,
548+
data: (
549+
np.ndarray
550+
| Sequence[Any]
551+
| Iterable[Mapping[str, Any]]
552+
| Mapping[str, Sequence[Any]]
553+
),
554+
index: str | Sequence[str] | None = None,
551555
columns: SequenceNotStr[str] | None = None,
556+
exclude: SequenceNotStr[str] | None = None,
552557
coerce_float: bool = False,
553558
nrows: int | None = None,
554559
) -> Self: ...

tests/test_frame.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4688,3 +4688,101 @@ def test_unstack() -> None:
46884688
),
46894689
pd.DataFrame,
46904690
)
4691+
4692+
4693+
def test_from_records() -> None:
4694+
# testing with list of tuples
4695+
data_tuples = [(1, "a"), (2, "b"), (3, "c")]
4696+
check(
4697+
assert_type(
4698+
pd.DataFrame.from_records(data_tuples, columns=["id", "name"]),
4699+
pd.DataFrame,
4700+
),
4701+
pd.DataFrame,
4702+
)
4703+
4704+
# Testing with numpy structured array
4705+
data_array = np.array(
4706+
[(1, "a"), (2, "b"), (3, "c")],
4707+
dtype=[("id", int), ("name", "U1")],
4708+
)
4709+
check(
4710+
assert_type(pd.DataFrame.from_records(data_array), pd.DataFrame),
4711+
pd.DataFrame,
4712+
)
4713+
4714+
# testing with list of dictionaries
4715+
data_dicts = [{"id": 1, "name": "a"}, {"id": 2, "name": "b"}]
4716+
check(
4717+
assert_type(pd.DataFrame.from_records(data_dicts), pd.DataFrame),
4718+
pd.DataFrame,
4719+
)
4720+
4721+
# Testing with mapping of sequences
4722+
data_mapping = {"id": [1, 2, 3], "name": ["a", "b", "c"]}
4723+
check(
4724+
assert_type(pd.DataFrame.from_records(data_mapping), pd.DataFrame),
4725+
pd.DataFrame,
4726+
)
4727+
4728+
# Testing with index parameter as string
4729+
check(
4730+
assert_type(
4731+
pd.DataFrame.from_records(
4732+
data_tuples, columns=["id", "name"], index="id"
4733+
),
4734+
pd.DataFrame,
4735+
),
4736+
pd.DataFrame,
4737+
)
4738+
4739+
#Testing with index parameter as sequence
4740+
check(
4741+
assert_type(
4742+
pd.DataFrame.from_records(
4743+
data_tuples, columns=["id", "name"], index=["id"]
4744+
),
4745+
pd.DataFrame,
4746+
),
4747+
pd.DataFrame,
4748+
)
4749+
4750+
# Testing with exclude parameter
4751+
check(
4752+
assert_type(
4753+
pd.DataFrame.from_records(
4754+
[(1, "a", "extra"), (2, "b", "extra")],
4755+
columns=["id", "name", "extra"],
4756+
exclude=["extra"],
4757+
),
4758+
pd.DataFrame,
4759+
),
4760+
pd.DataFrame,
4761+
)
4762+
4763+
#Testing with all parameters
4764+
check(
4765+
assert_type(
4766+
pd.DataFrame.from_records(
4767+
data_tuples,
4768+
index=None,
4769+
columns=["id", "name"],
4770+
exclude=None,
4771+
coerce_float=True,
4772+
nrows=2,
4773+
),
4774+
pd.DataFrame,
4775+
),
4776+
pd.DataFrame,
4777+
)
4778+
4779+
# Testing parameter order
4780+
check(
4781+
assert_type(
4782+
pd.DataFrame.from_records(
4783+
data_tuples, columns=["id", "name"], exclude=None
4784+
),
4785+
pd.DataFrame,
4786+
),
4787+
pd.DataFrame,
4788+
)

0 commit comments

Comments
 (0)