【问题标题】:How to chart aggregated time series data with matplotlib如何使用 matplotlib 绘制汇总的时间序列数据
【发布时间】:2020-01-31 21:50:43
【问题描述】:

我有一个包含每个错误发生时间的时间戳的 csv 错误文件。示例数据如下所示

2020-01-06T02:54:01.012+0000, 500 Internal Server Error
2020-01-06T05:04:01.012+0000, 500 Internal Server Error
2020-01-06T05:44:01.012+0000, 500 Internal Server Error
2020-01-06T07:04:01.013+0000, 500 Internal Server Error
2020-01-06T08:04:01.012+0000, 500 Internal Server Error
2020-01-06T10:24:01.010+0000, 500 Internal Server Error
2020-01-06T17:48:31.192+0000, 503 Service Unavailable
2020-01-08T04:35:48.624+0000, 502 Bad Gateway
2020-01-08T16:56:04.814+0000, 503 Service Unavailable

我想使用 matplotlib 将每分钟(或 30 秒)的错误显示为图表上的单独一行。不知道怎么做聚合?这是我尝试过的。有什么帮助吗?


import matplotlib.pyplot as plt
import pandas as pd

infile="example.csv"

dateparse = lambda time_str: pd.datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S.%f')
df = pd.read_csv(infile, parse_dates=['datetime'], date_parser=dateparse, index_col="datetime", names=['datetime','error'])

df.plot()
plt.show()

【问题讨论】:

    标签: python pandas datetime matplotlib timeserieschart


    【解决方案1】:

    从定义以下函数开始:

    def counts(grp):
        codeList = ['500', '502', '503']
        return pd.Series([np.count_nonzero(grp.eq(code)) for code in codeList],
            index=map(lambda x: 'Err_' + x, codeList))
    

    它将很快应用到您重新采样后产生的每个组 数据帧。

    如下阅读您的文件:

    df = pd.read_csv('your_log.csv', names=['Date', 'Message'],
        skipinitialspace=True, parse_dates=[0])
    

    注意skipinitialspace参数,后面需要去掉初始空格 每个逗号(在日期/时间之后)。

    然后运行:df.Date = df.Date.dt.tz_localize(None) 删除时区 结果中的部分 (+0000)。

    由于比较完整的字符串很尴尬,我决定只比较错误 代码(每条消息的 3 个初始字符)。为此,让我们创建一个新的 仅包含错误代码的列:

    df['Code'] = df.Message.str.slice(0,3)
    

    下一步它通过重新采样在每小时生成错误数量 并将上述功能应用于每个小时组:

    errCnt = df.resample('1H', on='Date').Code.apply(counts).unstack(level=1)
    

    如果您想要任何其他时间分辨率,请将 1H 更改为所需的时间段。

    最后一步是添加 Total 列(如果需要):

    errCnt['Total'] = errCnt.sum(axis=1)
    

    对于我的示例数据(有更多错误,时间更短),我得到:

                         Err_500  Err_502  Err_503  Total
    Date                                                       
    2020-01-06 02:00:00        1        0        0      1
    2020-01-06 03:00:00        0        0        0      0
    2020-01-06 04:00:00        0        0        0      0
    2020-01-06 05:00:00        1        2        2      5
    2020-01-06 06:00:00        0        0        0      0
    2020-01-06 07:00:00        1        0        0      1
    2020-01-06 08:00:00        1        0        0      1
    

    【讨论】:

    • 我想要结果框中的 3 列。 “500 Internal Server Error”、“502 Bad Gateway”和“503 Service Unavailable”各一个
    • resample 是否可以创建 3 列而不是仅 1 列?
    • 我更改了我的解决方案,只是为了生成 3 个单独的计数(针对每个感兴趣的错误代码)。此外,我创建了 Totat 列。如果不需要,请从我的代码中删除相关说明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 2016-02-22
    • 2021-01-03
    • 2013-07-28
    • 1970-01-01
    • 2018-10-31
    相关资源
    最近更新 更多