【问题标题】:Converting time zone pandas dataframe转换时区熊猫数据框
【发布时间】:2021-04-03 20:10:54
【问题描述】:

我有数据:

                             Symbol      bid      ask
Timestamp                                            
2014-01-01 21:55:34.378000  EUR/USD  1.37622  1.37693
2014-01-01 21:55:40.410000  EUR/USD  1.37624  1.37698
2014-01-01 21:55:47.210000  EUR/USD  1.37619  1.37696
2014-01-01 21:55:57.963000  EUR/USD  1.37616  1.37696
2014-01-01 21:56:03.117000  EUR/USD  1.37616  1.37694

时间戳为格林威治标准时间。有没有办法将其转换为东方?

请注意:

data.index

我得到输出:

<class 'pandas.tseries.index.DatetimeIndex'>
[2014-01-01 21:55:34.378000, ..., 2014-01-01 21:56:03.117000]
Length: 5, Freq: None, Timezone: None

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    将索引(使用tz_localize)本地化为UTC(以使时间戳记时区感知),然后转换为东部(使用tz_convert):

    import pytz
    eastern = pytz.timezone('US/Eastern')
    df.index = df.index.tz_localize(pytz.utc).tz_convert(eastern)
    

    例如:

    import pandas as pd
    import pytz
    
    index = pd.date_range('20140101 21:55', freq='15S', periods=5)
    df = pd.DataFrame(1, index=index, columns=['X'])
    print(df)
    #                      X
    # 2014-01-01 21:55:00  1
    # 2014-01-01 21:55:15  1
    # 2014-01-01 21:55:30  1
    # 2014-01-01 21:55:45  1
    # 2014-01-01 21:56:00  1
    
    # [5 rows x 1 columns]
    print(df.index)
    # <class 'pandas.tseries.index.DatetimeIndex'>
    # [2014-01-01 21:55:00, ..., 2014-01-01 21:56:00]
    # Length: 5, Freq: 15S, Timezone: None
    
    eastern = pytz.timezone('US/Eastern')
    df.index = df.index.tz_localize(pytz.utc).tz_convert(eastern)
    print(df)
    #                            X
    # 2014-01-01 16:55:00-05:00  1
    # 2014-01-01 16:55:15-05:00  1
    # 2014-01-01 16:55:30-05:00  1
    # 2014-01-01 16:55:45-05:00  1
    # 2014-01-01 16:56:00-05:00  1
    
    # [5 rows x 1 columns]
    
    print(df.index)
    # <class 'pandas.tseries.index.DatetimeIndex'>
    # [2014-01-01 16:55:00-05:00, ..., 2014-01-01 16:56:00-05:00]
    # Length: 5, Freq: 15S, Timezone: US/Eastern
    

    【讨论】:

    • 我的日期时间在名为“日期”的列中,所以我正在做df.date = df['date'].pytz_localize(pytz.utc).pytz_convert(pytz.timezone('Europe/Amsterdam')),但我得到了TypeError: index is not a valid DatetimeIndex or PeriodIndex。为什么我没有提到“索引”时错误?这仅在日期时间在 df 索引中时才有效吗?
    • @AstroFloyd --- 请参阅下面的主题以获得您的答案(我遇到了同样的问题):这也适用于日期时间列,但您需要 dt 访问列后:df['column'] = df['column'].dt.tz_convert('America/New_York')
    【解决方案2】:

    最简单的方法是使用to_datetimeutc=True

    df = pd.DataFrame({'Symbol': ['EUR/USD'] * 5,
                      'bid': [1.37622, 1.37624, 1.37619, 1.37616, 1.37616],
                      'ask': [1.37693, 1.37698, 1.37696, 1.37696, 1.37694]})
    
    df.index = pd.to_datetime(['2014-01-01 21:55:34.378000',
                              '2014-01-01 21:55:40.410000',
                              '2014-01-01 21:55:47.210000',
                              '2014-01-01 21:55:57.963000',
                              '2014-01-01 21:56:03.117000'],
                               utc=True)
    

    为了获得更大的灵活性,您可以使用tz_convert() 转换时区。如果您的数据列/索引不支持时区,您将收到警告,应首先使用tz_localize 使数据时区支持。

    df = pd.DataFrame({'Symbol': ['EUR/USD'] * 5,
                      'bid': [1.37622, 1.37624, 1.37619, 1.37616, 1.37616],
                      'ask': [1.37693, 1.37698, 1.37696, 1.37696, 1.37694]})
    
    df.index = pd.to_datetime(['2014-01-01 21:55:34.378000',
                              '2014-01-01 21:55:40.410000',
                              '2014-01-01 21:55:47.210000',
                              '2014-01-01 21:55:57.963000',
                              '2014-01-01 21:56:03.117000'])
    
    df.index = df.index.tz_localize('GMT')
    df.index = df.index.tz_convert('America/New_York')
    

    这同样适用于日期时间列,但访问该列后需要dt

    df['column'] = df['column'].dt.tz_convert('America/New_York')
    

    【讨论】:

    • 如果你的列不是索引,你可以这样做:df['time_local'] = df['time'].dt.tz_localize('GMT').dt.tz_convert('America/New_York')
    • 对我来说,简单地写...dt.tz_convert('Asia/Singapore') 是行不通的,我遇到了奇怪的时刻。我不得不做...dt.tz_convert(pytz.timezone('Asia/Singapore'))
    • 无需本地化到GMT;只需使用 kwarg utc=True (docs) 解析时间戳即可。
    • 这个程序 (tc_localize & tc_convert) 是否也考虑了夏令时的丛林?
    • 针对@Psychotechnopath 的评论问题,我刚刚在 2020 年和 2021 年对其进行了测试,它正确捕获了 EST 的 DST 更改。
    【解决方案3】:

    将 EST 时间转换为亚洲 tz

    df.index = data.index.tz_localize('EST')
    df.index = data.index.tz_convert('Asia/Kolkata')
    

    Pandas 现在内置了 tz 转换能力。

    【讨论】:

    • 谢谢@Shivendra。
    • EST 上注明this comment - 最好改用US/EasternAmerica/New_York
    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 2021-11-03
    • 2018-07-12
    • 2021-05-27
    • 1970-01-01
    • 2019-10-12
    • 2021-08-21
    相关资源
    最近更新 更多