pandas 未来警告:不赞成使用DataFrame.quantile中numeric_only的默认值,如何修复这样的错误?

wbgh16ku  于 2023-05-12  发布在  其他
关注(0)|答案(2)|浏览(276)

未来警告:不赞成使用DataFrame.quantile中numeric_only的默认值。在未来的版本中,它将默认为False。仅选择有效列或指定numeric_only的值以消除此警告。这个警告不断出现,我不知道如何解决它。
我试着去掉异常值,但它一直给我未来的错误

z9smfwbn

z9smfwbn1#

在pandas版本2.0.0.中,numeric_only将其默认值更改为False,即mentioned in the docs

>>> import pandas as pd
>>> pd.__version__
'2.0.1'
>>> df = pd.DataFrame({'a': [1,3,3,6], 'b': ['Alice', 'Bob', 'Clyde','Debbie']})

我试图在我的机器上重现你的警告(MacOS Ventura,Python 3.18.15),但没有得到任何警告或错误。

>>> df.quantile(numeric_only=True)
a    3.0
Name: 0.5, dtype: float64
>>> df.quantile()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/pandas/core/frame.py", line 10887, in quantile
    res_df = self.quantile(  # type: ignore[call-overload]
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/pandas/core/frame.py", line 10932, in quantile
    res = data._mgr.quantile(qs=q, axis=1, interpolation=interpolation)
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 1587, in quantile
    blocks = [
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 1588, in <listcomp>
    blk.quantile(axis=axis, qs=qs, interpolation=interpolation)
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 1461, in quantile
    result = quantile_compat(self.values, np.asarray(qs._values), interpolation)
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/pandas/core/array_algos/quantile.py", line 37, in quantile_compat
    return quantile_with_mask(values, mask, fill_value, qs, interpolation)
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/pandas/core/array_algos/quantile.py", line 95, in quantile_with_mask
    result = _nanpercentile(
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/pandas/core/array_algos/quantile.py", line 216, in _nanpercentile
    return np.percentile(
  File "<__array_function__ internals>", line 5, in percentile
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/numpy/lib/function_base.py", line 3867, in percentile
    return _quantile_unchecked(
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/numpy/lib/function_base.py", line 3986, in _quantile_unchecked
    r, k = _ureduce(a, func=_quantile_ureduce_func, q=q, axis=axis, out=out,
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/numpy/lib/function_base.py", line 3564, in _ureduce
    r = func(a, **kwargs)
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/numpy/lib/function_base.py", line 4112, in _quantile_ureduce_func
    r = _lerp(x_below, x_above, weights_above, out=out)
  File "/opt/miniconda3/envs/dev/lib/python3.8/site-packages/numpy/lib/function_base.py", line 4009, in _lerp
    diff_b_a = subtract(b, a)
TypeError: unsupported operand type(s) for -: 'str' and 'str'

我可以确认,默认值是False

vvppvyoh

vvppvyoh2#

DataFrame.select_dtypes选择数值列:

df = pd.DataFrame({"col1":list('abcd'),
                  "col2":1,
                  "col3":[1,2,3,3]})

out = df.select_dtypes(np.number).quantile()
print (out)
col2    1.0
col3    2.5
Name: 0.5, dtype: float64

相关问题