- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check the data type of a pandas series?
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(series) print("Data type: ",series.dtype )
Explanation
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.
Output
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(s) print("Data type: ",s.dtype )
Explanation
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”.
Output
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 )
Explanation
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.
Output
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].
- Related Articles
- How to check the data type in pandas DataFrame?
- How to get the final rows of a time series data using pandas series.last() method?
- How to check each value of a pandas series is unique or not?\n
- How to plot arbitrary markers on a Pandas data series using Matplotlib?
- How to sort a Pandas Series?
- How to get the nth percentile of a Pandas series?
- How to check whether the Pandas series is having Nan values or not?
- Python - Check if the Pandas Index is a floating type
- How to Get the Position of Max Value of a pandas Series?
- How to Get the Position of Minimum Value of a pandas Series?
- How to check the data present in a Series object is monotonically increasing or not?
- How to check the data present in a Series object is monotonically decreasing or not?
- How to append elements to a Pandas series?
- How to append a pandas Series object to another Series in Python?
- How to calculate the frequency of each item in a Pandas series?
