Skip to content

Commit 8a2f567

Browse files
authored
Merge branch 'main' into numpy21
2 parents c153773 + 9c20865 commit 8a2f567

File tree

10 files changed

+250
-122
lines changed

10 files changed

+250
-122
lines changed

pandas-stubs/_testing/__init__.pyi

Lines changed: 95 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,117 @@
1-
from collections.abc import Generator
1+
from collections.abc import (
2+
Container,
3+
Generator,
4+
Iterable,
5+
)
26
from contextlib import contextmanager
37
from typing import (
48
Any,
59
Literal,
610
overload,
711
)
12+
import warnings
813

14+
from matplotlib.artist import Artist
15+
import numpy as np
916
from pandas import (
17+
Categorical,
1018
DataFrame,
1119
Index,
1220
Series,
1321
)
22+
from pandas.arrays import (
23+
DatetimeArray,
24+
IntervalArray,
25+
PeriodArray,
26+
SparseArray,
27+
TimedeltaArray,
28+
)
29+
from pandas.core.arrays.base import ExtensionArray
30+
31+
from pandas._typing import (
32+
AnyArrayLike,
33+
T,
34+
)
1435

1536
def assert_almost_equal(
16-
left,
17-
right,
18-
check_dtype: bool | str = ...,
19-
check_less_precise: bool | int = ...,
37+
left: T,
38+
right: T,
39+
check_dtype: bool | Literal["equiv"] = ...,
40+
rtol: float = ...,
41+
atol: float = ...,
2042
**kwargs,
21-
): ...
22-
def assert_dict_equal(left, right, compare_keys: bool = ...): ...
23-
def assert_index_equal(left: Index, right: Index) -> None: ...
24-
def assert_class_equal(left, right, exact: bool | str = ..., obj=...): ...
25-
def assert_attr_equal(attr, left, right, obj: str = ...): ...
26-
def assert_is_valid_plot_return_object(objs) -> None: ...
27-
def assert_is_sorted(seq) -> None: ...
43+
) -> None: ...
44+
def assert_dict_equal(left: dict, right: dict, compare_keys: bool = ...) -> None: ...
45+
def assert_index_equal(
46+
left: Index,
47+
right: Index,
48+
exact: bool | Literal["equiv"] = ...,
49+
check_names: bool = ...,
50+
check_exact: bool = ...,
51+
check_categorical: bool = ...,
52+
check_order: bool = ...,
53+
rtol: float = ...,
54+
atol: float = ...,
55+
obj: str = ...,
56+
) -> None: ...
57+
def assert_class_equal(
58+
left: T, right: T, exact: bool | Literal["equiv"] = ..., obj: str = ...
59+
) -> None: ...
60+
def assert_attr_equal(
61+
attr: str, left: object, right: object, obj: str = ...
62+
) -> None: ...
63+
def assert_is_valid_plot_return_object(
64+
objs: Series | np.ndarray | Artist | tuple | dict,
65+
) -> None: ...
66+
def assert_is_sorted(seq: AnyArrayLike) -> None: ...
2867
def assert_categorical_equal(
29-
left,
30-
right,
68+
left: Categorical,
69+
right: Categorical,
3170
check_dtype: bool = ...,
3271
check_category_order: bool = ...,
3372
obj: str = ...,
3473
) -> None: ...
3574
def assert_interval_array_equal(
36-
left, right, exact: str = ..., obj: str = ...
75+
left: IntervalArray,
76+
right: IntervalArray,
77+
exact: bool | Literal["equiv"] = ...,
78+
obj: str = ...,
79+
) -> None: ...
80+
def assert_period_array_equal(
81+
left: PeriodArray, right: PeriodArray, obj: str = ...
82+
) -> None: ...
83+
def assert_datetime_array_equal(
84+
left: DatetimeArray, right: DatetimeArray, check_freq: bool = ...
85+
) -> None: ...
86+
def assert_timedelta_array_equal(
87+
left: TimedeltaArray, right: TimedeltaArray, check_freq: bool = ...
3788
) -> None: ...
38-
def assert_period_array_equal(left, right, obj: str = ...) -> None: ...
39-
def assert_datetime_array_equal(left, right, obj: str = ...) -> None: ...
40-
def assert_timedelta_array_equal(left, right, obj: str = ...) -> None: ...
4189
def assert_numpy_array_equal(
4290
left,
4391
right,
4492
strict_nan: bool = ...,
45-
check_dtype: bool = ...,
46-
err_msg=...,
47-
check_same=...,
93+
check_dtype: bool | Literal["equiv"] = ...,
94+
err_msg: str | None = ...,
95+
check_same: Literal["copy", "same"] | None = ...,
4896
obj: str = ...,
49-
): ...
97+
index_values: Index | np.ndarray | None = ...,
98+
) -> None: ...
5099
def assert_extension_array_equal(
51-
left,
52-
right,
53-
check_dtype: bool = ...,
54-
check_less_precise: bool = ...,
100+
left: ExtensionArray,
101+
right: ExtensionArray,
102+
check_dtype: bool | Literal["equiv"] = ...,
103+
index_values: Index | np.ndarray | None = ...,
55104
check_exact: bool = ...,
105+
rtol: float = ...,
106+
atol: float = ...,
107+
obj: str = ...,
56108
) -> None: ...
57109
@overload
58110
def assert_series_equal(
59111
left: Series,
60112
right: Series,
61-
check_dtype: bool = ...,
62-
check_index_type: bool | str = ...,
113+
check_dtype: bool | Literal["equiv"] = ...,
114+
check_index_type: bool | Literal["equiv"] = ...,
63115
check_series_type: bool = ...,
64116
check_names: bool = ...,
65117
check_exact: bool = ...,
@@ -79,8 +131,8 @@ def assert_series_equal(
79131
def assert_series_equal(
80132
left: Series,
81133
right: Series,
82-
check_dtype: bool = ...,
83-
check_index_type: bool | str = ...,
134+
check_dtype: bool | Literal["equiv"] = ...,
135+
check_index_type: bool | Literal["equiv"] = ...,
84136
check_series_type: bool = ...,
85137
check_names: bool = ...,
86138
check_exact: bool = ...,
@@ -103,37 +155,34 @@ def assert_frame_equal(
103155
check_index_type: bool | Literal["equiv"] = ...,
104156
check_column_type: bool | Literal["equiv"] = ...,
105157
check_frame_type: bool = ...,
106-
check_less_precise: int | bool = ...,
107158
check_names: bool = ...,
108159
by_blocks: bool = ...,
109160
check_exact: bool = ...,
110161
check_datetimelike_compat: bool = ...,
111162
check_categorical: bool = ...,
112163
check_like: bool = ...,
113164
check_freq: bool = ...,
114-
check_flag: bool = ...,
165+
check_flags: bool = ...,
115166
rtol: float = ...,
116167
atol: float = ...,
117168
obj: str = ...,
118169
) -> None: ...
119170
def assert_equal(left, right, **kwargs) -> None: ...
120-
def assert_sp_array_equal(
121-
left,
122-
right,
123-
check_dtype: bool = ...,
124-
check_kind: bool = ...,
125-
check_fill_value: bool = ...,
126-
consolidate_block_indices: bool = ...,
127-
) -> None: ...
128-
def assert_contains_all(iterable, dic) -> None: ...
129-
def assert_copy(iter1, iter2, **eql_kwargs) -> None: ...
171+
def assert_sp_array_equal(left: SparseArray, right: SparseArray) -> None: ...
172+
def assert_contains_all(iterable: Iterable[T], dic: Container[T]) -> None: ...
173+
def assert_copy(iter1: Iterable[T], iter2: Iterable[T], **eql_kwargs) -> None: ...
174+
@contextmanager
130175
def assert_produces_warning(
131-
expected_warning=...,
132-
filter_level: str = ...,
133-
clear=...,
176+
expected_warning: (
177+
type[Warning] | Literal[False] | tuple[type[Warning], ...] | None
178+
) = ...,
179+
filter_level: Literal[
180+
"error", "ignore", "always", "default", "module", "once"
181+
] = ...,
134182
check_stacklevel: bool = ...,
135183
raise_on_extra_warnings: bool = ...,
136-
) -> None: ...
184+
match: str | None = None,
185+
) -> Generator[list[warnings.WarningMessage], None, None]: ...
137186
@contextmanager
138187
def ensure_clean(
139188
filename: str | None = ..., return_filelike: bool = ..., **kwargs: Any

pandas-stubs/core/dtypes/missing.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ from pandas import (
1010
Index,
1111
Series,
1212
)
13-
from typing_extensions import TypeGuard
13+
from typing_extensions import TypeIs
1414

1515
from pandas._libs.missing import NAType
1616
from pandas._libs.tslibs import NaTType
@@ -32,7 +32,7 @@ def isna(obj: Index[Any] | list[Any] | ArrayLike) -> npt.NDArray[np.bool_]: ...
3232
@overload
3333
def isna(
3434
obj: Scalar | NaTType | NAType | None,
35-
) -> TypeGuard[NaTType | NAType | None]: ...
35+
) -> TypeIs[NaTType | NAType | None]: ...
3636

3737
isnull = isna
3838

@@ -43,6 +43,6 @@ def notna(obj: Series[Any]) -> Series[bool]: ...
4343
@overload
4444
def notna(obj: Index[Any] | list[Any] | ArrayLike) -> npt.NDArray[np.bool_]: ...
4545
@overload
46-
def notna(obj: ScalarT | NaTType | NAType | None) -> TypeGuard[ScalarT]: ...
46+
def notna(obj: ScalarT | NaTType | NAType | None) -> TypeIs[ScalarT]: ...
4747

4848
notnull = notna

pandas-stubs/io/clipboards.pyi

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ def read_clipboard(
7676
encoding: str | None = ...,
7777
encoding_errors: str | None = ...,
7878
dialect: str | csv.Dialect = ...,
79-
# error_bad_lines: bool | None = ..., # Deprecated: 1.3.0
80-
# warn_bad_lines: bool | None = ..., # Deprecated: 1.3.0
8179
on_bad_lines: (
8280
Literal["error", "warn", "skip"] | Callable[[list[str]], list[str] | None]
8381
) = ...,
@@ -137,8 +135,6 @@ def read_clipboard(
137135
encoding: str | None = ...,
138136
encoding_errors: str | None = ...,
139137
dialect: str | csv.Dialect = ...,
140-
# error_bad_lines: bool | None = ..., # Deprecated: 1.3.0
141-
# warn_bad_lines: bool | None = ..., # Deprecated: 1.3.0
142138
on_bad_lines: (
143139
Literal["error", "warn", "skip"] | Callable[[list[str]], list[str] | None]
144140
) = ...,
@@ -198,8 +194,6 @@ def read_clipboard(
198194
encoding: str | None = ...,
199195
encoding_errors: str | None = ...,
200196
dialect: str | csv.Dialect = ...,
201-
# error_bad_lines: bool | None = ..., # Deprecated: 1.3.0
202-
# warn_bad_lines: bool | None = ..., # Deprecated: 1.3.0
203197
on_bad_lines: (
204198
Literal["error", "warn", "skip"] | Callable[[list[str]], list[str] | None]
205199
) = ...,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ packages = [{ "include" = "pandas-stubs" }]
3030
[tool.poetry.dependencies]
3131
python = ">=3.10"
3232
types-pytz = ">= 2022.1.1"
33-
numpy = ">= 2.1"
33+
numpy = ">= 1.23.5"
3434

3535
[tool.poetry.group.dev.dependencies]
3636
mypy = "1.10.1"

tests/test_frame.py

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,13 @@ def test_types_to_html() -> None:
14461446
def test_types_resample() -> None:
14471447
df = pd.DataFrame({"values": [2, 11, 3, 13, 14, 18, 17, 19]})
14481448
df["date"] = pd.date_range("01/01/2018", periods=8, freq="W")
1449-
with pytest_warns_bounded(FutureWarning, "'M' is deprecated", lower="2.1.99"):
1449+
with pytest_warns_bounded(
1450+
FutureWarning,
1451+
"'M' is deprecated",
1452+
lower="2.1.99",
1453+
upper="2.2.99",
1454+
upper_exception=ValueError,
1455+
):
14501456
df.resample("M", on="date")
14511457
df.resample("20min", origin="epoch", offset=pd.Timedelta(2, "minutes"), on="date")
14521458
df.resample("20min", origin="epoch", offset=datetime.timedelta(2), on="date")
@@ -1583,8 +1589,14 @@ def resampler_foo(resampler: Resampler[pd.DataFrame]) -> pd.DataFrame:
15831589
assert isinstance(resampler, Resampler)
15841590
return pd.DataFrame(resampler)
15851591

1586-
with pytest_warns_bounded(FutureWarning, "'M' is deprecated", lower="2.1.99"):
1587-
val = (
1592+
with pytest_warns_bounded(
1593+
FutureWarning,
1594+
"'M' is deprecated",
1595+
lower="2.1.99",
1596+
upper="2.2.99",
1597+
upper_exception=ValueError,
1598+
):
1599+
(
15881600
pd.DataFrame(
15891601
{
15901602
"price": [10, 11, 9, 13, 14, 18, 17, 19],
@@ -1596,6 +1608,18 @@ def resampler_foo(resampler: Resampler[pd.DataFrame]) -> pd.DataFrame:
15961608
.pipe(resampler_foo)
15971609
)
15981610

1611+
val = (
1612+
pd.DataFrame(
1613+
{
1614+
"price": [10, 11, 9, 13, 14, 18, 17, 19],
1615+
"volume": [50, 60, 40, 100, 50, 100, 40, 50],
1616+
}
1617+
)
1618+
.assign(week_starting=pd.date_range("01/01/2018", periods=8, freq="W"))
1619+
.resample("MS", on="week_starting")
1620+
.pipe(resampler_foo)
1621+
)
1622+
15991623
def foo(df: pd.DataFrame) -> pd.DataFrame:
16001624
return pd.DataFrame(df)
16011625

@@ -1854,11 +1878,22 @@ def test_types_regressions() -> None:
18541878
d: datetime.date = pd.Timestamp("2021-01-01")
18551879
tslist: list[pd.Timestamp] = list(pd.to_datetime(["2022-01-01", "2022-01-02"]))
18561880
sseries: pd.Series = pd.Series(tslist)
1857-
sseries_plus1: pd.Series = sseries + pd.Timedelta(1, "d")
1881+
with pytest_warns_bounded(FutureWarning, "'d' is deprecated", lower="2.2.99"):
1882+
sseries + pd.Timedelta(1, "d")
1883+
1884+
sseries_plus1: pd.Series = sseries + pd.Timedelta(1, "D")
18581885

18591886
# https://github.com/microsoft/pylance-release/issues/2133
1860-
with pytest_warns_bounded(FutureWarning, "'H' is deprecated", lower="2.1.99"):
1861-
dr = pd.date_range(start="2021-12-01", periods=24, freq="H")
1887+
with pytest_warns_bounded(
1888+
FutureWarning,
1889+
"'H' is deprecated",
1890+
lower="2.1.99",
1891+
upper="2.2.99",
1892+
upper_exception=ValueError,
1893+
):
1894+
pd.date_range(start="2021-12-01", periods=24, freq="H")
1895+
1896+
dr = pd.date_range(start="2021-12-01", periods=24, freq="h")
18621897
time = dr.strftime("%H:%M:%S")
18631898

18641899
# https://github.com/microsoft/python-type-stubs/issues/115
@@ -2885,8 +2920,16 @@ def test_quantile_150_changes() -> None:
28852920
def test_resample_150_changes() -> None:
28862921
idx = pd.date_range("2020-1-1", periods=700)
28872922
frame = pd.DataFrame(np.random.standard_normal((700, 1)), index=idx, columns=["a"])
2888-
with pytest_warns_bounded(FutureWarning, "'M' is deprecated", lower="2.1.99"):
2889-
resampler = frame.resample("M", group_keys=True)
2923+
with pytest_warns_bounded(
2924+
FutureWarning,
2925+
"'M' is deprecated",
2926+
lower="2.1.99",
2927+
upper="2.2.99",
2928+
upper_exception=ValueError,
2929+
):
2930+
frame.resample("M", group_keys=True)
2931+
2932+
resampler = frame.resample("MS", group_keys=True)
28902933
check(
28912934
assert_type(resampler, "DatetimeIndexResampler[pd.DataFrame]"),
28922935
DatetimeIndexResampler,

0 commit comments

Comments
 (0)