| 1435 | ) |
| 1436 | @pytest.mark.parametrize("numeric_only", [None, True, False]) |
| 1437 | def test_dataframe_quantile(method, expected, numeric_only): |
| 1438 | # column X is for test column order and result division |
| 1439 | df = pd.DataFrame( |
| 1440 | { |
| 1441 | "A": np.arange(20), |
| 1442 | "X": np.arange(20, 40), |
| 1443 | "B": np.arange(10, 30), |
| 1444 | "C": ["a", "b", "c", "d"] * 5, |
| 1445 | }, |
| 1446 | columns=["A", "X", "B", "C"], |
| 1447 | ) |
| 1448 | ddf = dd.from_pandas(df, 3) |
| 1449 | |
| 1450 | numeric_only_kwarg = {} |
| 1451 | if numeric_only is not None: |
| 1452 | numeric_only_kwarg = {"numeric_only": numeric_only} |
| 1453 | |
| 1454 | if numeric_only is False or numeric_only is None: |
| 1455 | # TypeError for pandas<3, ArrowNotImplementedError for pandas>=3 |
| 1456 | with pytest.raises((TypeError, ArrowNotImplementedError)): |
| 1457 | df.quantile(**numeric_only_kwarg) |
| 1458 | with pytest.raises( |
| 1459 | (TypeError, ArrowNotImplementedError, ValueError), |
| 1460 | match="unsupported operand|no kernel|non-numeric|not supported", |
| 1461 | ): |
| 1462 | ddf.quantile(**numeric_only_kwarg) |
| 1463 | else: |
| 1464 | result = ddf.quantile(method=method, **numeric_only_kwarg) |
| 1465 | assert result.npartitions == 1 |
| 1466 | assert result.divisions == ("A", "X") |
| 1467 | |
| 1468 | result = result.compute() |
| 1469 | assert isinstance(result, pd.Series) |
| 1470 | assert result.name == 0.5 |
| 1471 | assert_eq(result, expected[0], check_names=False) |
| 1472 | |
| 1473 | result = ddf.quantile([0.25, 0.75], method=method, **numeric_only_kwarg) |
| 1474 | assert result.npartitions == 1 |
| 1475 | assert result.divisions == (0.25, 0.75) |
| 1476 | |
| 1477 | result = result.compute() |
| 1478 | assert isinstance(result, pd.DataFrame) |
| 1479 | tm.assert_index_equal(result.index, pd.Index([0.25, 0.75])) |
| 1480 | tm.assert_index_equal(result.columns, pd.Index(["A", "X", "B"])) |
| 1481 | |
| 1482 | assert (result == expected[1]).all().all() |
| 1483 | |
| 1484 | with warnings.catch_warnings(record=True): |
| 1485 | warnings.filterwarnings("ignore", category=FutureWarning) |
| 1486 | # pandas issues a warning with 1.5, but not 1.3 |
| 1487 | expected = df.quantile(axis=1, **numeric_only_kwarg) |
| 1488 | |
| 1489 | result = ddf.quantile(axis=1, method=method, **numeric_only_kwarg) |
| 1490 | |
| 1491 | assert_eq(result, expected) |
| 1492 | |
| 1493 | with pytest.raises(ValueError): |
| 1494 | ddf.quantile([0.25, 0.75], axis=1, method=method, **numeric_only_kwarg) |