Home > Net >  New column for quarter of year from datetime col
New column for quarter of year from datetime col

Time:09-27

I have a column below as

date
2019-05-11
2019-11-11
2020-03-01
2021-02-18

How can I create a new column that is the same format but by quarter?

Expected output

date        |   quarter
2019-05-11    2019-04-01
2019-11-11    2019-10-01
2020-03-01    2020-01-01
2021-02-18    2021-01-01

Thanks

CodePudding user response:

You can use pandas.PeriodIndex :

df['date'] = pd.to_datetime(df['date'])
df['quarter'] = pd.PeriodIndex(df['date'].dt.to_period('Q'), freq='Q').to_timestamp()

# Output :

print(df)

        date    quarter
0 2019-05-11 2019-04-01
1 2019-11-11 2019-10-01
2 2020-03-01 2020-01-01
3 2021-02-18 2021-01-01

CodePudding user response:

Steps:

  1. Convert your date to date_time object if not in date_time type
  2. Convert your dates to quarter period with dt.to_period or with PeriodIndex
  3. Convert current output of quarter numbers to timestamp to get the starting date of each quarter with to_timestamp

Source Code

import pandas as pd

df = pd.DataFrame({"Dates": pd.date_range("01-01-2022", periods=30, freq="24d")})
df["Quarters"] = df["Dates"].dt.to_period("Q").dt.to_timestamp()
print(df.sample(10))

OUTPUT

        Dates   Quarters
19 2023-04-02 2023-04-01
29 2023-11-28 2023-10-01
26 2023-09-17 2023-07-01
1  2022-01-25 2022-01-01
25 2023-08-24 2023-07-01
22 2023-06-13 2023-04-01
6  2022-05-25 2022-04-01
18 2023-03-09 2023-01-01
12 2022-10-16 2022-10-01
15 2022-12-27 2022-10-01

CodePudding user response:

In this case, a quarter will always be in the same year and will start at day 1. All there is to calculate is the month. Considering quarter is 3 month (12 / 4) then quarters will be 1, 4, 7 and 10.

You can use the integer division (//) to achieve this.

n = month

quarter = ( (n-1) // 3 ) * 3 1

  • Related