gpt4 book ai didi

python - pandas `to_numeric` integer downcast cast floats not to integer

转载 作者:行者123 更新时间:2023-12-02 18:10:06 26 4
gpt4 key购买 nike

使用这个示例数据框:

>>> d = pd.DataFrame({'si': ['1', '2', 'NA'], 's': ['a', 'b', 'c']})

>>> d.dtypes
#
si object
s object
dtype: object

我的第一次尝试是使用 astype 和 'Int64' NA 感知 int 类型,但我得到了一个

回溯

>>> d.si.astype('Int64')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-144-ed289e0c95aa> in <module>
----> 1 d.si.astype('Int64')
...

然后我尝试使用 to_numeric 方法:

pandas to_numeric 整数向下转换 float

In [112]: d.loc[:, 'ii'] = pd.to_numeric(d.si, errors='coerce', downcast='integer')

In [113]: d.dtypes
Out[113]:
si object
s object
ii float64
dtype: object

In [114]: d
Out[114]:
si s ii
0 1 a 1.0
1 2 b 2.0
2 NA c NA

在上面,我希望 ii 列包含整数和整数 nan

文档说:

downcast : {'integer', 'signed', 'unsigned', 'float'}, default None
If not None, and if the data has been successfully cast to a
numerical dtype (or if the data was numeric to begin with),
downcast that resulting data to the smallest numerical dtype
possible according to the following rules:

- 'integer' or 'signed': smallest signed int dtype (min.: np.int8)
- 'unsigned': smallest unsigned int dtype (min.: np.uint8)
- 'float': smallest float dtype (min.: np.float32)

最佳答案

不幸的是,pandas 仍在适应/过渡到完全支持整数 NaN。为此,您必须在 pd.to_numeric 操作后将其显式转换为 Int64

无需沮丧。

# Can also use `'Int64' as dtype below.
>>> pd.to_numeric(df['col'], errors='coerce').astype(pd.Int64Dtype())

# or

>>> pd.to_numeric(df['col'], errors='coerce').astype('Int64')

0       1
1 2
2 3
3 <NA>
Name: col, dtype: Int64

关于python - pandas `to_numeric` integer downcast cast floats not to integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72520974/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com