pandas之to_datetime时区转换

from datetime import date, datetime, timedelta
    import time
    import pandas as pd
    from pandas.tseries.offsets import Hour, Minute, Second
    
    
    print(time.localtime()) # time.struct_time(tm_year=2019, tm_mon=11, tm_mday=12, tm_hour=18, tm_min=34, tm_sec=52, tm_wday=1, tm_yday=316, tm_isdst=0)
    print(datetime.now().timestamp()) # 1573554892.416593 带小时分秒的
    print(datetime.now().tzinfo)    # None
    print(datetime.now().timetz())  # 18:34:52.416593
    print(datetime.now().date())    # 2019-11-12
    print(datetime.now())  # 2019-11-12 18:34:52.416593
        
    # date
    fixdate = date.today()+timedelta(days=-1)
    # date convert to datetime
    dtt = datetime(fixdate.year, fixdate.month, fixdate.day)
    print(dtt)              # 2019-11-11 00:00:00
    print(dtt.tzinfo)       # None 默认没有时区信息
    s_ts = dtt.timestamp()  # 1573401600.0 UTC时间戳
    print(‘------------------pd.to_datetime--------------------------‘)
    # pandas需要自己处理时区问题,默认没有时区信息,可以先本地化tz_localize时区再转换成tz_convert目标时区
    pddt = pd.to_datetime(s_ts, utc=True, unit=‘s‘) 
    print(pddt.tzinfo)                        # UTC
    print(pddt.timestamp())                   # 1573401600.0 
    print(pddt)                               # 2019-11-10 16:00:00+00:00
    pddt = pd.to_datetime(s_ts, utc=False, unit=‘s‘)
    print(pddt.tzinfo)                        # None
    print(pddt.timestamp())                   # 1573401600.0
    print(pddt)                               # 2019-11-10 16:00:00 
    pddt = pd.to_datetime(s_ts, utc=False, unit=‘s‘).tz_localize(‘Asia/Shanghai‘) # 本地化成某个时区
    print(pddt.tzinfo)                        # Asia/Shanghai
    print(pddt.timestamp())                   # 1573372800.0 
    print(pddt)                               # 2019-11-10 16:00:00+08:00 
    pddt = pd.to_datetime(s_ts, utc=True, unit=‘s‘).tz_convert(‘Asia/Shanghai‘)  # 转换成某个时区
    print(pddt.tzinfo)                        # Asia/Shanghai
    print(pddt.timestamp())                   # 1573401600.0 
    print(pddt)                               # 2019-11-11 00:00:00+08:00 
    print(‘--------------------Hour(23) + Minute(59) + Second(59) --------------------------‘)
    sec = Hour(23) + Minute(59) + Second(59) 
    print(sec.freqstr) # 86399S 秒,结束带个S
    pddt = pd.to_datetime(s_ts+int(sec.freqstr[:-1]), utc=True, unit=‘s‘).tz_convert(‘Asia/Shanghai‘)
    print(pddt)              # 2019-11-11 23:59:59+08:00
    print(pddt.timestamp())  # 1573487999.0 
    
     # 这样算本来才是真正想要的目标时间戳,推荐使用这种方式
    dtt_hms = (dtt + Hour(23) + Minute(59) + Second(59)).tz_localize(‘Asia/Shanghai‘).tz_convert(‘utc‘).value/1000000000
    print(dtt_hms)           # 1573487999.0 2019-11-11 23:59:59+08:00
    
    # 这样算出为是目标时间多了8小时的时间戳
    dtt_hms = (dtt + Hour(23) + Minute(59) + Second(59)).value/1000000000
    print(dtt_hms)           # 1573516799.0  2019-11-12 07:59:59 
    exit(0)
文章来自:https://www.cnblogs.com/zxpo/p/11844577.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3