pandas 报告系列是小数时的对象 [英] Pandas reporting series to be an object when it's a decimal

查看:34
本文介绍了 pandas 报告系列是小数时的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种自动可靠的方法来查找 Pandas 数据框中每一列的数据类型.我一直在使用 .dtype() 但注意到它有一些意想不到的东西.

I need an automated reliable way to find the data type of each column in a pandas data frame. I have been using .dtype() but have noticed something unexpected with it.

考虑这个 10 行数据框:

Consider this 10 row data frame:

df['a']
Out[6]: 
0    250.00
1    750.00
2      0.00
3      0.00
4      0.00
5      0.00
6      0.00
7      0.00
8      0.00
9      0.00
Name: a, dtype: object

type(df['a'][0])
Out[9]: decimal.Decimal

当每个条目都是小数时,为什么整列的 dtype 是对象"?我真的需要它来表示十进制或浮点数或数字.任何帮助,将不胜感激!

Why is the dtype of the entire column an 'object' when each entry is a decimal? I really need it to say decimal or float or something numeric. Any help would be appreciated!

推荐答案

这不是错误,而是由于 numpy dtype 表示:https://docs.scipy.org/doc/numpy/reference/arrays.scalars.html.

This is not an error but is due to the numpy dtype representation: https://docs.scipy.org/doc/numpy/reference/arrays.scalars.html.

基本上因为 Decimal 不是一个原则内置类型,那么它的 dtype 最终是 object 即使每个单元格的实际类型仍然是 Decimal>.

Basically as Decimal is not a principle inbuilt type then it's dtype ends up being object even though the actual type of each cell is still Decimal.

建议尽可能使用内置标量类型,在本例中为 float64,因为算术运算不太可能被向量化,即使类型很可能是数字.

It's advised where possible to use the inbuilt scalar types, in this case float64, because arithmetic operations are unlikely to be vectorised even though the type may well be numerical.

当您存储 strdatetime.date 值时观察到同样的情况,dtype 是这些值的 object.

The same is observed when you store str or datetime.date values, the dtype is object for these.

这篇关于 pandas 报告系列是小数时的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