()
| 283 | |
| 284 | |
| 285 | def test_pivot_table_errors(): |
| 286 | df = pd.DataFrame( |
| 287 | { |
| 288 | "A": np.random.choice(list("abc"), size=10), |
| 289 | "B": np.random.randn(10), |
| 290 | "C": pd.Categorical(np.random.choice(list("abc"), size=10)), |
| 291 | } |
| 292 | ) |
| 293 | ddf = dd.from_pandas(df, 2) |
| 294 | |
| 295 | msg = "'index' must be the name of an existing column" |
| 296 | with pytest.raises(ValueError) as err: |
| 297 | dd.pivot_table(ddf, index=["A"], columns="C", values="B") |
| 298 | assert msg in str(err.value) |
| 299 | msg = "'columns' must be the name of an existing column" |
| 300 | with pytest.raises(ValueError) as err: |
| 301 | dd.pivot_table(ddf, index="A", columns=["C"], values="B") |
| 302 | assert msg in str(err.value) |
| 303 | msg = "'values' must refer to an existing column or columns" |
| 304 | with pytest.raises(ValueError) as err: |
| 305 | dd.pivot_table(ddf, index="A", columns="C", values=[["B"]]) |
| 306 | assert msg in str(err.value) |
| 307 | |
| 308 | msg = "aggfunc must be either 'mean', 'sum', 'count', 'first', 'last'" |
| 309 | with pytest.raises(ValueError) as err: |
| 310 | dd.pivot_table(ddf, index="A", columns="C", values="B", aggfunc=["sum"]) |
| 311 | assert msg in str(err.value) |
| 312 | |
| 313 | with pytest.raises(ValueError) as err: |
| 314 | dd.pivot_table(ddf, index="A", columns="C", values="B", aggfunc="xx") |
| 315 | assert msg in str(err.value) |
| 316 | |
| 317 | # unknown categories |
| 318 | ddf["C"] = ddf.C.cat.as_unknown() |
| 319 | msg = "'columns' must have known categories" |
| 320 | with pytest.raises(ValueError) as err: |
| 321 | dd.pivot_table(ddf, index="A", columns="C", values=["B"]) |
| 322 | assert msg in str(err.value) |
| 323 | |
| 324 | df = pd.DataFrame( |
| 325 | { |
| 326 | "A": np.random.choice(list("abc"), size=10), |
| 327 | "B": np.random.randn(10), |
| 328 | "C": np.random.choice(list("abc"), size=10), |
| 329 | } |
| 330 | ) |
| 331 | ddf = dd.from_pandas(df, 2) |
| 332 | msg = "'columns' must be category dtype" |
| 333 | with pytest.raises(ValueError) as err: |
| 334 | dd.pivot_table(ddf, index="A", columns="C", values="B") |
| 335 | assert msg in str(err.value) |
nothing calls this directly
no test coverage detected