简体   繁体   English

isinstance() 和 np.issubdtype() 有什么区别?

[英]What's the difference between isinstance() and np.issubdtype()?

I have a dataframe df:我有一个数据框 df:

>>> df = pd.DataFrame({'values':[1.23, 3.12, 23.12]}, dtype=float)

Running the check:运行检查:

>>> df.values.dtype
dtype('float64')

Now using isinstance method:现在使用 isinstance 方法:

>>> isinstance(df.values.dtype, float)
False

Now using issubdtype method:现在使用 issubdtype 方法:

>>> np.issubdtype(df.values.dtype, float)
True

By reading the doc.通过阅读文档。

Syntax isinstance (object, classinfo)语法 isinstance (object, classinfo)

object : An object instance. object : 一个对象实例。

I'm passing a ndarray object (Can I consider this as an object instance?)我正在传递一个 ndarray 对象(我可以将其视为对象实例吗?)

classinfo : A class, type or a tuple containing classes, types or other tuples. classinfo :包含类、类型或其他元组的类、类型或元组。

I'm passing the float which is a type, which is fine.我正在传递一种类型的浮点数,这很好。

The question is: why is the isinstance not returning true in the above example?问题是:为什么上面例子中的 isinstance 没有返回 true? and what's the difference between isinstance and issubdtype? isinstance 和 issubdtype 有什么区别?

why is the isinstance not returning true in the above example?为什么在上面的例子中isinstance没有返回 true?

Because df.values.dtype returns a type , not an object of that type.因为df.values.dtype返回一个type ,而不是那个类型的对象。 Indeed, df.values.dtype returns dtype('float') , but that is not a float object .实际上, df.values.dtype返回df.values.dtype dtype('float') ,但这不是float object It is a dtype object.它是一个dtype对象。

The issubdtype takes a dtype, type, or a string and checks if the first type is the same or a subclass of the second type. issubdtype采用issubdtype 、类型或字符串,并检查第一个类型是否相同或第二个类型的子类。 We can inspect the method resolution order of the type, and see:我们可以检查类型的方法解析顺序,并看到:

>>> df.values.dtype.type.__mro__
(<class 'numpy.float64'>, <class 'numpy.floating'>, <class 'numpy.inexact'>, <class 'numpy.number'>, <class 'numpy.generic'>, <class 'float'>, <class 'object'>)

so the type is indeed a subclass of float .所以类型确实是float的子类。 But it is not an instance of a float .但它不是float的实例。 An instance of a float is for example 0.0 , 3.14 , np.nan , etc. If we inspect the type of these objects, we get: float一个实例是例如0.03.14np.nan等。如果我们检查这些对象的类型,我们会得到:

>>> type(0.0)
<class 'float'>
>>> type(3.14)
<class 'float'>
>>> type(np.nan)
<class 'float'>

Whereas the type of for the dtype('float') is dtype , and for float , it is just type :dtype('float')dtype ,而对于float ,它只是type

>>> type(df.values.dtype)
<class 'numpy.dtype'>
>>> type(float)
<class 'type'>

We can thus check that the df.values.dtype is an instance of dtype and obtain:因此,我们可以检查df.values.dtype是一个实例dtype ,并获得:

>>> isinstance(df.values.dtype, np.dtype)
True

First, df.values.dtype is an instance of the numpy's class dtype , not of the Python's class float :首先, df.values.dtype是numpy的的类的实例, D型,而不是Python的类的float

>>> df.values.dtype
dtype('float64')

>>> isinstance(df.values.dtype, np.dtype)
True

>>> isinstance(df.values.dtype, float)
False

Second, from the source code of the function np.issubdtype we see that when calling np.issubdtype(df.values.dtype, float) , the first argument df.values.dtype is converted to df.values.dtype.type (ie np.float64 ) and the second argument float to np.floating .其次,从函数np.issubdtype源代码中我们看到,在调用np.issubdtype(df.values.dtype, float) ,第一个参数df.values.dtype被转换为df.values.dtype.type (即np.float64 ) 和第二个参数floatnp.floating Then what is returned is issubclass(np.float64, np.floating) , which is of course True .然后返回的是issubclass(np.float64, np.floating) ,当然是True

So, in short, df.values.dtype is a subclass of np.floating , not an instance of float .因此,简而言之, df.values.dtype是的一个子类np.floating ,而不是一个实例float

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM