Skip to content

Commit da80815

Browse files
author
Laurent Mutricy
committed
tentative solution for #896
1 parent ae7e473 commit da80815

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,19 @@ def test_types_from_dict() -> None:
14571457
},
14581458
orient="tight",
14591459
)
1460+
# added following #896
1461+
data = {"l1": [1, 2, 3], "l2": [4, 5, 6]}
1462+
# testing `dtype`
1463+
pd.DataFrame.from_dict(data, orient="index", dtype="float")
1464+
pd.DataFrame.from_dict(data, orient="index", dtype=float)
1465+
pd.DataFrame.from_dict(data, orient="index", dtype=None)
1466+
# testing `columns`
1467+
pd.DataFrame.from_dict(data, orient="index", columns=["a", "b", "c"])
1468+
pd.DataFrame.from_dict(
1469+
data, orient="index", columns=[1.0, 2, datetime.datetime.now()]
1470+
)
1471+
# with pytest.raises(ValueError):
1472+
# pd.DataFrame.from_dict(data, orient='columns', columns=['a', 'b', 'c'])
14601473

14611474

14621475
def test_pipe() -> None:

0 commit comments

Comments
 (0)