From 0667dfed94751f140a062756e53c6a95603cc8f5 Mon Sep 17 00:00:00 2001 From: Kian S <34144932+KianShah@users.noreply.github.com> Date: Thu, 23 Jun 2022 01:38:24 -0700 Subject: [PATCH 1/5] Update frame.pyi --- pandas-stubs/core/frame.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 7dd54e7b5..958ebabb9 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -215,7 +215,7 @@ class DataFrame(NDFrame, OpsMixin): @overload def to_dict( self, - orient: Literal["dict", "list", "series", "split", "index"] = ..., + orient: Literal["dict", "list", "series", "split", "tight", "index"] = ..., into: Hashable = ..., ) -> Dict[_str, Any]: ... def to_gbq( From c9da96a83bf9363bbc5f92b3b46ba4837e561892 Mon Sep 17 00:00:00 2001 From: kians376 Date: Wed, 13 Jul 2022 17:40:12 -0700 Subject: [PATCH 2/5] - Added tests for both from_dict and to_dict --- tests/test_frame.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/tests/test_frame.py b/tests/test_frame.py index 6d7731f01..4abc2172b 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -1,8 +1,8 @@ # flake8: noqa: F841 -from datetime import date import io -from pathlib import Path import tempfile +from datetime import date +from pathlib import Path from typing import ( TYPE_CHECKING, Any, @@ -16,14 +16,13 @@ import numpy as np import pandas as pd -from pandas._testing import getSeriesData import pytest +from pandas.io.parsers import TextFileReader from typing_extensions import assert_type +from pandas._testing import getSeriesData from tests import check -from pandas.io.parsers import TextFileReader - def test_types_init() -> None: pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]}) @@ -769,6 +768,19 @@ def test_types_resample() -> None: df.resample("20min", origin="epoch", offset=pd.Timedelta(2, "minutes"), on="date") +def test_types_to_dict() -> None: + data = pd.DataFrame({"a": 1, "b": 2}) + data.to_dict(orient='records') + data.to_dict(orient='dict') + data.to_dict(orient='list') + data.to_dict(orient='series') + data.to_dict(orient='split') + data.to_dict(orient='index') + + # orient param accepting "tight" added in 1.4.0 https://pandas.pydata.org/docs/whatsnew/v1.4.0.html + data.to_dict(orient='tight') + + 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"]}) @@ -778,7 +790,15 @@ def test_types_from_dict() -> None: pd.DataFrame.from_dict( data={"col_1": {"a": 1}, "col_2": {"a": 1, "b": 2}}, orient="columns" ) - + # 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') def test_pipe() -> None: if TYPE_CHECKING: # skip pytest From a2351d951925ff09b30afbaf23ecc53c23ec2772 Mon Sep 17 00:00:00 2001 From: kians376 Date: Wed, 13 Jul 2022 17:42:52 -0700 Subject: [PATCH 3/5] Added types to from_dict --- pandas-stubs/core/frame.pyi | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index 0d86df484..c7feb2095 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -210,7 +210,13 @@ class DataFrame(NDFrame, OpsMixin): def __matmul__(self, other): ... def __rmatmul__(self, other): ... @classmethod - def from_dict(cls, data, orient=..., dtype=..., columns=...) -> DataFrame: ... + def from_dict( + cls, + data: Dict[Any, Any], + orient: Literal["columns", "index", "tight"] = ..., + dtype: _str = ..., + columns: List[_str] = ... + ) -> DataFrame: ... def to_numpy( self, dtype: Optional[Union[Type[DtypeNp], Dtype]] = ..., From f6d273a1200055f765a530076a063c61c8755524 Mon Sep 17 00:00:00 2001 From: kians376 Date: Thu, 14 Jul 2022 10:09:56 -0700 Subject: [PATCH 4/5] Fix failing tests --- tests/test_frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_frame.py b/tests/test_frame.py index 4abc2172b..5222ac75c 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -769,7 +769,7 @@ def test_types_resample() -> None: def test_types_to_dict() -> None: - data = pd.DataFrame({"a": 1, "b": 2}) + data = pd.DataFrame({"a": [1], "b": [2]}) data.to_dict(orient='records') data.to_dict(orient='dict') data.to_dict(orient='list') From 906c8cc26c376ae951e416dfaae6f8c9677d20c3 Mon Sep 17 00:00:00 2001 From: kians376 Date: Mon, 18 Jul 2022 16:02:38 -0700 Subject: [PATCH 5/5] Fix formatting --- pandas-stubs/core/frame.pyi | 10 +++++----- tests/test_frame.py | 38 ++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/pandas-stubs/core/frame.pyi b/pandas-stubs/core/frame.pyi index c7feb2095..d637a953d 100644 --- a/pandas-stubs/core/frame.pyi +++ b/pandas-stubs/core/frame.pyi @@ -211,11 +211,11 @@ class DataFrame(NDFrame, OpsMixin): def __rmatmul__(self, other): ... @classmethod def from_dict( - cls, - data: Dict[Any, Any], - orient: Literal["columns", "index", "tight"] = ..., - dtype: _str = ..., - columns: List[_str] = ... + cls, + data: Dict[Any, Any], + orient: Literal["columns", "index", "tight"] = ..., + dtype: _str = ..., + columns: List[_str] = ..., ) -> DataFrame: ... def to_numpy( self, diff --git a/tests/test_frame.py b/tests/test_frame.py index 5222ac75c..20cc77bc8 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -1,8 +1,8 @@ # flake8: noqa: F841 -import io -import tempfile from datetime import date +import io from pathlib import Path +import tempfile from typing import ( TYPE_CHECKING, Any, @@ -16,13 +16,14 @@ import numpy as np import pandas as pd +from pandas._testing import getSeriesData import pytest -from pandas.io.parsers import TextFileReader from typing_extensions import assert_type -from pandas._testing import getSeriesData from tests import check +from pandas.io.parsers import TextFileReader + def test_types_init() -> None: pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]}) @@ -770,15 +771,15 @@ def test_types_resample() -> None: def test_types_to_dict() -> None: data = pd.DataFrame({"a": [1], "b": [2]}) - data.to_dict(orient='records') - data.to_dict(orient='dict') - data.to_dict(orient='list') - data.to_dict(orient='series') - data.to_dict(orient='split') - data.to_dict(orient='index') + data.to_dict(orient="records") + data.to_dict(orient="dict") + data.to_dict(orient="list") + data.to_dict(orient="series") + data.to_dict(orient="split") + data.to_dict(orient="index") # orient param accepting "tight" added in 1.4.0 https://pandas.pydata.org/docs/whatsnew/v1.4.0.html - data.to_dict(orient='tight') + data.to_dict(orient="tight") def test_types_from_dict() -> None: @@ -793,12 +794,15 @@ def test_types_from_dict() -> None: # 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') + "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", + ) + def test_pipe() -> None: if TYPE_CHECKING: # skip pytest