Clean way to convert quarterly periods to datetime in pandas

You can (and should) use pd.PeriodIndex as a first step, then convert to timestamp using PeriodIndex.to_timestamp: qs = df[‘Quarter’].str.replace(r'(Q\d) (\d+)’, r’\2-\1′) qs 0 1996-Q3 1 1996-Q4 2 1997-Q1 Name: Quarter, dtype: object df[‘date’] = pd.PeriodIndex(qs, freq=’Q’).to_timestamp() df Quarter date 0 Q3 1996 1996-07-01 1 Q4 1996 1996-10-01 2 Q1 1997 1997-01-01 The initial replace step … Read more

Generate a list of datetimes between an interval

Use datetime.timedelta: from datetime import date, datetime, timedelta def perdelta(start, end, delta): curr = start while curr < end: yield curr curr += delta >>> for result in perdelta(date(2011, 10, 10), date(2011, 12, 12), timedelta(days=4)): … print result … 2011-10-10 2011-10-14 2011-10-18 2011-10-22 2011-10-26 2011-10-30 2011-11-03 2011-11-07 2011-11-11 2011-11-15 2011-11-19 2011-11-23 2011-11-27 2011-12-01 2011-12-05 2011-12-09 … Read more

Period to string [duplicate]

You need to normalize the period because if you construct it with the total number of seconds, then that’s the only value it has. Normalizing it will break it down into the total number of days, minutes, seconds, etc. Edit by ripper234 – Adding a TL;DR version: PeriodFormat.getDefault().print(period) For example: public static void main(String[] args) … Read more