How to check the data type of a pandas series?

PandasServer Side ProgrammingProgramming

To check the data type of a Series we have a dedicated attribute in the pandas series properties. The “dtype” is a pandas attribute that is used to verify data type in a pandas Series object.

This attribute will return a dtype object which represents the data type of the given series.

Example 1

# importing required packages
import pandas as pd
import numpy as np

# creating pandas Series object
series = pd.Series(np.random.rand(10))

print("Data type: ",series.dtype )


In this example, we have initialized a pandas series object using NumPy random module, which will create a series with random values.

Let’s apply the pandas dtype property and verify the data type of the series.


0  0.017282
1  0.869889
2  0.255800
3  0.191797
4  0.188235
5  0.261895
6  0.016623
7  0.399498
8  0.642102
9  0.671073
dtype: float64
Data type: float64

In this output block, we can see the series with random values, and the output of the dtype attribute. For the given series object float64 is the data type.

Example 2

import pandas as pd

s = pd.Series({97:'a', 98:'b', 99:'c', 100:'d', 101:'e', 102:'f'})


print("Data type: ",s.dtype )


Create another pandas series object with string data, here we have initialized the series using a python dictionary. Here the goal is to check the data type of the series, so the dtype attribute is applied to the series object “s”.


97   a
98   b
99   c
100  d
101  e
102  f
dtype: object
Data type: object

For the given series “s” the dtype is an object data type, in general pandas used to represents string data in the form of object data type.

Example 3

import pandas as pd

# creating range sequence of dates
dates = pd.date_range('2021-06-01', periods=5, freq='D')

#creating pandas Series with date index
s = pd.Series(dates)
print (s)
print("Data type: ",s.dtype )


In this following example, the series is created by using the pandas date_range method and apply the dtype attribute to verify the data type.


0  2021-06-01
1  2021-06-02
2  2021-06-03
3  2021-06-04
4  2021-06-05
dtype: datetime64[ns]
Data type: datetime64[ns]

The data type of the given series is datetime64[ns].

Updated on 09-Mar-2022 05:40:48