Reputation: 8090
I'm trying to convert a Pandas series to a period index. I found several useful time intervals already (quarterly, yearly), but I'd like to expand on this and have 2-yearly and 5-yearly intervals.
Providing a string to the to_period
method such as '2Y'
or '5Y'
raises a ValueError('Only mult == 1 supported')
.
period_width = 'Q' # 'Y'
df['period'] = df['date'].dt.to_period(period_width)
How can I create custom intervals?
Update
The result should behave the same as to_period()
.
In [2]:
import datetime
import pandas as pd
df = pd.DataFrame({
'date': [
datetime.datetime(2010, 2, 2),
datetime.datetime(2010, 2, 5),
datetime.datetime(2011, 2, 2),
datetime.datetime(2012, 3, 5)
]
})
period_width = 'Y'
df['period'] = df['date'].dt.to_period(period_width)
df
Out[2]:
date period
0 2010-02-02 2010
1 2010-02-05 2010
2 2011-02-02 2011
3 2012-03-05 2012
In [3]:
df.dtypes
Out[3]:
date datetime64[ns]
period object
dtype: object
Upvotes: 0
Views: 1586
Reputation: 13975
Well, to create 5 year gaps you might just use the datetime
module.
That would look something like this to create 5 year intervals of Jan 1 starting in the year 2000:
import pandas as pd
import datetime as datetime
five_y = pd.Series([datetime.datetime(Year, 1, 1).date() for Year in xrange(2000, 2100, 5)])
In [2]: five_y
Out[2]:
0 2000-01-01
1 2005-01-01
2 2010-01-01
3 2015-01-01
4 2020-01-01
5 2025-01-01
6 2030-01-01
7 2035-01-01
8 2040-01-01
9 2045-01-01
10 2050-01-01
11 2055-01-01
12 2060-01-01
13 2065-01-01
14 2070-01-01
15 2075-01-01
16 2080-01-01
17 2085-01-01
18 2090-01-01
19 2095-01-01
dtype: object
Upvotes: 1