From c642cf7de34410b5b7977b09b4a87c8f91ac685f Mon Sep 17 00:00:00 2001 From: Vaibhav Vishal Date: Mon, 23 Sep 2019 12:29:58 +0530 Subject: [PATCH 1/6] annotate method style --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0638c4c1b6a01..caccf937cd493 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -786,7 +786,7 @@ def to_string( # ---------------------------------------------------------------------- @property - def style(self): + def style(self) -> Styler: """ Property returning a Styler object containing methods for building a styled HTML representation fo the DataFrame. From 035d28a7948753a1604be6618f707ee756992c21 Mon Sep 17 00:00:00 2001 From: Vaibhav Vishal Date: Mon, 23 Sep 2019 13:57:00 +0530 Subject: [PATCH 2/6] initial type annotations of all methods (has errors) --- pandas/core/frame.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index caccf937cd493..ba1d943c71f80 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -14,7 +14,17 @@ import itertools import sys from textwrap import dedent -from typing import FrozenSet, List, Optional, Sequence, Set, Tuple, Type, Union +from typing import ( + FrozenSet, + Generator, + List, + Optional, + Sequence, + Set, + Tuple, + Type, + Union, +) import warnings import numpy as np @@ -786,10 +796,10 @@ def to_string( # ---------------------------------------------------------------------- @property - def style(self) -> Styler: + def style(self) -> "Styler": """ Property returning a Styler object containing methods for - building a styled HTML representation fo the DataFrame. + building a styled HTML representation of the DataFrame. See Also -------- @@ -850,7 +860,7 @@ def style(self) -> Styler: """ @Appender(_shared_docs["items"]) - def items(self): + def items(self) -> Generator[Tuple[str, Dtype], None, None]: if self.columns.is_unique and hasattr(self, "_item_cache"): for k in self.columns: yield k, self._get_item_cache(k) @@ -862,7 +872,7 @@ def items(self): def iteritems(self): yield from self.items() - def iterrows(self): + def iterrows(self) -> Generator[Tuple[Index, Series], None, None]: """ Iterate over DataFrame rows as (index, Series) pairs. @@ -994,13 +1004,13 @@ def itertuples(self, index=True, name="Pandas"): # fallback to regular tuples return zip(*arrays) - def __len__(self): + def __len__(self) -> int: """ Returns length of info axis, but here we use the index. """ return len(self.index) - def dot(self, other): + def dot(self, other: Union[Series, DataFrame]) -> Union[Series, DataFrame]: """ Compute the matrix multiplication between the DataFrame and other. @@ -1111,13 +1121,13 @@ def dot(self, other): else: # pragma: no cover raise TypeError("unsupported type: {oth}".format(oth=type(other))) - def __matmul__(self, other): + def __matmul__(self, other: Union[Series, DataFrame]) -> Union[Series, DataFrame]: """ Matrix multiplication using binary `@` operator in Python>=3.5. """ return self.dot(other) - def __rmatmul__(self, other): + def __rmatmul__(self, other: Union[Series, DataFrame]) -> Union[Series, DataFrame]: """ Matrix multiplication using binary `@` operator in Python>=3.5. """ From a640588d7c75ad2634f4cb8968be005cfb22b5e8 Mon Sep 17 00:00:00 2001 From: Vaibhav Vishal Date: Tue, 29 Oct 2019 10:46:57 +0530 Subject: [PATCH 3/6] use Iterator instead of generator and np.ndarray to dot --- pandas/core/frame.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index c9acf1be4a11c..523ec93379fab 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -16,7 +16,6 @@ from textwrap import dedent from typing import ( FrozenSet, - Generator, Hashable, Iterable, List, @@ -891,7 +890,7 @@ def items(self) -> Iterable[Tuple[Optional[Hashable], Series]]: def iteritems(self): yield from self.items() - def iterrows(self) -> Generator[Tuple[Index, Series], None, None]: + def iterrows(self) -> Iterable[Tuple[Index, Series]]: """ Iterate over DataFrame rows as (index, Series) pairs. @@ -1029,7 +1028,9 @@ def __len__(self) -> int: """ return len(self.index) - def dot(self, other: Union[Series, DataFrame]) -> Union[Series, DataFrame]: + def dot( + self, other: Union[Series, DataFrame, np.ndarray] + ) -> Union[Series, DataFrame, np.ndarray]: """ Compute the matrix multiplication between the DataFrame and other. @@ -4826,7 +4827,7 @@ def drop_duplicates(self, subset=None, keep="first", inplace=False): duplicated = self.duplicated(subset, keep=keep) if inplace: - inds, = (-duplicated)._ndarray_values.nonzero() + (inds,) = (-duplicated)._ndarray_values.nonzero() new_data = self._data.take(inds) self._update_inplace(new_data) else: From cc767b33fa28daf189917eb232751c21a6392d79 Mon Sep 17 00:00:00 2001 From: Vaibhav Vishal Date: Mon, 2 Dec 2019 13:02:24 +0530 Subject: [PATCH 4/6] changes --- pandas/core/frame.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 772825a1687a7..626058eae6cb7 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1021,8 +1021,8 @@ def __len__(self) -> int: return len(self.index) def dot( - self, other: Union[Series, DataFrame, np.ndarray] - ) -> Union[Series, DataFrame, np.ndarray]: + self, other: Union[Series, DataFrame] + ) -> Union[Series, DataFrame]: """ Compute the matrix multiplication between the DataFrame and other. @@ -4633,7 +4633,7 @@ def drop_duplicates(self, subset=None, keep="first", inplace=False): duplicated = self.duplicated(subset, keep=keep) if inplace: - (inds,) = (-duplicated)._ndarray_values.nonzero() + inds = (-duplicated)._ndarray_values.nonzero()[0] new_data = self._data.take(inds) self._update_inplace(new_data) else: From fd5ddd5d85dde4b9f570ca683d659776adddba5b Mon Sep 17 00:00:00 2001 From: Vaibhav Vishal Date: Mon, 2 Dec 2019 13:04:58 +0530 Subject: [PATCH 5/6] format black --- pandas/core/frame.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 626058eae6cb7..24670c5608338 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1020,9 +1020,7 @@ def __len__(self) -> int: """ return len(self.index) - def dot( - self, other: Union[Series, DataFrame] - ) -> Union[Series, DataFrame]: + def dot(self, other: Union[Series, DataFrame]) -> Union[Series, DataFrame]: """ Compute the matrix multiplication between the DataFrame and other. From ca647368859975a8a0560e368a71993e78ce856e Mon Sep 17 00:00:00 2001 From: Vaibhav Vishal Date: Tue, 3 Dec 2019 01:52:46 +0530 Subject: [PATCH 6/6] fix Styler import --- pandas/_typing.py | 1 + pandas/core/frame.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 445eff9e19e47..e480933e9da39 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -23,6 +23,7 @@ from pandas.core.indexes.base import Index # noqa: F401 from pandas.core.series import Series # noqa: F401 from pandas.core.generic import NDFrame # noqa: F401 + from pandas.io.formats.style import Styler # noqa: F401 AnyArrayLike = TypeVar("AnyArrayLike", "ExtensionArray", "Index", "Series", np.ndarray) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 24670c5608338..08ad92478c67c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -84,7 +84,7 @@ ) from pandas.core.dtypes.missing import isna, notna -from pandas._typing import Axes, Dtype, FilePathOrBuffer +from pandas._typing import Axes, Dtype, FilePathOrBuffer, Styler from pandas.core import algorithms, common as com, nanops, ops from pandas.core.accessor import CachedAccessor from pandas.core.arrays import Categorical, ExtensionArray