jgg
jgg

Reputation: 831

Pandas DF dtype attribute

When calling the dtypes attribute on a pandas data frame, the last line of the output is usually dtype: object. For example:

In [1]: import pandas as pd
In [2]: df = pd.DataFrame({'numbers':100,'floats': 5.75,'name':'Jill'},index=['a'])
In [3]: df.dtypes
Out[3]: 
numbers  int64
floats float64
name    object
dtype: object

What is the dtype: object line referring to in the output?

Upvotes: 3

Views: 438

Answers (2)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 96172

pandas.DataFrame.dtypes is a pd.Series object, so that's just the dtype of the Series that holds your dtypes!

>>> type(df.dtypes)
<class 'pandas.core.series.Series'>

That makes sense, since it holds numpy.dtype objects:

>>> df.dtypes.map(type)
numbers    <class 'numpy.dtype'>
floats     <class 'numpy.dtype'>
name       <class 'numpy.dtype'>
dtype: object

Upvotes: 2

BENY
BENY

Reputation: 323326

object here refer to not number or mixed data type : which will include string|list|dict..

df = pd.DataFrame({'numbers':100,'floats': 5.75,'name':'Jill'},index=['a'])

df.applymap(type)
Out[7]: 
         numbers           floats           name
a  <class 'int'>  <class 'float'>  <class 'str'>

Notice when the column is mixed with number and other type , it still object

df = pd.DataFrame({'Mix':[111,'notnumber']})
df.dtypes
Out[10]: 
Mix    object
dtype: object

However when we check each cell, it will return the cell type which contain at least two different types

df.applymap(type)
Out[11]: 
             Mix
0  <class 'int'>
1  <class 'str'>

Upvotes: 1

Related Questions