-2

I have a simple timeseries of daily observations over 2 years. I basically want to plot the daily date for each month of the series (looking daily seasonality that occurs each month). For example:

enter image description here

I'd expect a series on the chart for each month. Is there a way to split the dataframe easily to do this?

I'm trying to avoid doing this for each month/year...

df['JUN-2016'] = data[df['date'].month==12 & df['date'].year==2016]

A sample of the dataframe:

DATE    
2015-01-05  2.7483
2015-01-06  2.7400
2015-01-07  2.7250
2015-01-08  2.7350
2015-01-09  2.7350
2015-01-12  2.7350
2015-01-13  2.7450
2015-01-14  2.7450
2015-01-15  2.7350
2015-01-16  2.7183
2015-01-19  2.7300
2015-01-20  2.7150
2015-01-21  2.7150
2015-01-22  2.6550
2015-01-23  2.6500
2015-01-27  2.6450
2015-01-28  2.6350
2015-01-29  2.6100
2015-01-30  2.5600
2015-02-02  2.4783
2015-02-03  2.4700
keynesiancross
  • 3,441
  • 15
  • 47
  • 87

1 Answers1

1

First you need to convert the column with all dates in your dataframe (let's say it is called df["dates"]) into datetimeformat:

df["date"]=pd.to_datetime(df["date"])

also you need to import datetime library:

from datetime import datetime

Then you can just do:

startDateOfInterval = "2016-05-31"
endDateOfInterval = "2016-07-01"

dfOfDesiredMonth = df[df["date"].apply(lambda x: x > datetime.strptime(startDateOfInterval, "%Y-%m-%d") and x < datetime.strptime(endDateOfInterval, "%Y-%m-%d"))]

The df you will get will then only contain the rows with date within this interval.

WurzelseppQX
  • 520
  • 1
  • 6
  • 17