|
9 | 9 | Dict, |
10 | 10 | Hashable, |
11 | 11 | Iterable, |
| 12 | + Iterator, |
12 | 13 | List, |
13 | 14 | Tuple, |
14 | 15 | Union, |
|
20 | 21 | import pytest |
21 | 22 | from typing_extensions import assert_type |
22 | 23 |
|
| 24 | +from pandas._typing import Scalar |
| 25 | + |
23 | 26 | from tests import check |
24 | 27 |
|
25 | 28 | from pandas.io.parsers import TextFileReader |
@@ -1251,3 +1254,30 @@ def test_boolean_loc() -> None: |
1251 | 1254 | df = pd.DataFrame([[0, 1], [1, 0]], columns=[True, False], index=[True, False]) |
1252 | 1255 | check(assert_type(df.loc[True], pd.Series), pd.Series) |
1253 | 1256 | check(assert_type(df.loc[:, False], pd.Series), pd.Series) |
| 1257 | + |
| 1258 | + |
| 1259 | +def test_groupby_result() -> None: |
| 1260 | + # GH 142 |
| 1261 | + df = pd.DataFrame({"a": [0, 1, 2], "b": [4, 5, 6], "c": [7, 8, 9]}) |
| 1262 | + iterator = df.groupby(["a", "b"]).__iter__() |
| 1263 | + assert_type(iterator, Iterator[Tuple[Tuple, pd.DataFrame]]) |
| 1264 | + index, value = next(iterator) |
| 1265 | + assert_type((index, value), Tuple[Tuple, pd.DataFrame]) |
| 1266 | + |
| 1267 | + check(assert_type(index, Tuple), tuple, np.int64) |
| 1268 | + check(assert_type(value, pd.DataFrame), pd.DataFrame) |
| 1269 | + |
| 1270 | + iterator2 = df.groupby("a").__iter__() |
| 1271 | + assert_type(iterator2, Iterator[Tuple[Scalar, pd.DataFrame]]) |
| 1272 | + index2, value2 = next(iterator2) |
| 1273 | + assert_type((index2, value2), Tuple[Scalar, pd.DataFrame]) |
| 1274 | + |
| 1275 | + check(assert_type(index2, Scalar), int) |
| 1276 | + check(assert_type(value2, pd.DataFrame), pd.DataFrame) |
| 1277 | + |
| 1278 | + # Want to make sure these cases are differentiated |
| 1279 | + for (k1, k2), g in df.groupby(["a", "b"]): |
| 1280 | + pass |
| 1281 | + |
| 1282 | + for kk, g in df.groupby("a"): |
| 1283 | + pass |
0 commit comments