Skip to content

ENH: Improve typing of some general functions #355

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

Merged
merged 24 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5a03064
ENH: Improve typing of some general functions
bashtage Oct 5, 2022
d3a07a0
Further typing
Oct 5, 2022
936edb0
ENH: Add overload to factorize
bashtage Oct 6, 2022
f6e38ed
BUG: Correct npt import
bashtage Oct 6, 2022
e68812f
ENH: Improve unique
bashtage Oct 6, 2022
65538fe
ENH: Imrpveo typing in merge functions, cut and qcut
bashtage Oct 6, 2022
9cbb9f5
Merge remote-tracking branch 'upstream/main' into gen-funcs-v2
bashtage Oct 6, 2022
cee8aad
Merge branch 'gen-funcs-v2' of github.com:bashtage/pandas-stubs into …
bashtage Oct 6, 2022
220c561
CLN: Remove unused imports
bashtage Oct 6, 2022
9e61288
CLN: Catch warning
bashtage Oct 6, 2022
5e0df69
TST: Add tests for cut and fixes
Oct 6, 2022
6c475f8
TST: Add final test for cut
Oct 6, 2022
7ca155a
TST: Add tests and fixes for qcut
Oct 6, 2022
a0fb2c3
TYP: Add typ ignore for overlapping defs
bashtage Oct 7, 2022
737c4d4
Merge remote-tracking branch 'upstream/main' into gen-funcs-v2
bashtage Oct 7, 2022
64f0c1a
TST: Add tests for merge
bashtage Oct 7, 2022
bc1b3c8
TST: Add tests for merge_ordered and improve typing
bashtage Oct 7, 2022
0cf5ec3
TST: Add tests for merge_asof and improve typing accuracy
bashtage Oct 7, 2022
be824c6
CLN: Remove TODO since pandas PR opened
bashtage Oct 7, 2022
093206c
Merge remote-tracking branch 'upstream/main' into gen-funcs-v2
bashtage Oct 7, 2022
1c12204
TYP: Final refinements
bashtage Oct 7, 2022
a5e43fd
TST: Fix intentionally failing test
bashtage Oct 7, 2022
620e799
MAINT: Refactor MergeHow and add JoinHow
Oct 7, 2022
7876a5e
TYP: Correct use of MergeHow and add test
Oct 7, 2022
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
13 changes: 12 additions & 1 deletion pandas-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ FillnaOptions: TypeAlias = Literal["backfill", "bfill", "ffill", "pad"]
ReplaceMethod: TypeAlias = Literal["pad", "ffill", "bfill"]
SortKind: TypeAlias = Literal["quicksort", "mergesort", "heapsort", "stable"]
NaPosition: TypeAlias = Literal["first", "last"]
MergeHow: TypeAlias = Literal["left", "right", "outer", "inner"]
JoinHow: TypeAlias = Literal["left", "right", "outer", "inner"]
MergeHow: TypeAlias = Union[JoinHow, Literal["cross"]]
JsonFrameOrient: TypeAlias = Literal[
"split", "records", "index", "columns", "values", "table"
]
Expand Down Expand Up @@ -333,4 +334,14 @@ class StyleExportDict(TypedDict, total=False):

CalculationMethod: TypeAlias = Literal["single", "table"]

ValidationOptions: TypeAlias = Literal[
"one_to_one",
"1:1",
"one_to_many",
"1:m",
"many_to_one",
"m:1",
"many_to_many",
"m:m",
]
__all__ = ["npt", "type_t"]
39 changes: 35 additions & 4 deletions pandas-stubs/core/algorithms.pyi
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
from typing import (
Any,
Sequence,
overload,
)

import numpy as np
import pandas as pd
from pandas import (
Categorical,
CategoricalIndex,
Index,
IntervalIndex,
PeriodIndex,
Series,
)
from pandas.api.extensions import ExtensionArray

from pandas._typing import AnyArrayLike

