Skip to content

ENH: Add more 1.5.0 features #338

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 10 commits into from
Sep 30, 2022
1 change: 1 addition & 0 deletions pandas-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ from ._config import (
)
from .core.api import (
NA as NA,
ArrowDtype as ArrowDtype,
BooleanDtype as BooleanDtype,
Categorical as Categorical,
CategoricalDtype as CategoricalDtype,
Expand Down
28 changes: 27 additions & 1 deletion pandas-stubs/_libs/tslibs/offsets.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,33 @@ class CustomBusinessHour(BusinessHour):

class CustomBusinessMonthEnd(_CustomBusinessMonth): ...
class CustomBusinessMonthBegin(_CustomBusinessMonth): ...
class DateOffset(RelativeDeltaOffset): ...

class DateOffset(RelativeDeltaOffset):
def __init__(
self,
*,
n: int = ...,
normalize: bool = ...,
years: int = ...,
months: int = ...,
weeks: int = ...,
days: int = ...,
hours: int = ...,
minutes: int = ...,
seconds: int = ...,
milliseconds: int = ...,
microseconds: int = ...,
nanoseconds: int = ...,
year: int = ...,
month: int = ...,
day: int = ...,
weekday: int = ...,
hour: int = ...,
minute: int = ...,
second: int = ...,
microsecond: int = ...,
nanosecond: int = ...,
): ...

BDay = BusinessDay
BMonthEnd = BusinessMonthEnd
Expand Down
26 changes: 25 additions & 1 deletion pandas-stubs/_testing/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from typing import (
Any,
Generator,
Literal,
overload,
)

from pandas import (
Expand Down Expand Up @@ -54,6 +55,7 @@ def assert_extension_array_equal(
check_less_precise: bool = ...,
check_exact: bool = ...,
) -> None: ...
@overload
def assert_series_equal(
left: Series,
right: Series,
Expand All @@ -71,7 +73,29 @@ def assert_series_equal(
atol: float = ...,
obj: str = ...,
*,
check_index: bool = ...,
check_index: Literal[False],
check_like: Literal[False],
) -> None: ...
@overload
def assert_series_equal(
left: Series,
right: Series,
check_dtype: bool = ...,
check_index_type: bool | str = ...,
check_series_type: bool = ...,
check_names: bool = ...,
check_exact: bool = ...,
check_datetimelike_compat: bool = ...,
check_categorical: bool = ...,
check_category_order: bool = ...,
check_freq: bool = ...,
check_flags: bool = ...,
rtol: float = ...,
atol: float = ...,
obj: str = ...,
*,
check_index: Literal[True] = ...,
check_like: bool = ...,
) -> None: ...
def assert_frame_equal(
left: DataFrame,
Expand Down
1 change: 1 addition & 0 deletions pandas-stubs/core/api.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ from pandas.core.algorithms import (
value_counts as value_counts,
)
from pandas.core.arrays import Categorical as Categorical
from pandas.core.arrays.arrow.dtype import ArrowDtype as ArrowDtype
from pandas.core.arrays.boolean import BooleanDtype as BooleanDtype
from pandas.core.arrays.floating import (
Float32Dtype as Float32Dtype,
Expand Down
8 changes: 8 additions & 0 deletions pandas-stubs/core/arrays/arrow/dtype.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import numpy as np
import pyarrow as pa

from pandas.core.dtypes.base import StorageExtensionDtype

class ArrowDtype(StorageExtensionDtype):
pyarrow_dtype: pa.DataType
def __init__(self, pyarrow_dtype: pa.DataType) -> None: ...
2 changes: 2 additions & 0 deletions pandas-stubs/core/dtypes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ class ExtensionDtype:
def construct_from_string(cls, string: str): ...
@classmethod
def is_dtype(cls, dtype) -> bool: ...

class StorageExtensionDtype(ExtensionDtype): ...
3 changes: 2 additions & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ from pandas._typing import (
Dtype,
DtypeArg,
DtypeObj,
HashableT,
IndexT,
Label,
Level,
Expand Down Expand Up @@ -148,7 +149,7 @@ class Index(IndexOpsMixin, PandasObject):
def __neg__(self: IndexT) -> IndexT: ...
def __nonzero__(self) -> None: ...
__bool__ = ...
def union(self, other: list[T1] | Index, sort=...) -> Index: ...
def union(self, other: list[HashableT] | Index, sort=...) -> Index: ...
def intersection(self, other: list[T1] | Index, sort: bool = ...) -> Index: ...
def difference(self, other: list | Index) -> Index: ...
def symmetric_difference(
Expand Down
9 changes: 8 additions & 1 deletion pandas-stubs/core/indexes/multi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ from typing import (
)

import numpy as np
import pandas as pd
from pandas.core.indexes.base import Index

from pandas._typing import (
T1,
DtypeArg,
HashableT,
np_ndarray_bool,
)

Expand Down Expand Up @@ -88,7 +90,12 @@ class MultiIndex(Index):
def get_value(self, series, key): ...
def get_level_values(self, level: str | int) -> Index: ...
def unique(self, level=...): ...
def to_frame(self, index: bool = ..., name=...): ...
def to_frame(
self,
index: bool = ...,
name: list[HashableT] = ...,
allow_duplicates: bool = ...,
) -> pd.DataFrame: ...
def to_flat_index(self): ...
@property
def is_all_dates(self) -> bool: ...
Expand Down
9 changes: 8 additions & 1 deletion pandas-stubs/core/indexes/range.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import numpy as np
from pandas.core.indexes.base import Index
from pandas.core.indexes.numeric import Int64Index

from pandas._typing import npt
from pandas._typing import (
HashableT,
npt,
)

class RangeIndex(Int64Index):
def __new__(
Expand Down Expand Up @@ -70,3 +74,6 @@ class RangeIndex(Int64Index):
def __floordiv__(self, other): ...
def all(self) -> bool: ...
def any(self) -> bool: ...
def union(
self, other: list[HashableT] | Index, sort=...
) -> Index | Int64Index | RangeIndex: ...
2 changes: 1 addition & 1 deletion pandas-stubs/core/window/ewm.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ExponentialMovingWindow(BaseWindow[NDFrameT], Generic[NDFrameT]):
adjust: bool = ...,
ignore_na: bool = ...,
axis: Axis = ...,
times: str | np.ndarray | Series | None = ...,
times: str | np.ndarray | Series | None | np.timedelta64 = ...,
method: CalculationMethod = ...,
) -> None: ...
@overload
Expand Down
8 changes: 6 additions & 2 deletions pandas-stubs/io/parsers/readers.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from collections import abc
from collections import (
abc,
defaultdict,
)
import csv
from types import TracebackType
from typing import (
Expand All @@ -18,6 +21,7 @@ from pandas._typing import (
CompressionOptions,
CSVEngine,
CSVQuoting,
Dtype,
DtypeArg,
FilePath,
ReadCsvBuffer,
Expand All @@ -44,7 +48,7 @@ def read_csv(
| npt.NDArray
| Callable[[str], bool]
| None = ...,
dtype: DtypeArg | None = ...,
dtype: DtypeArg | defaultdict[str, Dtype] | None = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

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

doesn't this have to be changed for all the overloads in read_csv() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably, and also for read_clipboard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm still seeing that only one of the overloads was changed.

engine: CSVEngine | None = ...,
converters: dict[int | str, Callable[[str], Any]] = ...,
true_values: list[str] = ...,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,19 @@ def test_types_to_feather() -> None:
df.to_feather(file)


def test_arrow_dtype() -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should go in test_pandas.py not test_frame.py

pytest.importorskip("pyarrow")

import pyarrow as pa

check(
assert_type(
pd.ArrowDtype(pa.timestamp("s", tz="America/New_York")), pd.ArrowDtype
),
pd.ArrowDtype,
)


# compare() method added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
def test_types_compare() -> None:
df1 = pd.DataFrame(
Expand Down
32 changes: 32 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import annotations

from typing import Union

import numpy as np
from numpy import typing as npt
import pandas as pd
from pandas.core.indexes.numeric import NumericIndex
import pytest
from typing_extensions import assert_type

from tests import check
Expand Down Expand Up @@ -31,6 +34,10 @@ def test_index_astype() -> None:
mi = pd.MultiIndex.from_product([["a", "b"], ["c", "d"]], names=["ab", "cd"])
mia = mi.astype(object) # object is only valid parameter for MultiIndex.astype()
check(assert_type(mia, pd.MultiIndex), pd.MultiIndex)
check(
assert_type(mi.to_frame(name=[3, 7], allow_duplicates=True), pd.DataFrame),
pd.DataFrame,
)


def test_multiindex_get_level_values() -> None:
Expand Down Expand Up @@ -148,3 +155,28 @@ def test_index_relops() -> None:
check(assert_type(ind >= 2, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
check(assert_type(ind < 2, npt.NDArray[np.bool_]), np.ndarray, np.bool_)
check(assert_type(ind > 2, npt.NDArray[np.bool_]), np.ndarray, np.bool_)


def test_range_index_union():
with pytest.warns(FutureWarning, match="pandas.Int64Index"):
check(
assert_type(
pd.RangeIndex(0, 10).union(pd.RangeIndex(10, 20)),
Union[pd.Index, pd.Int64Index, pd.RangeIndex],
),
pd.RangeIndex,
)
check(
assert_type(
pd.RangeIndex(0, 10).union([11, 12, 13]),
Union[pd.Index, pd.Int64Index, pd.RangeIndex],
),
pd.Int64Index,
)
check(
assert_type(
pd.RangeIndex(0, 10).union(["a", "b", "c"]),
Union[pd.Index, pd.Int64Index, pd.RangeIndex],
),
pd.Index,
)
14 changes: 14 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import defaultdict
import csv
import io
import os.path
Expand Down Expand Up @@ -208,6 +209,10 @@ def test_clipboard():
check(assert_type(read_clipboard(), DataFrame), DataFrame)
check(assert_type(read_clipboard(iterator=False), DataFrame), DataFrame)
check(assert_type(read_clipboard(chunksize=None), DataFrame), DataFrame)
check(
assert_type(read_clipboard(dtype=defaultdict(lambda: "f8")), DataFrame),
DataFrame,
)


def test_clipboard_iterator():
Expand Down Expand Up @@ -426,6 +431,11 @@ def test_read_csv():
check(assert_type(read_csv(path, iterator=False), DataFrame), DataFrame)
check(assert_type(read_csv(path, chunksize=None), DataFrame), DataFrame)

check(
assert_type(read_csv(path, dtype=defaultdict(lambda: "f8")), DataFrame),
DataFrame,
)


def test_read_csv_iterator():
with ensure_clean() as path:
Expand Down Expand Up @@ -489,6 +499,10 @@ def test_read_table():
check(assert_type(read_table(path), DataFrame), DataFrame)
check(assert_type(read_table(path, iterator=False), DataFrame), DataFrame)
check(assert_type(read_table(path, chunksize=None), DataFrame), DataFrame)
check(
assert_type(read_table(path, dtype=defaultdict(lambda: "f8")), DataFrame),
DataFrame,
)


def test_read_table_iterator():
Expand Down
5 changes: 3 additions & 2 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ def test_types_assert_series_equal() -> None:
check_datetimelike_compat=True,
)
if TYPE_CHECKING_INVALID_USAGE:
assert_series_equal(
assert_series_equal( # type: ignore[call-overload]
s1,
s2,
check_dtype=True,
check_less_precise=True, # type: ignore[call-arg]
check_less_precise=True,
check_names=True,
)
assert_series_equal(s1, s2, check_like=True)


def test_assert_frame_equal():
Expand Down