quarters
quarters

Reputation: 207

Divide entire DataFrame based on dates in specific columns into quarterly dataframes

New to pandas.

Have a DataFrame of the order: A B C Date1 Date2 D with multiple rows with values. I want to divide the entire DataFrame into multiple dataframes based on quarters, i.e (Jan-Mar, Apr-Jun, Jul-Sep, Oct-Dec). I am trying to use only the Date1 column values for the same. I tried the following so far:

data_q = data.groupby(pandas.TimeGrouper(freq = '3M'))

The dates are in the form 2009-11-03.

Upvotes: 1

Views: 3455

Answers (2)

kennes
kennes

Reputation: 2145

There a few ways to do this.

I would ensure that Date1 column is a datetime type using the .dtype method.

e.g. df['Date1'].dtype

If it's not, cast to datetime object using:

df.Date1 = pd.to_datetime(df.Date1)

Add a quarters column for eventual data frame slicing:

df['quarters'] = df.Date1.dt.quarter

Create your data frames:

q1 = df[df.quarters == 1]
q2 = df[df.quarters == 2]
q3 = df[df.quarters == 3]
q4 = df[df.quarters == 4]

Upvotes: 3

TheBlackCat
TheBlackCat

Reputation: 10308

So the approach that appears easiest to me is to convert Date1 to your index, then groupby on the quarter.

df2 = df.set_index('Date1')
quardfs = list(df2.groupby(df2.index.quarter))

This will leave you with quardfs, which a list of DataFrames.

If you don't want to set Date1 to an index, you can also copy it out of the DataFrame and use it:

quars = pd.DatetimeIndex(df['Date1']).quarter
quardfs = list(df2.groupby(quars))

Upvotes: 0

Related Questions