Pregunta

Simple setup

import pandas
from datetime import datetime

v = [100, 200]
i = [datetime(2012, 1, 31), datetime(2012, 4, 30)]
s = pandas.Series(data=v, index=i)  

Here's the original timeseries:

In [11]: s
Out[11]: 
2012-01-31    100
2012-04-30    200

And after resampling:

In [12]: s.resample('Q')
Out[12]: 
2012-03-31    100
2012-06-30    200
Freq: Q-DEC

So Jan/Apr got aligned to Mar/Jun.

Why didn't pandas choose Dec/Mar instead?

Also, is there any way I can get it to align to Dec/Mar (which seem to be closer to the original dates) with the timeseries functions?

¿Fue útil?

Solución

I know no easy solution to get to align to the closest and I find the current version quite logical. But with label='left' you can achieve what you want with the current data, still it doesn't align to the closest, so overall you probably have to figure out something else (like using apply to change the dates so they would conform as you wish).

s.resample('Q',label='left')    

Out[57]:
2011-12-31    100
2012-03-31    200
Freq: Q-DEC
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top