date_range does not capture right timezone from input dates · Issue #7901 · pandas-dev/pandas (original) (raw)

Example is below. I would expect that if dates have a timezone on them, date_range would then use that timezone to fill in the rest of the period. However, something goes awry (notice the 01:00 below). If I have dates with a timezone and pass a timezone (case 2), it still doesn't work. Only when I remove the timezone from the dates does it work (case 3). I would expect all these to work the same.

import pytz
tz = pytz.timezone('US/Eastern')
from datetime import datetime
sd = tz.localize(datetime(2014, 3, 6))
ed = tz.localize(datetime(2014, 3, 12))
list(pd.date_range(sd, ed, freq='D'))
Out[41]: 
[Timestamp('2014-03-06 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-07 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-08 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-09 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-10 01:00:00-0400', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-11 01:00:00-0400', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-12 01:00:00-0400', tz='US/Eastern', offset='D')]

list(pd.date_range(sd, ed, freq='D', tz='US/Eastern'))
Out[42]: 
[Timestamp('2014-03-06 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-07 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-08 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-09 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-10 01:00:00-0400', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-11 01:00:00-0400', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-12 01:00:00-0400', tz='US/Eastern', offset='D')]

list(pd.date_range(sd.replace(tzinfo=None), ed.replace(tzinfo=None), freq='D', tz='US/Eastern'))
Out[43]: 
[Timestamp('2014-03-06 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-07 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-08 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-09 00:00:00-0500', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-10 00:00:00-0400', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-11 00:00:00-0400', tz='US/Eastern', offset='D'),
 Timestamp('2014-03-12 00:00:00-0400', tz='US/Eastern', offset='D')]