pandas convert date to quarter 1

pandas convert date to quarter

df['quarter'] = pd.PeriodIndex(, freq='Q')

         date quarter
s1 2018-03-22  2018Q1
s2 2018-03-22  2018Q1
s1 2018-06-22  2018Q2
s2 2018-06-22  2018Q2
s1 2018-09-22  2018Q3
s2 2018-09-22  2018Q3

Here is what the above code is Doing:
1. Create a new column called ‘quarter’
2. Use the PeriodIndex function to create a new index based on the ‘date’ column
3. Set the frequency to ‘Q’ for quarterly

Similar Posts