I need to represent a sequence of events. These events are a little unusual in that they are:
For example:
I would like to represent these events using Pandas.PeriodIndex
but I can't figure out how to create Period
objects with irregular durations.
I have two questions:
Period
objects with irregular durations using existing Pandas functionality?Period
objects? (this comment suggests that it might be possible "using custom DateOffset classes with appropriately crafted onOffset, rollforward, rollback, and apply methods")Period
suggests that it is possible to specify arbitrary durations like 5T
for "5 minutes". I believe this docstring is incorrect. Running pd.Period('2013-01-01', freq='5T')
produces an exception ValueError: Only mult == 1 supported
. I have reported this issue.Period
objects for scalar values and PeriodIndex
for sequences of spans. Better support for irregular intervals with arbitrary start and end points are forth-coming in future releases." (my emphasis)Building a Period
with a custom duration looks pretty straightforward. BUT I think the main stumbling block will be persuading PeriodIndex
to accept Periods
with different freqs
. e.g.:
In [93]: pd.PeriodIndex([pd.Period('2000', freq='D'),
pd.Period('2001', freq='T')])
ValueError: 2001-01-01 00:00 is wrong freq
It looks like a central assumption in PeriodIndex
is that every Period has the same freq
.
A possible solution, depending on the application, is to bin your data by creating a PeriodIndex that has a period equal to the smallest unit of time resolution that you need in order to handle your data and then divide the data amongst the bins for each event, leaving the remaining bins null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With