【问题标题】:Conversion of Data frame consisting of a timestamp of UTC to Indian Standard time in python在python中将包含UTC时间戳的数据帧转换为印度标准时间
【发布时间】:2020-10-21 05:06:50
【问题描述】:

我想将当前为 UTC 格式的时间戳数据帧中存在的给定数据集转换为印度标准时间。

下面给出的是数据框中的数据类型:-

时间序列 0 01-02-2018 16:23:07 1 01-02-2018 16:23:08 2 2018 年 1 月 2 日 16:23:09 3 01-02-2018 16:23:10 4 01-02-2018 16:23:11

下面是我的代码:

enter code here
import pandas as pd
load_var=pd.read_excel(r'path/file_name.xlsx')
load_var
a=load_var[['Time Series']]
a

请在代码中建议将 UTC 转换为 IST 的后续步骤

谢谢。

【问题讨论】:

    标签: python python-3.x dataframe data-science


    【解决方案1】:

    如果没有转换,首先将列转换为 date-timestamp-format-type

    import dateutil
    ...
    # Converting to date-type (if it's already in that format)
    load_var['Time Series'] = load_var['Time Series'].apply(dateutil.parser.parse)
    
    #Then use `tz_localize` to make timestamps aware about time zone and then convert to IST (Asia/Kolkata)
    load_var['Time Series_ist'] = pd.to_datetime(load_var['Time Series']).\
                                                   dt.tz_localize('utc').\
                                                   dt.tz_convert('Asia/Kolkata') 
    
    
    

    【讨论】:

      猜你喜欢
      • 2017-04-27
      • 2019-01-05
      • 1970-01-01
      • 2012-02-05
      • 2021-03-05
      • 1970-01-01
      • 2019-08-15
      相关资源
      最近更新 更多