Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between pandas dtype vs dtypes

For the below pandas code in jupyter I am trying to get the data type information .tab in jupyter provides me information that there is two attributes It has both dtype and dtypes

import pandas as pd
new_list = [True,False]
new_pd = pd.Series(new_list)
new_pd

attributes

As per the documentation both returns data type information dtype

dtypes

return from both are good and useful

result

Question is why there is same duplicate attributes . Which one to be used in which scenario or its a don't care anyone can be used ?

like image 376
Hariom Singh Avatar asked Feb 20 '18 11:02

Hariom Singh


2 Answers

In a pd.Series object there is no difference. However, in pd.DataFrame objects you only have dtypes, which is a series with the data type of each column.

The good thing about this is that when you have a series you can treat it mostly uniformly as a NumPy array and use .dtype (which is a property present in every NumPy array) or as a data frame and use .dtypes (which is a property present in all Pandas objects). So in principle many functions for NumPy arrays or data frames already work with series out of the box.

like image 131
jdehesa Avatar answered Oct 01 '22 06:10

jdehesa


You would use dtypes with a DataFrame to get the dtypefor each column/Series.

like image 31
dasdachs Avatar answered Oct 01 '22 06:10

dasdachs