twstats.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:twtools 作者: fradeve 项目源码 文件源码
def run(tags, time_span, step):
    data_sets = [Data(time_span, step, t) for t in tags]
    data_frames = []

    step_fmt = period_map.get(step)

    for ss in data_sets:
        if not ss.df.empty:
            ss.df['start_time'] = pd.to_datetime(ss.df['start'])
            ss.df['end_time'] = pd.to_datetime(ss.df['end'])

            ss.df.drop('start', axis=1, inplace=True)  # Drop `start` column.
            ss.df.drop('end', axis=1, inplace=True)  # Drop `end` column.
            ss.df.drop('tags', axis=1, inplace=True)  # Drop `tags` column.

            ss.df['duration'] = (ss.df['end_time'] - ss.df['start_time'])
            ss.df['duration'] = (
                (ss.df['duration'] / np.timedelta64(1, 's')) / 60
            )
            ss.df['interval'] = ss.df.end_time.dt.to_period(step_fmt)
            ss.df = ss.df.set_index('interval')  # `interval` column as index.

            ss.df.drop('start_time', axis=1, inplace=True)  # Drop `start_time`.
            ss.df.drop('end_time', axis=1, inplace=True)  # Drop `end_time`.

            ss.df = ss.df.groupby(
                pd.TimeGrouper(step_fmt),
                level=0,
            ).aggregate(
                np.sum
            )
            ss.df.rename(columns={'duration': ss.tag}, inplace=True)

            data_frames.append(ss.df)

    result = pd.concat(data_frames, axis=1)

    if step_fmt == 'D':
        result = result.to_timestamp()  # `PeriodIndex` to `DatetimeIndex`.
        result = result.asfreq('D', fill_value=0)  # Fill missing days.

    plot = result.plot(kind='bar')
    plot.set_title('Minutes spent by {p}'.format(p=step))
    plot.set_xlabel('{p}s'.format(p=step))
    plot.set_ylabel('minutes')

    plt.show()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号