test_raises.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. # Only tests that raise an error and have no better location should go here.
  2. # Tests for specific groupby methods should go in their respective
  3. # test file.
  4. import datetime
  5. import re
  6. import numpy as np
  7. import pytest
  8. from pandas import (
  9. Categorical,
  10. DataFrame,
  11. Grouper,
  12. Series,
  13. )
  14. import pandas._testing as tm
  15. from pandas.tests.groupby import get_groupby_method_args
  16. @pytest.fixture(
  17. params=[
  18. "a",
  19. ["a"],
  20. ["a", "b"],
  21. Grouper(key="a"),
  22. lambda x: x % 2,
  23. [0, 0, 0, 1, 2, 2, 2, 3, 3],
  24. np.array([0, 0, 0, 1, 2, 2, 2, 3, 3]),
  25. dict(zip(range(9), [0, 0, 0, 1, 2, 2, 2, 3, 3])),
  26. Series([1, 1, 1, 1, 1, 2, 2, 2, 2]),
  27. [Series([1, 1, 1, 1, 1, 2, 2, 2, 2]), Series([3, 3, 4, 4, 4, 4, 4, 3, 3])],
  28. ]
  29. )
  30. def by(request):
  31. return request.param
  32. @pytest.fixture(params=[True, False])
  33. def groupby_series(request):
  34. return request.param
  35. @pytest.fixture
  36. def df_with_string_col():
  37. df = DataFrame(
  38. {
  39. "a": [1, 1, 1, 1, 1, 2, 2, 2, 2],
  40. "b": [3, 3, 4, 4, 4, 4, 4, 3, 3],
  41. "c": range(9),
  42. "d": list("xyzwtyuio"),
  43. }
  44. )
  45. return df
  46. @pytest.fixture
  47. def df_with_datetime_col():
  48. df = DataFrame(
  49. {
  50. "a": [1, 1, 1, 1, 1, 2, 2, 2, 2],
  51. "b": [3, 3, 4, 4, 4, 4, 4, 3, 3],
  52. "c": range(9),
  53. "d": datetime.datetime(2005, 1, 1, 10, 30, 23, 540000),
  54. }
  55. )
  56. return df
  57. @pytest.fixture
  58. def df_with_timedelta_col():
  59. df = DataFrame(
  60. {
  61. "a": [1, 1, 1, 1, 1, 2, 2, 2, 2],
  62. "b": [3, 3, 4, 4, 4, 4, 4, 3, 3],
  63. "c": range(9),
  64. "d": datetime.timedelta(days=1),
  65. }
  66. )
  67. return df
  68. @pytest.fixture
  69. def df_with_cat_col():
  70. df = DataFrame(
  71. {
  72. "a": [1, 1, 1, 1, 1, 2, 2, 2, 2],
  73. "b": [3, 3, 4, 4, 4, 4, 4, 3, 3],
  74. "c": range(9),
  75. "d": Categorical(
  76. ["a", "a", "a", "a", "b", "b", "b", "b", "c"],
  77. categories=["a", "b", "c", "d"],
  78. ordered=True,
  79. ),
  80. }
  81. )
  82. return df
  83. def _call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg=""):
  84. warn_klass = None if warn_msg == "" else FutureWarning
  85. with tm.assert_produces_warning(warn_klass, match=warn_msg):
  86. if klass is None:
  87. if how == "method":
  88. getattr(gb, groupby_func)(*args)
  89. elif how == "agg":
  90. gb.agg(groupby_func, *args)
  91. else:
  92. gb.transform(groupby_func, *args)
  93. else:
  94. with pytest.raises(klass, match=msg):
  95. if how == "method":
  96. getattr(gb, groupby_func)(*args)
  97. elif how == "agg":
  98. gb.agg(groupby_func, *args)
  99. else:
  100. gb.transform(groupby_func, *args)
  101. @pytest.mark.parametrize("how", ["method", "agg", "transform"])
  102. def test_groupby_raises_string(
  103. how, by, groupby_series, groupby_func, df_with_string_col, using_infer_string
  104. ):
  105. df = df_with_string_col
  106. args = get_groupby_method_args(groupby_func, df)
  107. gb = df.groupby(by=by)
  108. if groupby_series:
  109. gb = gb["d"]
  110. if groupby_func == "corrwith":
  111. assert not hasattr(gb, "corrwith")
  112. return
  113. klass, msg = {
  114. "all": (None, ""),
  115. "any": (None, ""),
  116. "bfill": (None, ""),
  117. "corrwith": (TypeError, "Could not convert"),
  118. "count": (None, ""),
  119. "cumcount": (None, ""),
  120. "cummax": (
  121. (NotImplementedError, TypeError),
  122. "(function|cummax) is not (implemented|supported) for (this|object) dtype",
  123. ),
  124. "cummin": (
  125. (NotImplementedError, TypeError),
  126. "(function|cummin) is not (implemented|supported) for (this|object) dtype",
  127. ),
  128. "cumprod": (
  129. (NotImplementedError, TypeError),
  130. "(function|cumprod) is not (implemented|supported) for (this|object) dtype",
  131. ),
  132. "cumsum": (
  133. (NotImplementedError, TypeError),
  134. "(function|cumsum) is not (implemented|supported) for (this|object) dtype",
  135. ),
  136. "diff": (TypeError, "unsupported operand type"),
  137. "ffill": (None, ""),
  138. "fillna": (None, ""),
  139. "first": (None, ""),
  140. "idxmax": (None, ""),
  141. "idxmin": (None, ""),
  142. "last": (None, ""),
  143. "max": (None, ""),
  144. "mean": (
  145. TypeError,
  146. re.escape("agg function failed [how->mean,dtype->object]"),
  147. ),
  148. "median": (
  149. TypeError,
  150. re.escape("agg function failed [how->median,dtype->object]"),
  151. ),
  152. "min": (None, ""),
  153. "ngroup": (None, ""),
  154. "nunique": (None, ""),
  155. "pct_change": (TypeError, "unsupported operand type"),
  156. "prod": (
  157. TypeError,
  158. re.escape("agg function failed [how->prod,dtype->object]"),
  159. ),
  160. "quantile": (TypeError, "dtype 'object' does not support operation 'quantile'"),
  161. "rank": (None, ""),
  162. "sem": (ValueError, "could not convert string to float"),
  163. "shift": (None, ""),
  164. "size": (None, ""),
  165. "skew": (ValueError, "could not convert string to float"),
  166. "std": (ValueError, "could not convert string to float"),
  167. "sum": (None, ""),
  168. "var": (
  169. TypeError,
  170. re.escape("agg function failed [how->var,dtype->"),
  171. ),
  172. }[groupby_func]
  173. if using_infer_string:
  174. if groupby_func in [
  175. "prod",
  176. "mean",
  177. "median",
  178. "cumsum",
  179. "cumprod",
  180. "std",
  181. "sem",
  182. "var",
  183. "skew",
  184. "quantile",
  185. ]:
  186. msg = f"dtype 'str' does not support operation '{groupby_func}'"
  187. if groupby_func in ["sem", "std", "skew"]:
  188. # The object-dtype raises ValueError when trying to convert to numeric.
  189. klass = TypeError
  190. elif groupby_func == "pct_change" and df["d"].dtype.storage == "pyarrow":
  191. # This doesn't go through EA._groupby_op so the message isn't controlled
  192. # there.
  193. msg = "operation 'truediv' not supported for dtype 'str' with dtype 'str'"
  194. elif groupby_func == "diff" and df["d"].dtype.storage == "pyarrow":
  195. # This doesn't go through EA._groupby_op so the message isn't controlled
  196. # there.
  197. msg = "operation 'sub' not supported for dtype 'str' with dtype 'str'"
  198. elif groupby_func in ["cummin", "cummax"]:
  199. msg = msg.replace("object", "str")
  200. elif groupby_func == "corrwith":
  201. msg = "Cannot perform reduction 'mean' with string dtype"
  202. if groupby_func == "fillna":
  203. kind = "Series" if groupby_series else "DataFrame"
  204. warn_msg = f"{kind}GroupBy.fillna is deprecated"
  205. else:
  206. warn_msg = ""
  207. _call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg)
  208. @pytest.mark.parametrize("how", ["agg", "transform"])
  209. def test_groupby_raises_string_udf(how, by, groupby_series, df_with_string_col):
  210. df = df_with_string_col
  211. gb = df.groupby(by=by)
  212. if groupby_series:
  213. gb = gb["d"]
  214. def func(x):
  215. raise TypeError("Test error message")
  216. with pytest.raises(TypeError, match="Test error message"):
  217. getattr(gb, how)(func)
  218. @pytest.mark.parametrize("how", ["agg", "transform"])
  219. @pytest.mark.parametrize("groupby_func_np", [np.sum, np.mean])
  220. def test_groupby_raises_string_np(
  221. how,
  222. by,
  223. groupby_series,
  224. groupby_func_np,
  225. df_with_string_col,
  226. using_infer_string,
  227. ):
  228. # GH#50749
  229. df = df_with_string_col
  230. gb = df.groupby(by=by)
  231. if groupby_series:
  232. gb = gb["d"]
  233. klass, msg = {
  234. np.sum: (None, ""),
  235. np.mean: (
  236. TypeError,
  237. "agg function failed|Cannot perform reduction 'mean' with string dtype",
  238. ),
  239. }[groupby_func_np]
  240. if using_infer_string:
  241. if groupby_func_np is np.mean:
  242. klass = TypeError
  243. msg = "dtype 'str' does not support operation 'mean'"
  244. if groupby_series:
  245. warn_msg = "using SeriesGroupBy.[sum|mean]"
  246. else:
  247. warn_msg = "using DataFrameGroupBy.[sum|mean]"
  248. _call_and_check(klass, msg, how, gb, groupby_func_np, (), warn_msg=warn_msg)
  249. @pytest.mark.parametrize("how", ["method", "agg", "transform"])
  250. def test_groupby_raises_datetime(
  251. how, by, groupby_series, groupby_func, df_with_datetime_col
  252. ):
  253. df = df_with_datetime_col
  254. args = get_groupby_method_args(groupby_func, df)
  255. gb = df.groupby(by=by)
  256. if groupby_series:
  257. gb = gb["d"]
  258. if groupby_func == "corrwith":
  259. assert not hasattr(gb, "corrwith")
  260. return
  261. klass, msg = {
  262. "all": (None, ""),
  263. "any": (None, ""),
  264. "bfill": (None, ""),
  265. "corrwith": (TypeError, "cannot perform __mul__ with this index type"),
  266. "count": (None, ""),
  267. "cumcount": (None, ""),
  268. "cummax": (None, ""),
  269. "cummin": (None, ""),
  270. "cumprod": (TypeError, "datetime64 type does not support cumprod operations"),
  271. "cumsum": (TypeError, "datetime64 type does not support cumsum operations"),
  272. "diff": (None, ""),
  273. "ffill": (None, ""),
  274. "fillna": (None, ""),
  275. "first": (None, ""),
  276. "idxmax": (None, ""),
  277. "idxmin": (None, ""),
  278. "last": (None, ""),
  279. "max": (None, ""),
  280. "mean": (None, ""),
  281. "median": (None, ""),
  282. "min": (None, ""),
  283. "ngroup": (None, ""),
  284. "nunique": (None, ""),
  285. "pct_change": (TypeError, "cannot perform __truediv__ with this index type"),
  286. "prod": (TypeError, "datetime64 type does not support prod"),
  287. "quantile": (None, ""),
  288. "rank": (None, ""),
  289. "sem": (None, ""),
  290. "shift": (None, ""),
  291. "size": (None, ""),
  292. "skew": (
  293. TypeError,
  294. "|".join(
  295. [
  296. r"dtype datetime64\[ns\] does not support reduction",
  297. "datetime64 type does not support skew operations",
  298. ]
  299. ),
  300. ),
  301. "std": (None, ""),
  302. "sum": (TypeError, "datetime64 type does not support sum operations"),
  303. "var": (TypeError, "datetime64 type does not support var operations"),
  304. }[groupby_func]
  305. if groupby_func in ["any", "all"]:
  306. warn_msg = f"'{groupby_func}' with datetime64 dtypes is deprecated"
  307. elif groupby_func == "fillna":
  308. kind = "Series" if groupby_series else "DataFrame"
  309. warn_msg = f"{kind}GroupBy.fillna is deprecated"
  310. else:
  311. warn_msg = ""
  312. _call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg=warn_msg)
  313. @pytest.mark.parametrize("how", ["agg", "transform"])
  314. def test_groupby_raises_datetime_udf(how, by, groupby_series, df_with_datetime_col):
  315. df = df_with_datetime_col
  316. gb = df.groupby(by=by)
  317. if groupby_series:
  318. gb = gb["d"]
  319. def func(x):
  320. raise TypeError("Test error message")
  321. with pytest.raises(TypeError, match="Test error message"):
  322. getattr(gb, how)(func)
  323. @pytest.mark.parametrize("how", ["agg", "transform"])
  324. @pytest.mark.parametrize("groupby_func_np", [np.sum, np.mean])
  325. def test_groupby_raises_datetime_np(
  326. how, by, groupby_series, groupby_func_np, df_with_datetime_col
  327. ):
  328. # GH#50749
  329. df = df_with_datetime_col
  330. gb = df.groupby(by=by)
  331. if groupby_series:
  332. gb = gb["d"]
  333. klass, msg = {
  334. np.sum: (TypeError, "datetime64 type does not support sum operations"),
  335. np.mean: (None, ""),
  336. }[groupby_func_np]
  337. if groupby_series:
  338. warn_msg = "using SeriesGroupBy.[sum|mean]"
  339. else:
  340. warn_msg = "using DataFrameGroupBy.[sum|mean]"
  341. _call_and_check(klass, msg, how, gb, groupby_func_np, (), warn_msg=warn_msg)
  342. @pytest.mark.parametrize("func", ["prod", "cumprod", "skew", "var"])
  343. def test_groupby_raises_timedelta(func, df_with_timedelta_col):
  344. df = df_with_timedelta_col
  345. gb = df.groupby(by="a")
  346. _call_and_check(
  347. TypeError,
  348. "timedelta64 type does not support .* operations",
  349. "method",
  350. gb,
  351. func,
  352. [],
  353. )
  354. @pytest.mark.parametrize("how", ["method", "agg", "transform"])
  355. def test_groupby_raises_category(
  356. how, by, groupby_series, groupby_func, using_copy_on_write, df_with_cat_col
  357. ):
  358. # GH#50749
  359. df = df_with_cat_col
  360. args = get_groupby_method_args(groupby_func, df)
  361. gb = df.groupby(by=by)
  362. if groupby_series:
  363. gb = gb["d"]
  364. if groupby_func == "corrwith":
  365. assert not hasattr(gb, "corrwith")
  366. return
  367. klass, msg = {
  368. "all": (None, ""),
  369. "any": (None, ""),
  370. "bfill": (None, ""),
  371. "corrwith": (
  372. TypeError,
  373. r"unsupported operand type\(s\) for \*: 'Categorical' and 'int'",
  374. ),
  375. "count": (None, ""),
  376. "cumcount": (None, ""),
  377. "cummax": (
  378. (NotImplementedError, TypeError),
  379. "(category type does not support cummax operations|"
  380. "category dtype not supported|"
  381. "cummax is not supported for category dtype)",
  382. ),
  383. "cummin": (
  384. (NotImplementedError, TypeError),
  385. "(category type does not support cummin operations|"
  386. "category dtype not supported|"
  387. "cummin is not supported for category dtype)",
  388. ),
  389. "cumprod": (
  390. (NotImplementedError, TypeError),
  391. "(category type does not support cumprod operations|"
  392. "category dtype not supported|"
  393. "cumprod is not supported for category dtype)",
  394. ),
  395. "cumsum": (
  396. (NotImplementedError, TypeError),
  397. "(category type does not support cumsum operations|"
  398. "category dtype not supported|"
  399. "cumsum is not supported for category dtype)",
  400. ),
  401. "diff": (
  402. TypeError,
  403. r"unsupported operand type\(s\) for -: 'Categorical' and 'Categorical'",
  404. ),
  405. "ffill": (None, ""),
  406. "fillna": (
  407. TypeError,
  408. r"Cannot setitem on a Categorical with a new category \(0\), "
  409. "set the categories first",
  410. )
  411. if not using_copy_on_write
  412. else (None, ""), # no-op with CoW
  413. "first": (None, ""),
  414. "idxmax": (None, ""),
  415. "idxmin": (None, ""),
  416. "last": (None, ""),
  417. "max": (None, ""),
  418. "mean": (
  419. TypeError,
  420. "|".join(
  421. [
  422. "'Categorical' .* does not support reduction 'mean'",
  423. "category dtype does not support aggregation 'mean'",
  424. ]
  425. ),
  426. ),
  427. "median": (
  428. TypeError,
  429. "|".join(
  430. [
  431. "'Categorical' .* does not support reduction 'median'",
  432. "category dtype does not support aggregation 'median'",
  433. ]
  434. ),
  435. ),
  436. "min": (None, ""),
  437. "ngroup": (None, ""),
  438. "nunique": (None, ""),
  439. "pct_change": (
  440. TypeError,
  441. r"unsupported operand type\(s\) for /: 'Categorical' and 'Categorical'",
  442. ),
  443. "prod": (TypeError, "category type does not support prod operations"),
  444. "quantile": (TypeError, "No matching signature found"),
  445. "rank": (None, ""),
  446. "sem": (
  447. TypeError,
  448. "|".join(
  449. [
  450. "'Categorical' .* does not support reduction 'sem'",
  451. "category dtype does not support aggregation 'sem'",
  452. ]
  453. ),
  454. ),
  455. "shift": (None, ""),
  456. "size": (None, ""),
  457. "skew": (
  458. TypeError,
  459. "|".join(
  460. [
  461. "dtype category does not support reduction 'skew'",
  462. "category type does not support skew operations",
  463. ]
  464. ),
  465. ),
  466. "std": (
  467. TypeError,
  468. "|".join(
  469. [
  470. "'Categorical' .* does not support reduction 'std'",
  471. "category dtype does not support aggregation 'std'",
  472. ]
  473. ),
  474. ),
  475. "sum": (TypeError, "category type does not support sum operations"),
  476. "var": (
  477. TypeError,
  478. "|".join(
  479. [
  480. "'Categorical' .* does not support reduction 'var'",
  481. "category dtype does not support aggregation 'var'",
  482. ]
  483. ),
  484. ),
  485. }[groupby_func]
  486. if groupby_func == "fillna":
  487. kind = "Series" if groupby_series else "DataFrame"
  488. warn_msg = f"{kind}GroupBy.fillna is deprecated"
  489. else:
  490. warn_msg = ""
  491. _call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg)
  492. @pytest.mark.parametrize("how", ["agg", "transform"])
  493. def test_groupby_raises_category_udf(how, by, groupby_series, df_with_cat_col):
  494. # GH#50749
  495. df = df_with_cat_col
  496. gb = df.groupby(by=by)
  497. if groupby_series:
  498. gb = gb["d"]
  499. def func(x):
  500. raise TypeError("Test error message")
  501. with pytest.raises(TypeError, match="Test error message"):
  502. getattr(gb, how)(func)
  503. @pytest.mark.parametrize("how", ["agg", "transform"])
  504. @pytest.mark.parametrize("groupby_func_np", [np.sum, np.mean])
  505. def test_groupby_raises_category_np(
  506. how, by, groupby_series, groupby_func_np, df_with_cat_col
  507. ):
  508. # GH#50749
  509. df = df_with_cat_col
  510. gb = df.groupby(by=by)
  511. if groupby_series:
  512. gb = gb["d"]
  513. klass, msg = {
  514. np.sum: (TypeError, "category type does not support sum operations"),
  515. np.mean: (
  516. TypeError,
  517. "category dtype does not support aggregation 'mean'",
  518. ),
  519. }[groupby_func_np]
  520. if groupby_series:
  521. warn_msg = "using SeriesGroupBy.[sum|mean]"
  522. else:
  523. warn_msg = "using DataFrameGroupBy.[sum|mean]"
  524. _call_and_check(klass, msg, how, gb, groupby_func_np, (), warn_msg=warn_msg)
  525. @pytest.mark.parametrize("how", ["method", "agg", "transform"])
  526. def test_groupby_raises_category_on_category(
  527. how,
  528. by,
  529. groupby_series,
  530. groupby_func,
  531. observed,
  532. using_copy_on_write,
  533. df_with_cat_col,
  534. ):
  535. # GH#50749
  536. df = df_with_cat_col
  537. df["a"] = Categorical(
  538. ["a", "a", "a", "a", "b", "b", "b", "b", "c"],
  539. categories=["a", "b", "c", "d"],
  540. ordered=True,
  541. )
  542. args = get_groupby_method_args(groupby_func, df)
  543. gb = df.groupby(by=by, observed=observed)
  544. if groupby_series:
  545. gb = gb["d"]
  546. if groupby_func == "corrwith":
  547. assert not hasattr(gb, "corrwith")
  548. return
  549. empty_groups = not observed and any(group.empty for group in gb.groups.values())
  550. if (
  551. not observed
  552. and how != "transform"
  553. and isinstance(by, list)
  554. and isinstance(by[0], str)
  555. and by == ["a", "b"]
  556. ):
  557. assert not empty_groups
  558. # TODO: empty_groups should be true due to unobserved categorical combinations
  559. empty_groups = True
  560. if how == "transform":
  561. # empty groups will be ignored
  562. empty_groups = False
  563. klass, msg = {
  564. "all": (None, ""),
  565. "any": (None, ""),
  566. "bfill": (None, ""),
  567. "corrwith": (
  568. TypeError,
  569. r"unsupported operand type\(s\) for \*: 'Categorical' and 'int'",
  570. ),
  571. "count": (None, ""),
  572. "cumcount": (None, ""),
  573. "cummax": (
  574. (NotImplementedError, TypeError),
  575. "(cummax is not supported for category dtype|"
  576. "category dtype not supported|"
  577. "category type does not support cummax operations)",
  578. ),
  579. "cummin": (
  580. (NotImplementedError, TypeError),
  581. "(cummin is not supported for category dtype|"
  582. "category dtype not supported|"
  583. "category type does not support cummin operations)",
  584. ),
  585. "cumprod": (
  586. (NotImplementedError, TypeError),
  587. "(cumprod is not supported for category dtype|"
  588. "category dtype not supported|"
  589. "category type does not support cumprod operations)",
  590. ),
  591. "cumsum": (
  592. (NotImplementedError, TypeError),
  593. "(cumsum is not supported for category dtype|"
  594. "category dtype not supported|"
  595. "category type does not support cumsum operations)",
  596. ),
  597. "diff": (TypeError, "unsupported operand type"),
  598. "ffill": (None, ""),
  599. "fillna": (
  600. TypeError,
  601. r"Cannot setitem on a Categorical with a new category \(0\), "
  602. "set the categories first",
  603. )
  604. if not using_copy_on_write
  605. else (None, ""), # no-op with CoW
  606. "first": (None, ""),
  607. "idxmax": (ValueError, "empty group due to unobserved categories")
  608. if empty_groups
  609. else (None, ""),
  610. "idxmin": (ValueError, "empty group due to unobserved categories")
  611. if empty_groups
  612. else (None, ""),
  613. "last": (None, ""),
  614. "max": (None, ""),
  615. "mean": (TypeError, "category dtype does not support aggregation 'mean'"),
  616. "median": (TypeError, "category dtype does not support aggregation 'median'"),
  617. "min": (None, ""),
  618. "ngroup": (None, ""),
  619. "nunique": (None, ""),
  620. "pct_change": (TypeError, "unsupported operand type"),
  621. "prod": (TypeError, "category type does not support prod operations"),
  622. "quantile": (TypeError, "No matching signature found"),
  623. "rank": (None, ""),
  624. "sem": (
  625. TypeError,
  626. "|".join(
  627. [
  628. "'Categorical' .* does not support reduction 'sem'",
  629. "category dtype does not support aggregation 'sem'",
  630. ]
  631. ),
  632. ),
  633. "shift": (None, ""),
  634. "size": (None, ""),
  635. "skew": (
  636. TypeError,
  637. "|".join(
  638. [
  639. "category type does not support skew operations",
  640. "dtype category does not support reduction 'skew'",
  641. ]
  642. ),
  643. ),
  644. "std": (
  645. TypeError,
  646. "|".join(
  647. [
  648. "'Categorical' .* does not support reduction 'std'",
  649. "category dtype does not support aggregation 'std'",
  650. ]
  651. ),
  652. ),
  653. "sum": (TypeError, "category type does not support sum operations"),
  654. "var": (
  655. TypeError,
  656. "|".join(
  657. [
  658. "'Categorical' .* does not support reduction 'var'",
  659. "category dtype does not support aggregation 'var'",
  660. ]
  661. ),
  662. ),
  663. }[groupby_func]
  664. if groupby_func == "fillna":
  665. kind = "Series" if groupby_series else "DataFrame"
  666. warn_msg = f"{kind}GroupBy.fillna is deprecated"
  667. else:
  668. warn_msg = ""
  669. _call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg)
  670. def test_subsetting_columns_axis_1_raises():
  671. # GH 35443
  672. df = DataFrame({"a": [1], "b": [2], "c": [3]})
  673. msg = "DataFrame.groupby with axis=1 is deprecated"
  674. with tm.assert_produces_warning(FutureWarning, match=msg):
  675. gb = df.groupby("a", axis=1)
  676. with pytest.raises(ValueError, match="Cannot subset columns when using axis=1"):
  677. gb["b"]