pandas.api.types.is_datetime64_dtype() Function in Python
The pandas.api.types.is_datetime64_dtype() function is used to check whether an array like object or a datatype is of the datetime64 dtype.
Syntax: pandas.api.types.is_datetime64_dtype(arr_or_dtype)
parameters:
- arr_or_dtype : array like iterable object or datatype.
function returns: a boolean value. True or False. True if object is of the type datetime64 False if not
Example 1:
pandas.api.types is imported and is_datetime64_dtype() function is used to verify whether the given array is of type datetime64. as it is of type int , false is returned.
# importing packages
import pandas.api.types as pd
print(pd.is_datetime64_dtype([10, 20, 30]))
Output:
False
Example 2:
In this example, a datetime array is created and np.datetime64 is given as its type. is_datetime64_dtype() function returns 'True' as the array is of the datetime64 type.
# importing packages
import pandas.api.types as pd
import datetime
import numpy as np
date_list = np.array([datetime.datetime.today()
+ datetime.timedelta(days=x)
for x in range(10)],
dtype=np.datetime64)
print(pd.is_datetime64_dtype(date_list))
Output:
True
Example 3:
numpy.datetime64 dtype from the is directly passed in the method. 'True' is returned.
# importing packages
import pandas.api.types as pd
import numpy as np
print(pd.is_datetime64_dtype(np.datetime64))
Output:
True
Example 4:
An empty NumPy array of type datetime64 is created and it's passed into is_datetime64_dtype() function. 'True' is returned.
# importing packages
import pandas.api.types as pd
import numpy as np
datetime_array = np.array([], dtype=np.datetime64)
print(pd.is_datetime64_dtype(datetime_array))
Output:
True
Example 5:
A string object is passed into the is_datetime64_dtype() function and 'False' is returned.
# importing packages
import pandas.api.types as pd
print(pd.is_datetime64_dtype('string'))
Output:
False