DataFrame.plot()不正确地将东部时间转换为UCT

时间:2019-06-24 13:40:39

标签: python pandas dataframe matplotlib timezone-offset

我的数据(原始数据和DataFrame数据)都在美国东部时间,但是每当我绘制图形时,它都会在UCT时间显示。

我已经尝试了以下方法:

    df = pd.read_sql_query(self.consolidated_gateway_query(), self.database_connection)
    df['begin_ts'] = df['begin_ts'].dt.tz_convert('US/Eastern') 
    pd.set_option('display.float_format', lambda x: '%.10f' % x)
    print(df)

    plt.rcParams["timezone"] = 'US/Eastern' 
    ax = plt.subplots()[1] # subplots returns a tuple(Figure, Axes) 
    ax.xaxis_date('US/Eastern') 
    df.plot(kind='line', x='begin_ts', y='average', ax=ax, title="Gateway TPS for %s" % self.curr_date, legend=False) 
    plt.xlabel('Time', fontsize=20) 
    plt.ylabel('Transactions per Second', fontsize=20) 
    plt.show()

================================================ =======================

这是正在输出的图形

Graph

作为参考,这是用于创建图形的DataFrame的片段:

                      begin_ts            average
    0   2019-06-21 06:47:00-04:00     701.3958333333
    1   2019-06-21 07:30:00-04:00   21445.5208333333
    2   2019-06-21 07:31:00-04:00       8.0000000000
    3   2019-06-21 07:34:00-04:00      13.0000000000
    4   2019-06-21 07:35:00-04:00       4.0000000000
    5   2019-06-21 07:37:00-04:00       4.0000000000
    6   2019-06-21 07:38:00-04:00       2.0000000000
    7   2019-06-21 07:41:00-04:00       1.0000000000
    8   2019-06-21 07:42:00-04:00       1.3666666667
    9   2019-06-21 07:45:00-04:00       5.4351851852
    10  2019-06-21 07:47:00-04:00       1.0000000000

编辑:以下代码解决了我的问题:

df.begin_ts = df.begin_ts.apply(lambda x: datetime.datetime.replace(x, tzinfo=None))

0 个答案:

没有答案
相关问题