# These are type: ignored because the Index types overlap due to inheritance but indices
# with extension types return the same type while standard type return ndarray
@overload
def unique(values: Index) -> Index: ...
def unique(values: PeriodIndex) -> PeriodIndex: ... # type: ignore[misc]
@overload
def unique(values: CategoricalIndex) -> CategoricalIndex: ... # type: ignore[misc]
@overload
def unique(values: IntervalIndex) -> IntervalIndex: ... # type: ignore[misc]
@overload
def unique(values: Index) -> np.ndarray: ...
@overload
def unique(values: Categorical) -> Categorical: ...
@overload
Expand All @@ -23,14 +35,33 @@ def unique(values: Series) -> np.ndarray | ExtensionArray: ...
def unique(values: np.ndarray | list) -> np.ndarray: ...
@overload
def unique(values: ExtensionArray) -> ExtensionArray: ...
@overload
def factorize(
values: Sequence,
sort: bool = ...,
# Not actually positional-only, used to handle deprecations in 1.5.0
*,
use_na_sentinel: bool = ...,
size_hint: int | None = ...,
) -> tuple[np.ndarray, np.ndarray]: ...
@overload
def factorize(
values: Index | Series,
sort: bool = ...,
# Not actually positional-only, used to handle deprecations in 1.5.0
*,
use_na_sentinel: bool = ...,
size_hint: int | None = ...,
) -> tuple[np.ndarray, Index]: ...
@overload
def factorize(
values: Any,
values: Categorical,
sort: bool = ...,
# Not actually positional-only, used to handle deprecations in 1.5.0
*,
use_na_sentinel: bool = ...,
size_hint: int | None = ...,
) -> tuple[np.ndarray, np.ndarray | Index]: ...
) -> tuple[np.ndarray, Categorical]: ...
def value_counts(
values: AnyArrayLike | list | tuple,
sort: bool = ...,
Expand Down
18 changes: 5 additions & 13 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ from pandas._typing import (
IndexLabel,
IndexType,
IntervalClosedType,
JoinHow,
JsonFrameOrient,
Label,
Level,
Expand All @@ -97,6 +98,7 @@ from pandas._typing import (
Suffixes,
T as TType,
TimestampConvention,
ValidationOptions,
WriteBuffer,
XMLParsers,
np_ndarray_bool,
Expand Down Expand Up @@ -531,7 +533,7 @@ class DataFrame(NDFrame, OpsMixin):
def align(
self,
other: DataFrame | Series,
join: MergeHow = ...,
join: JoinHow = ...,
axis: AxisType | None = ...,
level: Level | None = ...,
copy: _bool = ...,
Expand Down Expand Up @@ -1101,21 +1103,11 @@ class DataFrame(NDFrame, OpsMixin):
self,
other: DataFrame | Series | list[DataFrame | Series],
on: _str | list[_str] | None = ...,
how: MergeHow = ...,
how: JoinHow = ...,
lsuffix: _str = ...,
rsuffix: _str = ...,
sort: _bool = ...,
validate: Literal[
"one_to_one",
"1:1",
"one_to_many",
"1:m",
"many_to_one",
"m:1",
"many_to_many",
"m:m",
]
| None = ...,
validate: ValidationOptions | None = ...,
) -> DataFrame: ...
def merge(
self,
Expand Down
17 changes: 14 additions & 3 deletions pandas-stubs/core/reshape/melt.pyi
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
from typing import Hashable

import numpy as np
from pandas.core.frame import DataFrame

from pandas._typing import HashableT

def melt(
frame: DataFrame,
id_vars: tuple | list | np.ndarray | None = ...,
value_vars: tuple | list | np.ndarray | None = ...,
var_name: str | None = ...,
value_name: str = ...,
value_name: Hashable = ...,
col_level: int | str | None = ...,
ignore_index: bool = ...,
) -> DataFrame: ...
def lreshape(data: DataFrame, groups, dropna: bool = ..., label=...) -> DataFrame: ...
def lreshape(
data: DataFrame, groups: dict[HashableT, list[HashableT]], dropna: bool = ...
) -> DataFrame: ...
def wide_to_long(
df: DataFrame, stubnames, i, j, sep: str = ..., suffix: str = ...
df: DataFrame,
stubnames: str | list[str],
i: str | list[str],
j: str,
sep: str = ...,
suffix: str = ...,
) -> DataFrame: ...
177 changes: 72 additions & 105 deletions pandas-stubs/core/reshape/merge.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Sequence
from typing import (
Literal,
overload,
)

from pandas import (
DataFrame,
Expand All @@ -8,131 +11,95 @@ from pandas import (
from pandas._libs.tslibs import Timedelta
from pandas._typing import (
AnyArrayLike,
HashableT,
JoinHow,
Label,
MergeHow,
ValidationOptions,
)

def merge(
left: DataFrame | Series,
right: DataFrame | Series,
how: str = ...,
on: Label | Sequence | AnyArrayLike | None = ...,
left_on: Label | Sequence | AnyArrayLike | None = ...,
right_on: Label | Sequence | AnyArrayLike | None = ...,
how: MergeHow = ...,
on: Label | list[HashableT] | AnyArrayLike | None = ...,
left_on: Label | list[HashableT] | AnyArrayLike | None = ...,
right_on: Label | list[HashableT] | AnyArrayLike | None = ...,
left_index: bool = ...,
right_index: bool = ...,
sort: bool = ...,
suffixes: Sequence[str | None] = ...,
suffixes: list[str | None]
| tuple[str, str]
| tuple[None, str]
| tuple[str, None] = ...,
copy: bool = ...,
indicator: bool | str = ...,
validate: str = ...,
validate: ValidationOptions = ...,
) -> DataFrame: ...
@overload
def merge_ordered(
left: DataFrame | Series,
left: DataFrame,
right: DataFrame,
on: Label | list[HashableT] | None = ...,
left_on: Label | list[HashableT] | None = ...,
right_on: Label | list[HashableT] | None = ...,
left_by: Label | list[HashableT] | None = ...,
right_by: Label | list[HashableT] | None = ...,
fill_method: Literal["ffill"] | None = ...,
suffixes: list[str | None]
| tuple[str, str]
| tuple[None, str]
| tuple[str, None] = ...,
how: JoinHow = ...,
) -> DataFrame: ...
@overload
def merge_ordered(
left: Series,
right: DataFrame | Series,
on: Label | Sequence | AnyArrayLike | None = ...,
left_on: Label | Sequence | AnyArrayLike | None = ...,
right_on: Label | Sequence | AnyArrayLike | None = ...,
left_by: str | list[str] | None = ...,
right_by: str | list[str] | None = ...,
fill_method: str | None = ...,
suffixes: Sequence[str | None] = ...,
how: str = ...,
on: Label | list[HashableT] | None = ...,
left_on: Label | list[HashableT] | None = ...,
right_on: Label | list[HashableT] | None = ...,
left_by: None = ...,
right_by: None = ...,
fill_method: Literal["ffill"] | None = ...,
suffixes: list[str | None]
| tuple[str, str]
| tuple[None, str]
| tuple[str, None] = ...,
how: JoinHow = ...,
) -> DataFrame: ...
@overload
def merge_ordered(
left: DataFrame | Series,
right: Series,
on: Label | list[HashableT] | None = ...,
left_on: Label | list[HashableT] | None = ...,
right_on: Label | list[HashableT] | None = ...,
left_by: None = ...,
right_by: None = ...,
fill_method: Literal["ffill"] | None = ...,
suffixes: list[str | None]
| tuple[str, str]
| tuple[None, str]
| tuple[str, None] = ...,
how: JoinHow = ...,
) -> DataFrame: ...
def merge_asof(
left: DataFrame | Series,
right: DataFrame | Series,
on: Label | None = ...,
left_on: Label | AnyArrayLike | None = ...,
right_on: Label | AnyArrayLike | None = ...,
left_on: Label | None = ...,
right_on: Label | None = ...,
left_index: bool = ...,
right_index: bool = ...,
by: str | list[str] | None = ...,
left_by: str | None = ...,
right_by: str | None = ...,
suffixes: Sequence[str | None] = ...,
by: Label | list[HashableT] | None = ...,
left_by: Label | list[HashableT] | None = ...,
right_by: Label | list[HashableT] | None = ...,
suffixes: list[str | None]
| tuple[str, str]
| tuple[None, str]
| tuple[str, None] = ...,
tolerance: int | Timedelta | None = ...,
allow_exact_matches: bool = ...,
direction: str = ...,
direction: Literal["backward", "forward", "nearest"] = ...,
) -> DataFrame: ...

class _MergeOperation:
left = ...
right = ...
how = ...
axis = ...
on = ...
left_on = ...
right_on = ...
copy = ...
suffixes = ...
sort = ...
left_index = ...
right_index = ...
indicator = ...
indicator_name = ...
def __init__(
self,
left: Series | DataFrame,
right: Series | DataFrame,
how: str = ...,
on=...,
left_on=...,
right_on=...,
axis=...,
left_index: bool = ...,
right_index: bool = ...,
sort: bool = ...,
suffixes=...,
copy: bool = ...,
indicator: bool = ...,
validate=...,
) -> None: ...
def get_result(self): ...

class _OrderedMerge(_MergeOperation):
fill_method = ...
def __init__(
self,
left,
right,
on=...,
left_on=...,
right_on=...,
left_index: bool = ...,
right_index: bool = ...,
axis=...,
suffixes=...,
copy: bool = ...,
fill_method=...,
how: str = ...,
) -> None: ...
def get_result(self): ...

class _AsOfMerge(_OrderedMerge):
by = ...
left_by = ...
right_by = ...
tolerance = ...
allow_exact_matches = ...
direction = ...
def __init__(
self,
left,
right,
on=...,
left_on=...,
right_on=...,
left_index: bool = ...,
right_index: bool = ...,
by=...,
left_by=...,
right_by=...,
axis=...,
suffixes=...,
copy: bool = ...,
fill_method=...,
how: str = ...,
tolerance=...,
allow_exact_matches: bool = ...,
direction: str = ...,
) -> None: ...
Loading