diff --git a/pandas-stubs/_libs/tslibs/timestamps.pyi b/pandas-stubs/_libs/tslibs/timestamps.pyi index af856ae6f..9df14b3d4 100644 --- a/pandas-stubs/_libs/tslibs/timestamps.pyi +++ b/pandas-stubs/_libs/tslibs/timestamps.pyi @@ -219,7 +219,7 @@ class Timestamp(datetime): @overload def __sub__(self, other: TimedeltaSeries) -> TimestampSeries: ... @overload - def __sub__( # pyright: ignore[reportIncompatibleMethodOverride] + def __sub__( self, other: npt.NDArray[np.timedelta64] ) -> npt.NDArray[np.datetime64]: ... @overload diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 7ce2f5d61..44b3a080e 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -272,13 +272,22 @@ class DataFrame(NDFrame, OpsMixin): def dot(self, other: Series) -> Series: ... def __matmul__(self, other): ... def __rmatmul__(self, other): ... + @overload + @classmethod + def from_dict( + cls, + data: dict[Any, Any], + orient: Literal["index"], + dtype: AstypeArg | None = ..., + columns: Axes | None = ..., + ) -> DataFrame: ... + @overload @classmethod def from_dict( cls, data: dict[Any, Any], - orient: Literal["columns", "index", "tight"] = ..., - dtype: _str = ..., - columns: list[_str] = ..., + orient: Literal["columns", "tight"] = ..., + dtype: AstypeArg | None = ..., ) -> DataFrame: ... def to_numpy( self, diff --git a/tests/test_frame.py b/tests/test_frame.py index af3183b93..099d608b1 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -1438,25 +1438,114 @@ def test_types_to_dict() -> None: def test_types_from_dict() -> None: - pd.DataFrame.from_dict({"col_1": [3, 2, 1, 0], "col_2": ["a", "b", "c", "d"]}) - pd.DataFrame.from_dict({1: [3, 2, 1, 0], 2: ["a", "b", "c", "d"]}) - pd.DataFrame.from_dict({"a": {1: 2}, "b": {3: 4, 1: 4}}, orient="index") - pd.DataFrame.from_dict({"a": {"row1": 2}, "b": {"row2": 4, "row1": 4}}) - pd.DataFrame.from_dict({"a": (1, 2, 3), "b": (2, 4, 5)}) - pd.DataFrame.from_dict( - data={"col_1": {"a": 1}, "col_2": {"a": 1, "b": 2}}, orient="columns" + check( + assert_type( + pd.DataFrame.from_dict( + {"col_1": [3, 2, 1, 0], "col_2": ["a", "b", "c", "d"]} + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.DataFrame.from_dict({1: [3, 2, 1, 0], 2: ["a", "b", "c", "d"]}), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.DataFrame.from_dict({"a": {1: 2}, "b": {3: 4, 1: 4}}, orient="index"), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.DataFrame.from_dict({"a": {"row1": 2}, "b": {"row2": 4, "row1": 4}}), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.DataFrame.from_dict({"a": (1, 2, 3), "b": (2, 4, 5)}), pd.DataFrame + ), + pd.DataFrame, + ) + check( + assert_type( + pd.DataFrame.from_dict( + data={"col_1": {"a": 1}, "col_2": {"a": 1, "b": 2}}, orient="columns" + ), + pd.DataFrame, + ), + pd.DataFrame, ) # orient param accepting "tight" added in 1.4.0 https://pandas.pydata.org/docs/whatsnew/v1.4.0.html - pd.DataFrame.from_dict( - data={ - "index": [("a", "b"), ("a", "c")], - "columns": [("x", 1), ("y", 2)], - "data": [[1, 3], [2, 4]], - "index_names": ["n1", "n2"], - "column_names": ["z1", "z2"], - }, - orient="tight", + check( + assert_type( + pd.DataFrame.from_dict( + data={ + "index": [("a", "b"), ("a", "c")], + "columns": [("x", 1), ("y", 2)], + "data": [[1, 3], [2, 4]], + "index_names": ["n1", "n2"], + "column_names": ["z1", "z2"], + }, + orient="tight", + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + # added following #896 + data = {"l1": [1, 2, 3], "l2": [4, 5, 6]} + # testing `dtype` + check( + assert_type( + pd.DataFrame.from_dict(data, orient="index", dtype="float"), pd.DataFrame + ), + pd.DataFrame, + ) + check( + assert_type( + pd.DataFrame.from_dict(data, orient="index", dtype=float), pd.DataFrame + ), + pd.DataFrame, + ) + check( + assert_type( + pd.DataFrame.from_dict(data, orient="index", dtype=None), pd.DataFrame + ), + pd.DataFrame, ) + # testing `columns` + check( + assert_type( + pd.DataFrame.from_dict(data, orient="index", columns=["a", "b", "c"]), + pd.DataFrame, + ), + pd.DataFrame, + ) + check( + assert_type( + pd.DataFrame.from_dict( + data, orient="index", columns=[1.0, 2, datetime.datetime.now()] + ), + pd.DataFrame, + ), + pd.DataFrame, + ) + if TYPE_CHECKING_INVALID_USAGE: + check( + assert_type( # type: ignore[assert-type] + pd.DataFrame.from_dict(data, orient="columns", columns=["a", "b", "c"]), # type: ignore[call-overload] # pyright: ignore[reportArgumentType] + pd.DataFrame, + ), + pd.DataFrame, + ) def test_pipe() -> None: