MCPcopy
hub / github.com/pandas-dev/pandas / test_quantile

Function test_quantile

pandas/tests/extension/test_arrow.py:1367–1437  ·  view source on GitHub ↗
(data, interpolation, quantile, request)

Source from the content-addressed store, hash-verified

1365)
1366@pytest.mark.parametrize("quantile", [0.5, [0.5, 0.5]])
1367def test_quantile(data, interpolation, quantile, request):
1368 pa_dtype = data.dtype.pyarrow_dtype
1369
1370 data = data.take([0, 0, 0])
1371 ser = pd.Series(data)
1372
1373 if (
1374 pa.types.is_string(pa_dtype)
1375 or pa.types.is_binary(pa_dtype)
1376 or pa.types.is_boolean(pa_dtype)
1377 ):
1378 # For string, bytes, and bool, we don't *expect* to have quantile work
1379 # Note this matches the non-pyarrow behavior
1380 msg = r"Function 'quantile' has no kernel matching input types \(.*\)"
1381 with pytest.raises(pa.ArrowNotImplementedError, match=msg):
1382 ser.quantile(q=quantile, interpolation=interpolation)
1383 return
1384
1385 if (
1386 pa.types.is_integer(pa_dtype)
1387 or pa.types.is_floating(pa_dtype)
1388 or pa.types.is_decimal(pa_dtype)
1389 ):
1390 pass
1391 elif pa.types.is_temporal(data._pa_array.type):
1392 pass
1393 else:
1394 request.applymarker(
1395 pytest.mark.xfail(
1396 raises=pa.ArrowNotImplementedError,
1397 reason=f"quantile not supported by pyarrow for {pa_dtype}",
1398 )
1399 )
1400 data = data.take([0, 0, 0])
1401 ser = pd.Series(data)
1402 result = ser.quantile(q=quantile, interpolation=interpolation)
1403
1404 if pa.types.is_timestamp(pa_dtype) and interpolation not in ["lower", "higher"]:
1405 # rounding error will make the check below fail
1406 # (e.g. '2020-01-01 01:01:01.000001' vs '2020-01-01 01:01:01.000001024'),
1407 # so we'll check for now that we match the numpy analogue
1408 if pa_dtype.tz:
1409 pd_dtype = f"M8[{pa_dtype.unit}, {pa_dtype.tz}]"
1410 else:
1411 pd_dtype = f"M8[{pa_dtype.unit}]"
1412 ser_np = ser.astype(pd_dtype)
1413
1414 expected = ser_np.quantile(q=quantile, interpolation=interpolation)
1415 if quantile == 0.5:
1416 if pa_dtype.unit == "us":
1417 expected = expected.to_pydatetime(warn=False)
1418 assert result == expected
1419 else:
1420 if pa_dtype.unit == "us":
1421 expected = expected.dt.floor("us")
1422 tm.assert_series_equal(result, expected.astype(data.dtype))
1423 return
1424

Callers

nothing calls this directly

Calls 6

quantileMethod · 0.95
floorMethod · 0.80
takeMethod · 0.45
raisesMethod · 0.45
astypeMethod · 0.45
to_pydatetimeMethod · 0.45

Tested by

no test coverage detected