【发布时间】:2016-03-08 19:03:39
【问题描述】:
我正在使用 Python 2.7 / pandas 0.17.1 中的 Unix 时间戳,并且需要生成一系列本地日期(在美国东部时间)。在单个数据点上调用 .date() 时,时区感知标记似乎按预期工作(给出本地日期),但是当它们作为系列工作时,它们似乎恢复为 UTC 日期。有没有办法让基于系列的日期输出与在对象上单独调用 .date() 产生的本地日期相匹配? (谢谢!)
def stamp_converter(ts):
ser = pd.to_datetime(ts, unit='s', utc=True)
ind = pd.DatetimeIndex(ser).tz_localize('UTC').tz_convert('US/Eastern')
return ind
test_stamps = pd.Series([1396670200, 1405733045, 1448248441])
# e.g., 4/05 in GMT, 4/04 in Eastern
local_dates = pd.Series(stamp_converter(test_stamp))
print local_dates
# 0 2014-04-04 23:56:40-04:00
# 1 2014-07-18 21:24:05-04:00
# 2 2015-11-22 22:14:01-05:00
# dtype: datetime64[ns, US/Eastern]
print local_dates.apply(lambda x: x.date())
# 0 2014-04-05
# 1 2014-07-19
# 2 2015-11-23
# dtype: object
for i, x in local_dates.iteritems():
print x.date()
# 2014-04-04
# 2014-07-18
# 2015-11-22
【问题讨论】:
标签: python datetime pandas pytz timestamp-with-timezone