Skip to content

[WIP] Annotate DataFrame (Part 3) #28575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just put this in a if TYPE_CHECKING block in pandas.core.frame instead of here



AnyArrayLike = TypeVar("AnyArrayLike", "ExtensionArray", "Index", "Series", np.ndarray)
Expand Down
14 changes: 7 additions & 7 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -804,7 +804,7 @@ def to_string(
# ----------------------------------------------------------------------

@property
def style(self):
def style(self) -> "Styler":
"""
Returns a Styler object.

Expand Down Expand Up @@ -882,7 +882,7 @@ def items(self) -> Iterable[Tuple[Optional[Hashable], Series]]:
def iteritems(self):
yield from self.items()

def iterrows(self):
def iterrows(self) -> Iterable[Tuple[Index, Series]]:
"""
Iterate over DataFrame rows as (index, Series) pairs.

Expand Down Expand Up @@ -1020,7 +1020,7 @@ def __len__(self) -> int:
"""
return len(self.index)

def dot(self, other):
def dot(self, other: Union[Series, DataFrame]) -> Union[Series, DataFrame]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should include np.ndarray in the signature here. Applicable to the rest below as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright.
Added some types without paying much attention, will take a closer later.
Btw I am guessing if type of other is Series, return type will be Series too, similar for DataFrame and np.ndarray, how do I indicate that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A TypeVar would make the function generic (i.e. the return type would match other if the same TypeVar is used for both) but unfortunately I don't think that is how this is actually implemented

Copy link
Contributor Author

@vaibhavhrt vaibhavhrt Sep 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, pretty sure saw something like that in the docstring. But did this in a hurry(thats why draft PR), a bit busy. Will take a closer look as soon as I can.

"""
Compute the matrix multiplication between the DataFrame and other.

Expand Down Expand Up @@ -1131,13 +1131,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.
"""
Expand Down Expand Up @@ -4631,7 +4631,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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this required for mypy? If not can you revert?

new_data = self._data.take(inds)
self._update_inplace(new_data)
else:
Expand Down