NumPy or Pandas: Keeping array type as integer while having a NaN value(NumPy 或 Pandas:将数组类型保持为整数,同时具有 NaN 值)
问题描述
是否有一种首选方法可以将 numpy
数组的数据类型固定为 int
(或 int64
或其他),同时仍然里面有一个元素列为 numpy.NaN
?
Is there a preferred way to keep the data type of a numpy
array fixed as int
(or int64
or whatever), while still having an element inside listed as numpy.NaN
?
特别是,我正在将内部数据结构转换为 Pandas DataFrame.在我们的结构中,我们有仍然有 NaN 的整数类型列(但列的 dtype 是 int).如果我们将其设为 DataFrame,似乎会将所有内容重铸为浮点数,但我们真的很想成为 int
.
In particular, I am converting an in-house data structure to a Pandas DataFrame. In our structure, we have integer-type columns that still have NaN's (but the dtype of the column is int). It seems to recast everything as a float if we make this a DataFrame, but we'd really like to be int
.
想法?
尝试过的事情:
我尝试使用 pandas.DataFrame 下的 from_records()
函数和 coerce_float=False
但这没有帮助.我还尝试使用 NumPy 掩码数组和 NaN fill_value,这也不起作用.所有这些都导致列数据类型变为浮点数.
I tried using the from_records()
function under pandas.DataFrame, with coerce_float=False
and this did not help. I also tried using NumPy masked arrays, with NaN fill_value, which also did not work. All of these caused the column data type to become a float.
推荐答案
此功能已添加到 pandas(从 0.24 版本开始):https://pandas.pydata.org/pandas-docs/version/0.24/whatsnew/v0.24.0.html#optional-integer-na-support
This capability has been added to pandas (beginning with version 0.24): https://pandas.pydata.org/pandas-docs/version/0.24/whatsnew/v0.24.0.html#optional-integer-na-support
此时,它需要使用扩展dtype Int64(大写),而不是默认dtype int64(小写).
At this point, it requires the use of extension dtype Int64 (capitalized), rather than the default dtype int64 (lowercase).
这篇关于NumPy 或 Pandas:将数组类型保持为整数,同时具有 NaN 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:NumPy 或 Pandas:将数组类型保持为整数,同时具有 NaN 值


基础教程推荐
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01