您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

带有夏令时的python pandas TimeStamps到本地时间字符串

带有夏令时的python pandas TimeStamps到本地时间字符串

DST与您的位置有关(例如,伦敦DST在纽约之后的几周开始)。您首先需要使时间戳记时区:

from pytz import UTC
from pytz import timezone
import datetime as dt

ts = pd.Timestamp(datetime.datetime(2015, 3, 31, 15, 47, 25, 901597))
# or...
ts = pd.Timestamp('2015-03-31 15:47:25.901597')
# ts is a Timestamp, but it has no idea where in the world it is...
>>> ts.tzinfo is None
True

# So the timestamp needs to be localized.  Assuming it was originally a UTC timestamp, it can be localized to UTC.
ts_utc = ts.tz_localize(UTC)
# Once localized, it can be expressed in other timezone regions, e.g.: 
eastern = pytz.timezone('US/Eastern')
ts_eastern = ts_utc.astimezone(eastern)
# And to convert it to an ISO string of local time (e.g. eastern):
>>> ts_eastern.isoformat()
'2015-03-30T08:09:27.143173-04:00'

有关更多信息,请参见pytzdatetime

python 2022/1/1 18:39:00 有299人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