Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

Pandas resample bi-monthly on even months

$
0
0

I've got the following daily data:

1983-03-30    0.0012241983-03-31   -0.0037411983-04-04    0.0051211983-04-05    0.0091711983-04-06    0.0063951983-04-07    0.0090301983-04-08    0.0069611983-04-11   -0.0039501983-04-12    0.0188371983-04-13   -0.000324          ...

I'd like to resample this using bimonthly frequency. So I do:

s.resample('2ME').agg('last')  # Use '2M' for older versions of Pandas

This produces the following:

1983-03-31   -0.0037411983-05-31    0.0019871983-07-31    0.0056571983-09-30   -0.0078431983-11-30   -0.0054441984-01-31    0.0030111984-03-31   -0.0003241984-05-31    0.0006491984-07-31   -0.0014471984-09-30    0.002705

However, I'd like to group (3,4), (5,6), (7,8) etc. so I'd like a series where the timestamps are:

1983-04-301983-06-311983-08-31

To reproduce this:

index = pd.date_range(start='1983-03-28', end='1984-01-01', freq='B')s = pd.Series(data=np.random.randn(len(index)), index=index)

How can I tell resample to group on even months? Or even better, to always take the first two when grouping rather than the first 1 as in the case above - it has grouped March by itself then April and May together rather than grouping March and April together.


Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>