|
| 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