Q:

Python calculate a new column based on Date column

Got a Date column with values like this : "1997-08-05T00:00:00" going from 1997 to 2017 with values for 5 working days of the week.
I want to create a new column in the dataset that calculates the quarter of the date.
For example:
1997-01-01 to 1997-03-31 should become Q1
1997-04-01 to 1997-06-30 should become Q2
1997-07-01 to 1997-09-30 should become Q3
1997-10-01 to 1997-12-31 should become Q4

Not only 1997 but all the dates until 2017 should get a value on the new column called "quarter".
Would be great if regardless of year, dates between 01-01 to 03-31 was given Q1, and so forth
(the dates all looks like this: 1997-08-05T00:00:00 but I dont need the part with T00:00:00)

A:

As long as your Date column is in a datetime format you can use the pandas to_period function.
df['quarter'] = df['Date'].dt.to_period('Q')

which will return the quarter in a format similar to 2017Q3.  You can then strip out the year by:
df['quarter'] = df['quarter'].apply(lambda x: str(x)[-2:])

which will give you a column of the Q1, Q2, Q3, Q4 values.

