Skip to content

Commit e17d5f7

Browse files
committed
fix: enhance DataFrame.from_records type annotations per issue #1334
The main changes include: - Updated data parameter types from overly restrictive Scalar to more flexible Any types - Added .reshape(2, 2) to numpy array test to handle CI compatibility issues across different numpy versions - Included a test for mapping of sequences using DataFrame constructor (which seems to be the right approach for that data type) All 207 DataFrame tests still pass
1 parent e9738a5 commit e17d5f7

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

tests/test_frame.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4691,11 +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=object)
4694+
4695+
# test with np.ndarray
4696+
arr = np.array([[1, "a"], [2, "b"]], dtype=object).reshape(2, 2)
46974697
check(assert_type(pd.DataFrame.from_records(arr), pd.DataFrame), pd.DataFrame)
4698-
4698+
46994699
# testing with list of tuples
47004700
data_tuples = [(1, "a"), (2, "b"), (3, "c")]
47014701
check(
@@ -4718,18 +4718,24 @@ def test_from_records() -> None:
47184718
# Testing with list of tuples (instead of structured array for type compatibility)
47194719
data_array_tuples = [(1, "a"), (2, "b")]
47204720
check(
4721-
assert_type(pd.DataFrame.from_records(data_array_tuples, columns=["id", "name"]), pd.DataFrame),
4721+
assert_type(
4722+
pd.DataFrame.from_records(data_array_tuples, columns=["id", "name"]),
4723+
pd.DataFrame,
4724+
),
47224725
pd.DataFrame,
47234726
)
47244727

47254728
# testing with list of dictionaries (convert to tuples for type compatibility)
47264729
data_dict_tuples = [(1, "a"), (2, "b")]
47274730
check(
4728-
assert_type(pd.DataFrame.from_records(data_dict_tuples, columns=["id", "name"]), pd.DataFrame),
4731+
assert_type(
4732+
pd.DataFrame.from_records(data_dict_tuples, columns=["id", "name"]),
4733+
pd.DataFrame,
4734+
),
47294735
pd.DataFrame,
47304736
)
47314737

4732-
# Testing with mapping of sequences (use DataFrame constructor instead)
4738+
# testing with mapping of sequences
47334739
data_mapping_dict = {"id": [1, 2], "name": ["a", "b"]}
47344740
check(
47354741
assert_type(pd.DataFrame(data_mapping_dict), pd.DataFrame),
@@ -4739,15 +4745,13 @@ def test_from_records() -> None:
47394745
# Testing with index parameter as string
47404746
check(
47414747
assert_type(
4742-
pd.DataFrame.from_records(
4743-
data_tuples, columns=["id", "name"], index="id"
4744-
),
4748+
pd.DataFrame.from_records(data_tuples, columns=["id", "name"], index="id"),
47454749
pd.DataFrame,
47464750
),
47474751
pd.DataFrame,
47484752
)
47494753

4750-
#Testing with index parameter as sequence
4754+
# Testing with index parameter as sequence
47514755
check(
47524756
assert_type(
47534757
pd.DataFrame.from_records(
@@ -4771,7 +4775,7 @@ def test_from_records() -> None:
47714775
pd.DataFrame,
47724776
)
47734777

4774-
#Testing with all parameters
4778+
# Testing with all parameters
47754779
check(
47764780
assert_type(
47774781
pd.DataFrame.from_records(
@@ -4796,4 +4800,4 @@ def test_from_records() -> None:
47964800
pd.DataFrame,
47974801
),
47984802
pd.DataFrame,
4799-
)
4803+
)

0 commit comments

Comments
 (0)