简体   繁体   English

为什么 np nan 可以通过 `astype` 转换为 int(但不能通过 `int`)?

[英]Why is np nan convertible to int by `astype` (but not by `int`)?

This question comes from a finding that is very much not intuitive to me.这个问题来自一个对我来说非常不直观的发现。 If one tries the following:如果尝试以下操作:

import numpy as np
print(np.array([np.nan]).astype(int))
print(int(np.array([np.nan])))

then the output of the first is [-9223372036854775808] , and the second raises ValueError: cannot convert float NaN to integer .那么第一个的输出是[-9223372036854775808] ,第二个引发ValueError: cannot convert float NaN to integer I'd expect the later behaviour, and I'd definitely not expect that one can convert np.nan to an int.我期待后来的行为,我绝对不希望有人可以将np.nan转换为 int。 Why is this like that?为什么会这样? Why can one use astype to convert np.nan to int?为什么可以使用astypenp.nan转换为 int? Does it have any functionality or meaning?它有什么功能或意义吗?

.astype has optional argument casting whose default value is 'unsafe' . .astype具有可选参数casting ,其默认值为'unsafe' Following values are allowed允许以下值

  • 'no' means the data types should not be cast at all. 'no' 表示根本不应该转换数据类型。
  • 'equiv' means only byte-order changes are allowed. 'equiv' 表示只允许字节顺序更改。
  • 'safe' means only casts which can preserve values are allowed. “安全”意味着只允许可以保留值的强制转换。
  • 'same_kind' means only safe casts or casts within a kind, like float64 to float32, are allowed. 'same_kind' 表示只允许安全类型转换或类型中的类型转换,如 float64 到 float32。
  • 'unsafe' means any data conversions may be done. “不安全”意味着可以进行任何数据转换。

When one attempt to do当一个尝试做

import numpy as np
print(np.array([np.nan]).astype(int, casting="safe"))

one gets following error一个得到以下错误

TypeError: Cannot cast array from dtype('float64') to dtype('int32') according to the rule 'safe'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么将np.nan转换为int会产生大量结果? - Why converting np.nan to int results in huge number? int()与.astype('int')Python - int() vs .astype('int') Python 熊猫将convert转换为np.int,lambda和astype()之间的int差异 - pandas apply convert to int differences between np.int, lambda and astype() 这个数组是如何在 y = (iris.target == 0).astype(np.int) 中创建的(查看完整示例)[暂停] - How was this array created in y = (iris.target == 0).astype(np.int) (See full example) [on hold] pandas astype(float) 返回为 int - pandas astype(float) returning as int .astype(“int”)或.astype(int)?有和没有quote / double之间的任何差异? - .astype(“int”) or .astype(int)? Any differences between with and without quote/double? 使用 astype(int) 将时间戳转换为 int - Convert a timestamp to int using astype(int) Pandas Dataframe:为什么 astype 方法使用 int 参数产生 int32 结果 - Pandas Dataframe: Why is astype method producing int32 results with an argument of int 为什么 `int in List[List[int]]` 返回 `False` 而 `np.int in List[List[int]]` 返回 `True`? - Why does `int in List[List[int]]` return `False` but `np.int in List[List[int]]` return `True`? `(model.predict(img) > 0.5).astype(np.int32)`(即)0.5 中的阈值是否总是固定的? - Is the threshold value in `(model.predict(img) > 0.5).astype(np.int32)` (i.e) 0.5 is always fixed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM