python - pandas int 和 np.int64 中的奇怪 isinstance 行为

标签 python pandas types

我有一系列 np.int64,但由于某些原因,在不同情况下使用 isinstance() 会产生不同的答案。

您可以在附图中看到,如果我检查单个元素的类型,我会得到 numpy.int64,因此这个特定元素上的 isinstance 会正确运行。

但是,当我使用 apply 时,会发生相反的行为,我会得到不同的结果。这是因为 apply 以某种方式改变了类型吗?

更详细地说,原始系列定义为:

sample_series = pd.Series([np.int64(1), np.int64(25), np.int64(50) ,np.int64(75)])

当我检查一个元素的类型时,type(sample_series.loc[0]),我得到输出 numpy.int64

现在使用 isinstance 给了我以下(预期的)答案, isinstance(sample_series.loc[0], int) ,输出:Falseisinstance (sample_series.loc[0], np.int64) 输出:True

另一方面,sample_series.apply(lambda x : isinstance(x,int)) 给出输出:

0    True
1    True
2    True
3    True
dtype: bool

虽然 sample_series.apply(lambda x : isinstance(x, np.int64)) 给出输出:

0    False
1    False
2    False
3    False
dtype: bool

所以看起来结果不一致。

谢谢!

最佳答案

看来 DataFrame.applySeries.apply 在本质上略有不同。例如:

sample_series = pd.Series([np.int64(1), np.int64(50), np.int64(75)])
#0     1
#1    50
#2    75
#dtype: int64

sample_series.apply(lambda x: type(x))
#0    <class 'int'>
#1    <class 'int'>
#2    <class 'int'>
#dtype: object

但是

df = pd.DataFrame({'val': sample_series})
df.dtypes
#val    int64
#dtype: object

df.apply(lambda row: type(row.val), axis=1)
#0    <class 'numpy.int64'>
#1    <class 'numpy.int64'>
#2    <class 'numpy.int64'>
#dtype: object

如果您查看 Series.apply 代码,看起来奇怪的行为来自 here

# row-wise access
if is_extension_type(self.dtype):
    mapped = self._values.map(f)
else:
    values = self.asobject
    mapped = lib.map_infer(values, f, convert=convert_dtype)

它采用您的系列,然后创建 values,即 array([1, 50, 75], dtype=object) 并将其传递给 中的另一个函数code>pandas._libs 应用你的函数 f = lambda x: isinstance(x, np.int64)

另一方面,带有 axis=1DataFrame.apply 工作正常,因为当它定义 values 时,它通过 >values = self.values See here ,它给你 values = array([ 1, 50, 75], dtype=int64)

事实上,如果您将底层 pandas Series.apply 代码更改为 values=self.values,您将获得预期的输出。

关于python - pandas int 和 np.int64 中的奇怪 isinstance 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49882785/

相关文章:

python - 将 DataFrame 的每 n 个索引乘以(或除以)数组中的常量

Python - 值错误 : unknown locale: UTF-8

sql-server - 哪些 Progress 数据类型对应于哪些 SQL Server 数据类型?

javascript - 如何将csrf_token传递给jqgrid的editurl的post参数?

python - 在 Python 中的文本文件的特定行中附加数据?

pandas - 使用 BeautifulSoup 将表抓取到数据框中

c++ - 如何将模板参数与 "if"和 "switch"语句一起使用?

C:数据类型。 sqrt 函数与 int 一起使用为什么?

python - 追加或添加到特定索引的字符串

python - 下降栏是周末。选择仅工作日索引的列