Skip to content

Commit 0760586

Browse files
authored
fix various issues with unknown methods in pandas (#166)
* add concat set_flags Timestamp to deal with unknowns * fix various unknown declarations * add partial arg type checking
1 parent 6ab1717 commit 0760586

23 files changed

+494
-86
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
cp -R partial/ typings
3434

3535
- name: Run tests
36-
uses: jakebailey/pyright-action@main
36+
uses: gramster/pyright-action@main
3737
with:
3838
project: pyrighttestconfig.json
39+
warn-partial: true

partial/pandas/_libs/__init__.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .tslibs import NaT as NaT, NaTType as NaTType, OutOfBoundsDatetime as OutOfBoundsDatetime, Period as Period, Timedelta as Timedelta, Timestamp as Timestamp, iNaT as iNaT
2+
from .interval import Interval as Interval
3+
4+

partial/pandas/_typing.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ from pandas.core.generic import NDFrame
2828
from pandas._libs.tslibs import Period, Timedelta, Timestamp
2929
from pandas.core.arrays import ExtensionArray
3030
from pandas.core.series import Series
31+
from pandas.core.frame import DataFrame
3132
from pandas.core.indexes.base import Index
3233

3334
if sys.version_info >= (3, 8):
@@ -49,6 +50,7 @@ FileOrBuffer = Union[str, Buffer[AnyStr]]
4950
FilePathOrBuffer = Union["PathLike[str]", FileOrBuffer[AnyStr]]
5051

5152
FrameOrSeries = TypeVar("FrameOrSeries", bound=NDFrame)
53+
FrameOrSeriesUnion = Union[DataFrame, Series]
5254
Axis = Union[str, int]
5355
Label = Optional[Hashable]
5456
Level = Union[Label, int]

partial/pandas/core/arraylike.pyi

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@ from pandas._typing import FrameOrSeries, DataFrame
44
class OpsMixinProtocol(Protocol): ...
55

66
class OpsMixin:
7-
def __eq__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
8-
def __ne__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
9-
def __lt__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
10-
def __le__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
11-
def __gt__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
12-
def __ge__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
7+
def __eq__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
8+
def __ne__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
9+
def __lt__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
10+
def __le__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
11+
def __gt__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
12+
def __ge__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
1313
# -------------------------------------------------------------
1414
# Logical Methods
15-
def __and__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
16-
def __rand__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
17-
def __or__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
18-
def __ror__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
19-
def __xor__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
20-
def __rxor__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
15+
def __and__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
16+
def __rand__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
17+
def __or__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
18+
def __ror__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
19+
def __xor__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
20+
def __rxor__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
2121
# -------------------------------------------------------------
2222
# Arithmetic Methods
23-
def __add__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
24-
def __radd__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
25-
def __sub__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
26-
def __rsub__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
27-
def __mul__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
28-
def __rmul__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
29-
def __truediv__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
30-
def __rtruediv__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
31-
def __floordiv__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
32-
def __rfloordiv__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
33-
def __mod__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
34-
def __rmod__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
23+
def __add__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
24+
def __radd__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
25+
def __sub__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
26+
def __rsub__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
27+
def __mul__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
28+
def __rmul__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
29+
def __truediv__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
30+
def __rtruediv__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
31+
def __floordiv__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
32+
def __rfloordiv__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
33+
def __mod__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
34+
def __rmod__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
3535
def __divmod__(self: OpsMixinProtocol, other: DataFrame) -> Tuple[DataFrame, DataFrame]: ...
3636
def __rdivmod__(self: OpsMixinProtocol, other: DataFrame) -> Tuple[DataFrame, DataFrame]: ...
37-
def __pow__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
38-
def __rpow__(self: OpsMixinProtocol, other: Any) -> FrameOrSeries: ...
37+
def __pow__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...
38+
def __rpow__(self: OpsMixinProtocol, other: Any) -> DataFrame: ...

partial/pandas/core/frame.pyi

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,32 @@ class DataFrame(NDFrame, OpsMixin):
233233
version: int = ...,
234234
convert_strl: Optional[List[_str]] = ...,
235235
) -> None: ...
236-
def to_feather(self, path: FileOrBuffer, **kwargs) -> None: ...
236+
def to_feather(self, path: FilePathOrBuffer, **kwargs) -> None: ...
237237
@overload
238238
def to_markdown(self, buf: Optional[FilePathOrBuffer], mode: Optional[_str] = ..., **kwargs) -> None: ...
239239
@overload
240240
def to_markdown(self, mode: Optional[_str] = ..., **kwargs) -> _str: ...
241+
@overload
241242
def to_parquet(
242243
self,
243-
path: _str,
244+
path: Optional[FilePathOrBuffer],
244245
engine: Union[_str, Literal["auto", "pyarrow", "fastparquet"]] = ...,
245246
compression: Union[_str, Literal["snappy", "gzip", "brotli"]] = ...,
246247
index: Optional[_bool] = ...,
247248
partition_cols: Optional[List] = ...,
248249
**kwargs,
249250
) -> None: ...
250251
@overload
252+
def to_parquet(
253+
self,
254+
path: Optional[FilePathOrBuffer] = ...,
255+
engine: Union[_str, Literal["auto", "pyarrow", "fastparquet"]] = ...,
256+
compression: Union[_str, Literal["snappy", "gzip", "brotli"]] = ...,
257+
index: Optional[_bool] = ...,
258+
partition_cols: Optional[List] = ...,
259+
**kwargs,
260+
) -> bytes: ...
261+
@overload
251262
def to_html(
252263
self,
253264
buf: Optional[FilePathOrBuffer],
@@ -730,6 +741,14 @@ class DataFrame(NDFrame, OpsMixin):
730741
ignore_index: _bool = ...,
731742
key: Optional[Callable] = ...,
732743
) -> Union[None, DataFrame]: ...
744+
def value_counts(
745+
self,
746+
subset: Optional[Sequence[Hashable]] = ...,
747+
normalize: _bool = ...,
748+
sort: _bool = ...,
749+
ascending: _bool = ...,
750+
dropna: _bool = ...,
751+
) -> Series[int]: ...
733752
def nlargest(
734753
self,
735754
n: int,
@@ -744,6 +763,13 @@ class DataFrame(NDFrame, OpsMixin):
744763
) -> DataFrame: ...
745764
def swaplevel(self, i: Level = ..., j: Level = ..., axis: AxisType = ...) -> DataFrame: ...
746765
def reorder_levels(self, order: List, axis: AxisType = ...) -> DataFrame: ...
766+
def compare(
767+
self,
768+
other: DataFrame,
769+
align_axis: Axis = ...,
770+
keep_shape: bool = ...,
771+
keep_equal: bool = ...,
772+
) -> DataFrame: ...
747773
def combine(
748774
self,
749775
other: DataFrame,

partial/pandas/core/generic.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ from pandas._typing import (
1212
Scalar as Scalar,
1313
SeriesAxisType as SeriesAxisType,
1414
FrameOrSeries as FrameOrSeries,
15-
S1 as S1
15+
S1 as S1,
1616
)
1717
from pandas.core.base import PandasObject as PandasObject, SelectionMixin as SelectionMixin
1818
from pandas.core.indexes.api import Index as Index
@@ -37,6 +37,7 @@ class NDFrame(PandasObject, SelectionMixin, indexing.IndexingMixin):
3737
attrs: Optional[Mapping[Optional[Hashable], Any]] = ...,
3838
fastpath: _bool = ...,
3939
) -> None: ...
40+
def set_flags(self: FrameOrSeries, *, copy: bool = ..., allows_duplicate_labels: Optional[bool] = ...) -> FrameOrSeries: ...
4041
@property
4142
def attrs(self) -> Dict[Optional[Hashable], Any]: ...
4243
@attrs.setter
@@ -99,7 +100,7 @@ class NDFrame(PandasObject, SelectionMixin, indexing.IndexingMixin):
99100
def equals(self, other: Series[S1]) -> _bool: ...
100101
def __neg__(self) -> None: ...
101102
def __pos__(self) -> None: ...
102-
def __invert__(self): ...
103+
def __invert__(self) -> NDFrame: ...
103104
def __nonzero__(self) -> None: ...
104105
def bool(self) -> _bool: ...
105106
def __abs__(self) -> NDFrame: ...

partial/pandas/core/groupby/generic.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def generate_property(name: str, klass: Type[FrameOrSeries]): ...
2424
def pin_whitelisted_properties(klass: Type[FrameOrSeries], whitelist: FrozenSet[str]): ...
2525

2626
class SeriesGroupBy(GroupBy):
27-
def apply(self, func, *args, **kwargs): ...
27+
def apply(self, func, *args, **kwargs) -> Series: ...
2828
def aggregate(self, func=..., *args, **kwargs) -> Series: ...
2929
agg = aggregate
3030
def transform(self, func, *args, **kwargs): ...
@@ -71,6 +71,7 @@ class SeriesGroupBy(GroupBy):
7171
def nth(self, n: Union[int, Sequence[int]], dropna: Optional[str] = ...) -> Series[S1]: ...
7272

7373
class DataFrameGroupBy(GroupBy):
74+
def apply(self, func, *args, **kwargs) -> DataFrame: ...
7475
@overload
7576
def aggregate(self, arg: str, *args, **kwargs) -> DataFrame: ...
7677
@overload
@@ -85,6 +86,7 @@ class DataFrameGroupBy(GroupBy):
8586
def agg(self, arg: F, *args, **kwargs) -> DataFrame: ...
8687
def transform(self, func, *args, **kwargs): ...
8788
def filter(self, func: Callable, dropna: bool = ..., *args, **kwargs) -> DataFrame: ...
89+
def nunique(self, dropna: bool = ...) -> DataFrame: ...
8890
@overload
8991
def __getitem__(self, item: str) -> SeriesGroupBy: ...
9092
@overload
Lines changed: 82 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
from pandas._typing import FrameOrSeries as FrameOrSeries, Scalar as Scalar, AxisType as AxisType, KeysArgType
1+
from pandas._typing import (
2+
FrameOrSeries as FrameOrSeries,
3+
FrameOrSeriesUnion as FrameOrSeriesUnion,
4+
Scalar as Scalar,
5+
AxisType as AxisType,
6+
KeysArgType,
7+
)
28
from pandas.core.base import PandasObject as PandasObject, SelectionMixin as SelectionMixin
39
from pandas.core.frame import DataFrame as DataFrame
410
from pandas.core.generic import NDFrame as NDFrame
@@ -7,11 +13,10 @@ from pandas.core.indexes.api import Index as Index
713
from pandas.core.series import Series as Series
814
from typing import Any, Callable, Dict, Generator, List, Optional, Tuple, Union
915

