pandas TypeError: 'Series' 对象在访问数据帧的 dtypes 时不可调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49186752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:17:31  来源:igfitidea点击:

TypeError: 'Series' object is not callable when accessing dtypes of a dataframe

pythonpandasdataframetypeerror

提问by dia

What the hell? I didn't change the keyword to reading the text file I have on my directory.

我勒个去?我没有将关键字更改为读取目录中的文本文件。

Yet I can't check the dtype of each columns by the two methods that I know of.

但是我无法通过我知道的两种方法检查每列的 dtype。

If I use .dtypesit would return TypeError that Series is not callable:

如果我使用.dtypes它会返回 TypeError 该系列不可调用:

enter image description here

在此处输入图片说明

Here it returns AttributeError.. now claiming what I read is dataframe:

这里它返回 AttributeError .. 现在声称我读的是数据帧:

enter image description here

在此处输入图片说明

Any thoughts?

有什么想法吗?

回答by cs95

There's no ambiguity here. fileis a dataframe, and dtypesis an attribute.

这里没有歧义。file是一个数据框,dtypes是一个属性。

df
        productView  order
userId                    
A               4.5    5.0
B               1.5    2.5
C               4.0    2.0
D               2.0    3.0

df.dtypes
productView    float64
order          float64
dtype: object

When you access dtypes, a Series is returned:

当您访问 时dtypes,将返回一个系列:

type(df.dtypes)
pandas.core.series.Series

When you call df.dtypes(), you are effectively doing series = df.dtype; series()which is invalid, since seriesis an object (not a function, or an object with __call__defined).

当您调用 时df.dtypes(),您实际上是在执行series = df.dtype; series()无效的操作,因为它series是一个对象(不是函数,或已__call__定义的对象)。

In the second case, dtypeisn't even a valid attribute/method of df, and so an AttributeErroris raised.

在第二种情况下,dtype甚至不是 的有效属性/方法df,因此AttributeError引发了 。

TLDR; The first error is raised on the dtypeseries, the second is raised on the original dataframe df.

TLDR;第一个错误是在dtype系列上引发的,第二个是在原始数据帧上引发的df