2

How to get period duration in pandas? For example, if I have data

PeriodIndex  Value
2017Q1       1.156355
2017Q2       0.815639
2017Q3       0.798100
2017Q4       1.027752

How do I get duration of each of the indeces? I could do end_time - start_time, but that gets it slightly wrong like Timedelta('30 days 23:59:59.999999') for January and you have to round that somehow. As I need to multiply the value by amount of days, I can also resample the data to days, ffil, then resample back to Q with sum, but that doesn't seem very efficient.

EDIT: Of course in my example I can just multiply with timedelta/timedelta('1 day'), but in general if duration of period is needed, is there no built in function?

  • Can you add sample of your dataframe to dict? Because I cannot reproduce your dataframe. `print df.to_dict()`. And what is desired output? `timedelta` in `days`? – jezrael Apr 19 '16 at 08:37
  • `{Period('2017Q2', 'Q-DEC'): 0, Period('2017Q3', 'Q-DEC'): 0, Period('2017Q1', 'Q-DEC'): 0, Period('2017Q4', 'Q-DEC'): 0}` When I iterate through rows (preferably with apply) I would like duration of period. So for 2017 Q1 that would be 90 days. – user2837976 Apr 19 '16 at 08:46
  • Thank you. and desired output? – jezrael Apr 19 '16 at 08:46
  • Not sure if you can notice my edit, but I'd like a way to extract duration of each period, so for '2017Q1' that would be 90 days. Currently I'm generating timedelta, but can't 'properly' convert it to days (because of summer time and / or rounding). Currently my method works fine, it just seems weird that there would be no direct function for extracting duration of pandas period? – user2837976 Apr 19 '16 at 08:51
  • To avoid the rounding issue, subtract the next period's start time from this period's start time: `def getdays(p): return ((p+1).start_time - p.start_time).days`. – Norman Apr 28 '16 at 20:07

0 Answers0