10-
1116
class GroupByPlot(PandasObject):
1217
def __init__(self, groupby) -> None: ...
1318
def __call__(self, *args, **kwargs): ...
14-
def __getattr__(self, name: str) : ...
19+
def __getattr__(self, name: str): ...
1520

1621
class _GroupBy(PandasObject, SelectionMixin):
1722
level = ...
@@ -26,66 +31,103 @@ class _GroupBy(PandasObject, SelectionMixin):
2631
axis = ...
2732
grouper = ...
2833
exclusions = ...
29-
def __init__(self, obj: NDFrame, keys: Optional[KeysArgType]=..., axis: int=..., level=..., grouper: Optional[ops.BaseGrouper]=..., exclusions=..., selection=..., as_index: bool=..., sort: bool=..., group_keys: bool=..., squeeze: bool=..., observed: bool=..., mutated: bool=...) -> None: ...
34+
def __init__(
35+
self,
36+
obj: NDFrame,
37+
keys: Optional[KeysArgType] = ...,
38+
axis: int = ...,
39+
level=...,
40+
grouper: Optional[ops.BaseGrouper] = ...,
41+
exclusions=...,
42+
selection=...,
43+
as_index: bool = ...,
44+
sort: bool = ...,
45+
group_keys: bool = ...,
46+
squeeze: bool = ...,
47+
observed: bool = ...,
48+
mutated: bool = ...,
49+
) -> None: ...
3050
def __len__(self) -> int: ...
3151
@property
3252
def groups(self) -> Dict[str, str]: ...
3353
@property
3454
def ngroups(self): ...
3555
@property
3656
def indices(self) -> Dict[str, Index]: ...
37-
def __getattr__(self, attr: str) : ...
38-
def pipe(self, func: Callable, *args, **kwargs) : ...
57+
def __getattr__(self, attr: str): ...
58+
def pipe(self, func: Callable, *args, **kwargs): ...
3959
plot = ...
4060
def get_group(self, name, obj: Optional[DataFrame] = ...) -> DataFrame: ...
4161
def __iter__(self) -> Generator[Tuple[str, Any], None, None]: ...
42-
def apply(self, func: Callable, *args, **kwargs) -> FrameOrSeries: ...
62+
def apply(self, func: Callable, *args, **kwargs) -> FrameOrSeriesUnion: ...
4363
def transform(self, func: Callable, *args, **kwargs): ...
4464

