Open In App

Pandas Series dt.quarter | Find Quarter from DateTime Object

Improve
Improve
Like Article
Like
Save
Share
Report

Pandas dt.quarter attribute returns the quarter of the date in the underlying DateTime based data in the given Series object.

Example

Python3




import pandas as pd
sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30',
                '2010-4-22 09:25', '2019-11-8 02:22'])
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
sr.index = idx
sr = pd.to_datetime(sr)
result = sr.dt.quarter
print(result)


Output

output of dt.quarter attribute

Syntax

Syntax: Series.dt.quarter 

Parameter : None 

Returns: NumPy array containing quarter value

How to Get Quarter Value from DateTime Object in Pandas Series

To get the quarter value from the DateTime object in the Pandas series we use the dt.quarter attribute of the Pandas library in Python.

Let us understand it better with an example:

Example:

Use the dt.quarter attribute to return the quarter of the date in the underlying data of the given Series object.

Python3




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(pd.date_range('2012-12-12 12:12'
                       periods = 5, freq = 'M'))
  
# Creating the index
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
  
# set the index
sr.index = idx
  
# Print the series
print(sr)


Output :

datetime series created

Now we will use the Series.dt.quarter attribute to return the quarter of the date in the DateTime based data in the given series object.

Python3




# return the quarter of the date
result = sr.dt.quarter
  
# print the result
print(result)


Output :

printing quarter from datetime object

As we can see in the output, the Pandas dt.quarter attribute has successfully accessed and returned the quarter of the date in the underlying data of the given series object.



Last Updated : 07 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads