Making A Timeseries Plot In Python, But Want To Skip Few Months
I have a timeseries data of ice thickness. The plot is only useful for winter months and there is no interest in seeing big gaps during summer months. Would it be possible to skip
Solution 1:
Let's take this data:
importdatetimen_samples=20index=pd.date_range(start='1/1/2018',periods=n_samples,freq='M')values=np.random.randint(0,100,size=(n_samples))data=pd.Series(values,index=index)print(data)2018-01-31 582018-02-28 932018-03-31 152018-04-30 872018-05-31 512018-06-30 672018-07-31 222018-08-31 662018-09-30 552018-10-31 732018-11-30 702018-12-31 612019-01-31 952019-02-28 972019-03-31 312019-04-30 502019-05-31 752019-06-30 802019-07-31 842019-08-31 19Freq:M,dtype:int64
You can filter the data that is not in the range of the months, so you take the index of Serie, take the month, check if is in the range, and take the negative (with ~)
filtered1=data[~data.index.month.isin(range(4,10))]print(filtered1)2018-01-31 582018-02-28 932018-03-31 152018-10-31 732018-11-30 702018-12-31 612019-01-31 952019-02-28 972019-03-31 31
If you plot that,
filtered1.plot()
you will have this image so you need to set the frecuency, in this case, monthly (M)
filtered1.asfreq('M').plot()
Additionally, you could use filters like:
- filtered2 = data[data.index.month.isin([1,2,3,11,12])]
- filtered3 = data[~ data.index.month.isin([4,5,6,7,8,9,10])] if you need keep/filter specific months.
Post a Comment for "Making A Timeseries Plot In Python, But Want To Skip Few Months"