45-
4665
class GroupBy(_GroupBy):
47-
def any(self, skipna: bool=...) -> bool: ...
48-
def all(self, skipna: bool=...) -> bool: ...
49-
def count(self) -> FrameOrSeries: ...
50-
def mean(self, **kwargs) -> FrameOrSeries: ...
51-
def median(self, **kwargs) -> FrameOrSeries: ...
52-
def std(self, ddof: int = ...) -> FrameOrSeries: ...
53-
def var(self, ddof: int = ...) -> FrameOrSeries: ...
54-
def sem(self, ddof: int = ...) -> FrameOrSeries: ...
66+
def any(self, skipna: bool = ...) -> bool: ...
67+
def all(self, skipna: bool = ...) -> bool: ...
68+
def count(self) -> FrameOrSeriesUnion: ...
69+
def mean(self, **kwargs) -> FrameOrSeriesUnion: ...
70+
def median(self, **kwargs) -> FrameOrSeriesUnion: ...
71+
def std(self, ddof: int = ...) -> FrameOrSeriesUnion: ...
72+
def var(self, ddof: int = ...) -> FrameOrSeriesUnion: ...
73+
def sem(self, ddof: int = ...) -> FrameOrSeriesUnion: ...
5574
def size(self) -> Series: ...
5675
def ohlc(self) -> DataFrame: ...
57-
def describe(self, **kwargs) -> FrameOrSeries: ...
76+
def describe(self, **kwargs) -> FrameOrSeriesUnion: ...
5877
def resample(self, rule, *args, **kwargs): ...
5978
def rolling(self, *args, **kwargs): ...
6079
def expanding(self, *args, **kwargs): ...
6180
def pad(self, limit: Optional[int] = ...): ...
62-
def ffill(self, limit: Optional[int] = ...) -> FrameOrSeries: ...
63-
def backfill(self, limit: Optional[int] = ...) -> FrameOrSeries: ...
64-
def bfill(self, limit: Optional[int] = ...) -> FrameOrSeries: ...
65-
def nth(self, n: Union[int, List[int]], dropna: Optional[str] = ...) -> FrameOrSeries: ...
66-
def quantile(self, q=..., interpolation: str=...) : ...
81+
def ffill(self, limit: Optional[int] = ...) -> FrameOrSeriesUnion: ...
82+
def backfill(self, limit: Optional[int] = ...) -> FrameOrSeriesUnion: ...
83+
def bfill(self, limit: Optional[int] = ...) -> FrameOrSeriesUnion: ...
84+
def nth(self, n: Union[int, List[int]], dropna: Optional[str] = ...) -> FrameOrSeriesUnion: ...
85+
def quantile(self, q=..., interpolation: str = ...): ...
6786
def ngroup(self, ascending: bool = ...) -> Series: ...
6887
def cumcount(self, ascending: bool = ...) -> Series: ...
6988
def rank(
70-
self, method: str = ..., ascending: bool = ..., na_option: str = ..., pct: bool = ..., axis: int = ...,
89+
self,
90+
method: str = ...,
91+
ascending: bool = ...,
92+
na_option: str = ...,
93+
pct: bool = ...,
94+
axis: int = ...,
7195
) -> DataFrame: ...
72-
def cummax(self, axis: AxisType = ..., **kwargs) -> FrameOrSeries: ...
73-
def cummin(self, axis: AxisType = ..., **kwargs) -> FrameOrSeries: ...
74-
def cumprod(self, axis: AxisType = ..., **kwargs) -> FrameOrSeries: ...
75-
def cumsum(self, axis: AxisType = ..., **kwargs) -> FrameOrSeries: ...
76-
def shift(self, periods: int = ..., freq = ..., axis: int = ..., fill_value = ...): ...
96+
def cummax(self, axis: AxisType = ..., **kwargs) -> FrameOrSeriesUnion: ...
97+
def cummin(self, axis: AxisType = ..., **kwargs) -> FrameOrSeriesUnion: ...
98+
def cumprod(self, axis: AxisType = ..., **kwargs) -> FrameOrSeriesUnion: ...
99+
def cumsum(self, axis: AxisType = ..., **kwargs) -> FrameOrSeriesUnion: ...
100+
def shift(self, periods: int = ..., freq=..., axis: int = ..., fill_value=...): ...
77101
def pct_change(
78-
self, periods: int = ..., fill_method: str = ..., limit = ..., freq = ..., axis: AxisType = ...,
79-
) -> FrameOrSeries: ...
80-
def head(self, n: int = ...) -> FrameOrSeries: ...
81-
def tail(self, n: int = ...) -> FrameOrSeries: ...
102+
self,
103+
periods: int = ...,
104+
fill_method: str = ...,
105+
limit=...,
106+
freq=...,
107+
axis: AxisType = ...,
108+
) -> FrameOrSeriesUnion: ...
109+
def head(self, n: int = ...) -> FrameOrSeriesUnion: ...
110+
def tail(self, n: int = ...) -> FrameOrSeriesUnion: ...
82111
# Surplus methodss from original pylance stubs; should they go away?
83-
def first(self, **kwargs) -> FrameOrSeries: ...
84-
def last(self, **kwargs) -> FrameOrSeries: ...
85-
def max(self, **kwargs) -> FrameOrSeries: ...
86-
def min(self, **kwargs) -> FrameOrSeries: ...
87-
def prod(self, **kwargs) -> FrameOrSeries: ...
88-
def sum(self, **kwargs) -> FrameOrSeries: ...
89-
112+
def first(self, **kwargs) -> FrameOrSeriesUnion: ...
113+
def last(self, **kwargs) -> FrameOrSeriesUnion: ...
114+
def max(self, **kwargs) -> FrameOrSeriesUnion: ...
115+
def min(self, **kwargs) -> FrameOrSeriesUnion: ...
116+
def prod(self, **kwargs) -> FrameOrSeriesUnion: ...
117+
def sum(self, **kwargs) -> FrameOrSeriesUnion: ...
90118

91-
def get_groupby(obj: NDFrame, by: Optional[KeysArgType]=..., axis: int=..., level=..., grouper: Optional[ops.BaseGrouper]=..., exclusions=..., selection=..., as_index: bool=..., sort: bool=..., group_keys: bool=..., squeeze: bool=..., observed: bool=..., mutated: bool=...) -> GroupBy: ...
119+
def get_groupby(
120+
obj: NDFrame,
121+
by: Optional[KeysArgType] = ...,
122+
axis: int = ...,
123+
level=...,
124+
grouper: Optional[ops.BaseGrouper] = ...,
125+
exclusions=...,
126+
selection=...,
127+
as_index: bool = ...,
128+
sort: bool = ...,
129+
group_keys: bool = ...,
130+
squeeze: bool = ...,
131+
observed: bool = ...,
132+
mutated: bool = ...,
133+
) -> GroupBy: ...

0 commit comments

Comments
 (0)