問題描述
是否有一種首選方法可以將 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 值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!