diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 14d8bfea..34ba0547 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -545,10 +545,16 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack): @classmethod def from_records( cls, - data, - index=..., - exclude: SequenceNotStr[str] | None = None, - columns: SequenceNotStr[str] | None = None, + data: ( + np_2darray + | Sequence[SequenceNotStr] + | Sequence[Mapping[str, Any]] + | Mapping[str, Any] + | Mapping[str, SequenceNotStr[Any]] + ), + index: str | SequenceNotStr[Hashable] | None = None, + columns: ListLike | None = None, + exclude: ListLike | None = None, coerce_float: bool = False, nrows: int | None = None, ) -> Self: ... diff --git a/tests/test_frame.py b/tests/test_frame.py index cf1b5801..a212b441 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -4688,3 +4688,125 @@ def test_unstack() -> None: ), pd.DataFrame, ) + + +def test_from_records() -> None: + + # test with np.ndarray + arr = np.array([[1, "a"], [2, "b"]], dtype=object).reshape(2, 2) + check(assert_type(pd.DataFrame.from_records(arr), pd.DataFrame), pd.DataFrame) + + # testing with list of tuples + data_tuples = [(1, "a"), (2, "b"), (3, "c")] + check( + assert_type( + pd.DataFrame.from_records(data_tuples, columns=["id", "name"]), + pd.DataFrame, + ), + pd.DataFrame, + ) + + # testing with pd.Index as columns parameter + check( + assert_type( + pd.DataFrame.from_records(data_tuples, columns=pd.Index(["id", "name"])), + pd.DataFrame, + ), + pd.DataFrame, + ) + + # Testing with list of tuples (instead of structured array for type compatibility) + data_array_tuples = [(1, "a"), (2, "b")] + check( + assert_type( + pd.DataFrame.from_records(data_array_tuples, columns=["id", "name"]), + pd.DataFrame, + ), + pd.DataFrame, + ) + + # testing with list of dictionaries + data_dict_list = [{"id": 1, "name": "a"}, {"id": 2, "name": "b"}] + check( + assert_type( + pd.DataFrame.from_records(data_dict_list, columns=["id", "name"]), + pd.DataFrame, + ), + pd.DataFrame, + ) + + # test with single dictionary + data_single_dict = {"id": 1, "name": "a"} + check( + assert_type( + pd.DataFrame.from_records(data_single_dict, index=["0"]), pd.DataFrame + ), + pd.DataFrame, + ) + + # testing with mapping of sequences + data_mapping_dict = {"id": [1, 2], "name": ["a", "b"]} + check( + assert_type(pd.DataFrame.from_records(data_mapping_dict), pd.DataFrame), + pd.DataFrame, + ) + + # Testing with index parameter as string + check( + assert_type( + pd.DataFrame.from_records(data_tuples, columns=["id", "name"], index="id"), + pd.DataFrame, + ), + pd.DataFrame, + ) + + # Testing with index parameter as sequence + check( + assert_type( + pd.DataFrame.from_records( + data_tuples, columns=["id", "name"], index=["id"] + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + + # Testing with exclude parameter + check( + assert_type( + pd.DataFrame.from_records( + [(1, "a", "extra"), (2, "b", "extra")], + columns=["id", "name", "extra"], + exclude=["extra"], + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + + # Testing with all parameters + check( + assert_type( + pd.DataFrame.from_records( + data_tuples, + index=None, + columns=["id", "name"], + exclude=None, + coerce_float=True, + nrows=2, + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + + # Testing parameter order + check( + assert_type( + pd.DataFrame.from_records( + data_tuples, columns=["id", "name"], exclude=None + ), + pd.DataFrame, + ), + pd.DataFrame, + )