Skip to content

Commit 0342d90

Browse files
mutricylLaurent Mutricy
and
Laurent Mutricy
authored
896 dataframe from dict columns dtype args (#897)
* tentative solution for #896 * remove Unnecessary "# pyright: ignore" rule error * improving dataframe.from_dict test * check(assert_type every test * remove unwanted ellipsis * Adding overload and test to cover when litteral is not used * Revert "Adding overload and test to cover when litteral is not used" This reverts commit f760452. --------- Co-authored-by: Laurent Mutricy <[email protected]>
1 parent ae7e473 commit 0342d90

File tree

3 files changed

+118
-20
lines changed

3 files changed

+118
-20
lines changed

pandas-stubs/_libs/tslibs/timestamps.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class Timestamp(datetime):
219219
@overload
220220
def __sub__(self, other: TimedeltaSeries) -> TimestampSeries: ...
221221
@overload
222-
def __sub__( # pyright: ignore[reportIncompatibleMethodOverride]
222+
def __sub__(
223223
self, other: npt.NDArray[np.timedelta64]
224224
) -> npt.NDArray[np.datetime64]: ...
225225
@overload

pandas-stubs/core/frame.pyi

+12-3
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,22 @@ class DataFrame(NDFrame, OpsMixin):
272272
def dot(self, other: Series) -> Series: ...
273273
def __matmul__(self, other): ...
274274
def __rmatmul__(self, other): ...
275+
@overload
276+
@classmethod
277+
def from_dict(
278+
cls,
279+
data: dict[Any, Any],
280+
orient: Literal["index"],
281+
dtype: AstypeArg | None = ...,
282+
columns: Axes | None = ...,
283+
) -> DataFrame: ...
284+
@overload
275285
@classmethod
276286
def from_dict(
277287
cls,
278288
data: dict[Any, Any],
279-
orient: Literal["columns", "index", "tight"] = ...,
280-
dtype: _str = ...,
281-
columns: list[_str] = ...,
289+
orient: Literal["columns", "tight"] = ...,
290+
dtype: AstypeArg | None = ...,
282291
) -> DataFrame: ...
283292
def to_numpy(
284293
self,

tests/test_frame.py

+105-16
Original file line numberDiff line numberDiff line change
@@ -1438,25 +1438,114 @@ def test_types_to_dict() -> None:
14381438

14391439

14401440
def test_types_from_dict() -> None:
1441-
pd.DataFrame.from_dict({"col_1": [3, 2, 1, 0], "col_2": ["a", "b", "c", "d"]})
1442-
pd.DataFrame.from_dict({1: [3, 2, 1, 0], 2: ["a", "b", "c", "d"]})
1443-
pd.DataFrame.from_dict({"a": {1: 2}, "b": {3: 4, 1: 4}}, orient="index")
1444-
pd.DataFrame.from_dict({"a": {"row1": 2}, "b": {"row2": 4, "row1": 4}})
1445-
pd.DataFrame.from_dict({"a": (1, 2, 3), "b": (2, 4, 5)})
1446-
pd.DataFrame.from_dict(
1447-
data={"col_1": {"a": 1}, "col_2": {"a": 1, "b": 2}}, orient="columns"
1441+
check(
1442+
assert_type(
1443+
pd.DataFrame.from_dict(
1444+
{"col_1": [3, 2, 1, 0], "col_2": ["a", "b", "c", "d"]}
1445+
),
1446+
pd.DataFrame,
1447+
),
1448+
pd.DataFrame,
1449+
)
1450+
check(
1451+
assert_type(
1452+
pd.DataFrame.from_dict({1: [3, 2, 1, 0], 2: ["a", "b", "c", "d"]}),
1453+
pd.DataFrame,
1454+
),
1455+
pd.DataFrame,
1456+
)
1457+
check(
1458+
assert_type(
1459+
pd.DataFrame.from_dict({"a": {1: 2}, "b": {3: 4, 1: 4}}, orient="index"),
1460+
pd.DataFrame,
1461+
),
1462+
pd.DataFrame,
1463+
)
1464+
check(
1465+
assert_type(
1466+
pd.DataFrame.from_dict({"a": {"row1": 2}, "b": {"row2": 4, "row1": 4}}),
1467+
pd.DataFrame,
1468+
),
1469+
pd.DataFrame,
1470+
)
1471+
check(
1472+
assert_type(
1473+
pd.DataFrame.from_dict({"a": (1, 2, 3), "b": (2, 4, 5)}), pd.DataFrame
1474+
),
1475+
pd.DataFrame,
1476+
)
1477+
check(
1478+
assert_type(
1479+
pd.DataFrame.from_dict(
1480+
data={"col_1": {"a": 1}, "col_2": {"a": 1, "b": 2}}, orient="columns"
1481+
),
1482+
pd.DataFrame,
1483+
),
1484+
pd.DataFrame,
14481485
)
14491486
# orient param accepting "tight" added in 1.4.0 https://pandas.pydata.org/docs/whatsnew/v1.4.0.html
1450-
pd.DataFrame.from_dict(
1451-
data={
1452-
"index": [("a", "b"), ("a", "c")],
1453-
"columns": [("x", 1), ("y", 2)],
1454-
"data": [[1, 3], [2, 4]],
1455-
"index_names": ["n1", "n2"],
1456-
"column_names": ["z1", "z2"],
1457-
},
1458-
orient="tight",
1487+
check(
1488+
assert_type(
1489+
pd.DataFrame.from_dict(
1490+
data={
1491+
"index": [("a", "b"), ("a", "c")],
1492+
"columns": [("x", 1), ("y", 2)],
1493+
"data": [[1, 3], [2, 4]],
1494+
"index_names": ["n1", "n2"],
1495+
"column_names": ["z1", "z2"],
1496+
},
1497+
orient="tight",
1498+
),
1499+
pd.DataFrame,
1500+
),
1501+
pd.DataFrame,
1502+
)
1503+
# added following #896
1504+
data = {"l1": [1, 2, 3], "l2": [4, 5, 6]}
1505+
# testing `dtype`
1506+
check(
1507+
assert_type(
1508+
pd.DataFrame.from_dict(data, orient="index", dtype="float"), pd.DataFrame
1509+
),
1510+
pd.DataFrame,
1511+
)
1512+
check(
1513+
assert_type(
1514+
pd.DataFrame.from_dict(data, orient="index", dtype=float), pd.DataFrame
1515+
),
1516+
pd.DataFrame,
1517+
)
1518+
check(
1519+
assert_type(
1520+
pd.DataFrame.from_dict(data, orient="index", dtype=None), pd.DataFrame
1521+
),
1522+
pd.DataFrame,
14591523
)
1524+
# testing `columns`
1525+
check(
1526+
assert_type(
1527+
pd.DataFrame.from_dict(data, orient="index", columns=["a", "b", "c"]),
1528+
pd.DataFrame,
1529+
),
1530+
pd.DataFrame,
1531+
)
1532+
check(
1533+
assert_type(
1534+
pd.DataFrame.from_dict(
1535+
data, orient="index", columns=[1.0, 2, datetime.datetime.now()]
1536+
),
1537+
pd.DataFrame,
1538+
),
1539+
pd.DataFrame,
1540+
)
1541+
if TYPE_CHECKING_INVALID_USAGE:
1542+
check(
1543+
assert_type( # type: ignore[assert-type]
1544+
pd.DataFrame.from_dict(data, orient="columns", columns=["a", "b", "c"]), # type: ignore[call-overload] # pyright: ignore[reportArgumentType]
1545+
pd.DataFrame,
1546+
),
1547+
pd.DataFrame,
1548+
)
14601549

14611550

14621551
def test_pipe() -> None:

0 commit comments

Comments
 (0)