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
28 changes: 19 additions & 9 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -786,10 +796,10 @@ 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.
building a styled HTML representation of the DataFrame.

See Also
--------
Expand Down Expand Up @@ -850,7 +860,7 @@ def style(self):
"""

@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)
Expand All @@ -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.

Expand Down Expand Up @@ -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]:
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 @@ -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.
"""
Expand Down