Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resample 6 months

How to resample 6 months in pandas? For example, I have 2 dates '2018-02-07' and '2018-09-17'. I want to resample them in 6 month period, the result should be '2018-06-30' and '2018-12-31'.

I setup the code below:

import pandas as pd
import numpy as np

series = pd.Series([1,2], index=[pd.Timestamp('2018-02-07'), pd.Timestamp('2018-09-17')])
s.resample('6M').sum()

It gives '2018-02-28', '2018-08-31', '2019-02-28', but this is not what I want.

like image 316
liang Avatar asked Sep 17 '25 16:09

liang


1 Answers

I think this can help your purposes:

> series.resample('2Q', closed='left').last()

2018-06-30    1
2018-12-31    2
Freq: 2Q-DEC, dtype: int64

Just resampling every 2 Quarters, but closing the interval to the 'left' and getting the last value we have for each semester. More info about the aliases in pandas in the documentation.

like image 143
Mabel Villalba Avatar answered Sep 19 '25 05:09

Mabel Villalba