Skip to content

Commit bc7228d

Browse files
committed
feat: add type hints and tests for DataFrame.from_records method- Add np_2darray support to data parameter type annotation- Add comprehensive tests for DataFrame.from_records in test_frame.py- Fix NumPy 2.0 compatibility in test (S1 instead of a1)- Test covers np.ndarray, list of tuples, pd.Index columns, and structured arrays- Addresses GitHub issue #1334
1 parent b381173 commit bc7228d

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
546546
def from_records(
547547
cls,
548548
data: (
549-
Sequence[SequenceNotStr]
549+
np_2darray
550+
| Sequence[SequenceNotStr]
550551
| Sequence[Mapping[str, Scalar]]
551552
| Mapping[str, Sequence[Scalar]]
552553
),

tests/test_frame.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4691,6 +4691,11 @@ def test_unstack() -> None:
46914691

46924692

46934693
def test_from_records() -> None:
4694+
4695+
#test with np.ndarray
4696+
arr = np.array([(1, "a"), (2, "b")], dtype=[("x", "i4"), ("y", "S1")])
4697+
check(assert_type(pd.DataFrame.from_records(arr), pd.DataFrame), pd.DataFrame)
4698+
46944699
# testing with list of tuples
46954700
data_tuples = [(1, "a"), (2, "b"), (3, "c")]
46964701
check(

0 commit comments

Comments
 (0)