Skip to content

Commit 80fbecf

Browse files
GH1055 Add pandas.api.typing to pandas-stubs
1 parent 7f06ab1 commit 80fbecf

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

pandas-stubs/api/typing/__init__.pyi

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Public API classes that store intermediate results useful for type-hinting.
3+
"""
4+
5+
from pandas.core.groupby import (
6+
DataFrameGroupBy as DataFrameGroupBy,
7+
SeriesGroupBy as SeriesGroupBy,
8+
)
9+
from pandas.core.indexes.frozen import FrozenList as FrozenList
10+
from pandas.core.resample import (
11+
DatetimeIndexResamplerGroupby as DatetimeIndexResamplerGroupby,
12+
PeriodIndexResamplerGroupby as PeriodIndexResamplerGroupby,
13+
Resampler as Resampler,
14+
TimedeltaIndexResamplerGroupby as TimedeltaIndexResamplerGroupby,
15+
TimeGrouper as TimeGrouper,
16+
)
17+
from pandas.core.window import (
18+
Expanding as Expanding,
19+
ExpandingGroupby as ExpandingGroupby,
20+
ExponentialMovingWindow as ExponentialMovingWindow,
21+
ExponentialMovingWindowGroupby as ExponentialMovingWindowGroupby,
22+
Rolling as Rolling,
23+
RollingGroupby as RollingGroupby,
24+
Window as Window,
25+
)
26+
27+
from pandas._libs import NaTType as NaTType
28+
from pandas._libs.missing import NAType as NAType
29+
30+
from pandas.io.json._json import JsonReader as JsonReader
31+
32+
# SASReader is not defined so commenting it out for now
33+
# from pandas.io.sas.sasreader import SASReader as SASReader
34+
from pandas.io.stata import StataReader as StataReader

tests/test_api_typing.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Test module for classes in pandas.api.typing."""
2+
3+
from typing import (
4+
TYPE_CHECKING,
5+
Literal,
6+
assert_type,
7+
)
8+
9+
import numpy as np
10+
import pandas as pd
11+
from pandas.api.typing import (
12+
DataFrameGroupBy,
13+
DatetimeIndexResamplerGroupby,
14+
FrozenList,
15+
NAType,
16+
SeriesGroupBy,
17+
)
18+
19+
from pandas._typing import Scalar
20+
21+
from tests import check
22+
23+
if TYPE_CHECKING:
24+
from pandas.core.groupby.groupby import _ResamplerGroupBy # noqa: F401
25+
26+
27+
def test_dataframegroupby():
28+
df = pd.DataFrame({"a": [1, 2, 3]})
29+
check(
30+
assert_type(df.groupby("a"), DataFrameGroupBy[Scalar, Literal[True]]),
31+
DataFrameGroupBy,
32+
)
33+
34+
35+
def test_seriesgroupby():
36+
sr: pd.Series[int] = pd.Series([1, 2, 3], index=pd.Index(["a", "b", "a"]))
37+
check(
38+
assert_type(sr.groupby("a"), SeriesGroupBy[int, Scalar]),
39+
SeriesGroupBy,
40+
)
41+
42+
43+
def test_frozenlist() -> None:
44+
flst = FrozenList([1, 2, 3])
45+
check(assert_type(flst, FrozenList), FrozenList)
46+
47+
48+
def tests_datetimeindexersamplergroupby() -> None:
49+
idx = pd.date_range("1999-1-1", periods=365, freq="D")
50+
df = pd.DataFrame(
51+
np.random.standard_normal((365, 2)), index=idx, columns=["col1", "col2"]
52+
)
53+
gb_df = df.groupby("col2")
54+
check(
55+
assert_type(gb_df.resample("ME"), "_ResamplerGroupBy[pd.DataFrame]"),
56+
DatetimeIndexResamplerGroupby,
57+
pd.DataFrame,
58+
)
59+
60+
61+
def test_natype() -> None:
62+
i64dt = pd.Int64Dtype()
63+
check(assert_type(i64dt.na_value, NAType), NAType)

0 commit comments

Comments
 (0)