4.1. Checking datatypes#
4.1.1. Data Types in Python,Pandas and Numpy#
Python |
Pandas |
Numpy |
|
---|---|---|---|
integer |
int |
int64 |
int_,int8,int16,int32,int64 |
float |
float |
float64 |
float_,float16,float32,float64 |
string |
str |
object |
string_ |
boolean |
bool |
bool |
bool_ |
4.1.2. Note#
No date datatype is present in python
Extra Datatypes in Pandas:datetime64,timedelta[ns],category
Extra Datatypes in Python:list,tupple,set,dictinary
# Dataframe for testing purposes
import pandas as pd
import numpy as np
df=pd.DataFrame({'Name':['Sahil', 'Sonia', 'Sourav', 'Vishal'],
'Age':[20, 21, 19, 18]})
# Series for testing purposes
series1=pd.Series([1,2,3])
# Numpy Array for testing purposes
arr = np.array([1,2,3])
df
Name | Age | |
---|---|---|
0 | Sahil | 20 |
1 | Sonia | 21 |
2 | Sourav | 19 |
3 | Vishal | 18 |
series1
0 1
1 2
2 3
dtype: int64
arr
array([1, 2, 3])
4.1.3. Checking datatype#
# For basic Python data types -> only type can be used
type('a') # <class 'str'>
type(1) # <class 'int'>
type(1.0) # <class 'float'>
type(1.9) # <class 'float'>
type([1,2,3]) # <class 'list'>
type([1,2,3,'sahil']) # <class 'list'>
type(True) # <class 'bool'>
type(('a','b')) # <class 'tuple'>
type({'a':'b'}) # <class 'dict'>
# For other things like Numpy,Pandas(Series,Dataframe)
# We can use both type() and .dtype ( and .dtypes() also )
# type(numpy arr | series | df) will tell the type of container
# .dtype -> will tell us the datatype of elements inside it,which is useful
# .dtypes -> will tell datatype of entire dataframe columns
# For numpy array
type(arr) # numpy.ndarray
arr.dtype # int32
# For Series
type(df.Name) # pandas.core.series.Series
type(df.Age) # pandas.core.series.Series
df.Name.dtype # object
df.Age.dtype # 'int64'
# For Dataframe
type(df) # pandas.core.frame.DataFrame
df.dtypes
Name object
Age int64
dtype: object
4.1.4. Summary#
For Python’s inbuilt basic datatypes
type()
For numpy and Pandas columns/series/dataframes
.dtype and .dtypes() will tell type of elements (int,object,float)
type() will tell type of container (series,nparray,dataframe)