pandas - Pandas:即使缺少值也能绘制时间序列
问题描述
我的数据集是带有时间戳的一系列事件。我想绘制每个时间间隔发生的事件数(几个图,例如“每个月”或“每天”或“每个小时”)。这些地块是使用pandas
, 特别是groupby()
我已经想出了如何做到这一点,但是这些图省略了没有事件的日期范围。例如,在下面的示例中,2020-08-16 没有事件,因此未绘制该日期。相反,我希望它以 0 的计数绘制。
我知道如何用老办法做到这一点:我可以自己使用 Python 循环等对这些数据进行后处理。但这听起来pandas
应该可以更有效地完成,但我不知道怎么做。
我创建了一个最小的可重现代码片段: https ://gist.github.com/jlumbroso/50afaa12d8af8dac615331d515f0f0ff
并在此处提供了一个说明性示例:
0 2020-08-15 16:34:15.838169 False
1 2020-08-17 14:25:08.778913 True
2 2020-08-19 07:44:07.514456 False
3 2020-08-19 14:48:29.160890 True
4 2020-08-20 03:26:00.479444 False
5 2020-08-20 10:57:52.904366 False
6 2020-08-20 19:17:45.079390 True
7 2020-08-20 23:38:41.369156 False
8 2020-08-21 12:21:54.340702 True
9 2020-08-24 19:42:13.458472 False
10 2020-08-24 23:09:39.369394 True
11 2020-08-25 16:35:05.059722 False
12 2020-08-26 01:31:29.243435 True
13 2020-08-26 03:28:25.418322 True
14 2020-08-27 12:42:43.905486 True
15 2020-08-31 10:35:57.143843 False
16 2020-09-02 11:32:54.219081 True
17 2020-09-02 14:07:05.544261 False
18 2020-09-03 08:05:32.133082 False
19 2020-09-10 15:28:46.725916 True
20 2020-09-12 00:57:58.558055 True
21 2020-09-13 21:28:02.450837 True
我发现了这些相关的问题,但我无法从中推断出答案:
- Pandas 时间序列 DataFrame 缺失值
- pandas 绘制间隙最小的时间序列
- Pandas 重采样错误:仅对 DatetimeIndex 或 PeriodIndex 有效
- Groupby 和重新采样时间序列,因此日期范围是一致的
谢谢你的帮助!
解决方案
好的,您需要使用Resample。让我们使用您的数据
content = """0 2020-08-15 16:34:15.838169 False
1 2020-08-17 14:25:08.778913 True
2 2020-08-19 07:44:07.514456 False
3 2020-08-19 14:48:29.160890 True
4 2020-08-20 03:26:00.479444 False
5 2020-08-20 10:57:52.904366 False
6 2020-08-20 19:17:45.079390 True
7 2020-08-20 23:38:41.369156 False
8 2020-08-21 12:21:54.340702 True
9 2020-08-24 19:42:13.458472 False
10 2020-08-24 23:09:39.369394 True
11 2020-08-25 16:35:05.059722 False
12 2020-08-26 01:31:29.243435 True
13 2020-08-26 03:28:25.418322 True
14 2020-08-27 12:42:43.905486 True
15 2020-08-31 10:35:57.143843 False
16 2020-09-02 11:32:54.219081 True
17 2020-09-02 14:07:05.544261 False
18 2020-09-03 08:05:32.133082 False
19 2020-09-10 15:28:46.725916 True
20 2020-09-12 00:57:58.558055 True
21 2020-09-13 21:28:02.450837 True
"""
from io import StringIO
df = pd.read_csv(StringIO(content), sep=" ", header=None, index_col=0)
print(df)
1 2
0
0 2020-08-15 16:34:15.838169 False
1 2020-08-17 14:25:08.778913 True
2 2020-08-19 07:44:07.514456 False
3 2020-08-19 14:48:29.160890 True
4 2020-08-20 03:26:00.479444 False
5 2020-08-20 10:57:52.904366 False
6 2020-08-20 19:17:45.079390 True
7 2020-08-20 23:38:41.369156 False
8 2020-08-21 12:21:54.340702 True
9 2020-08-24 19:42:13.458472 False
10 2020-08-24 23:09:39.369394 True
11 2020-08-25 16:35:05.059722 False
12 2020-08-26 01:31:29.243435 True
13 2020-08-26 03:28:25.418322 True
14 2020-08-27 12:42:43.905486 True
15 2020-08-31 10:35:57.143843 False
16 2020-09-02 11:32:54.219081 True
17 2020-09-02 14:07:05.544261 False
18 2020-09-03 08:05:32.133082 False
19 2020-09-10 15:28:46.725916 True
20 2020-09-12 00:57:58.558055 True
21 2020-09-13 21:28:02.450837 True
使用第一列,如索引,然后删除它:
df = df.set_index(pd.DatetimeIndex(df.iloc[:,0]))
df.drop(df.columns[0], 1, inplace=True)
df
2
1
2020-08-15 16:34:15.838169 False
2020-08-17 14:25:08.778913 True
2020-08-19 07:44:07.514456 False
2020-08-19 14:48:29.160890 True
2020-08-20 03:26:00.479444 False
2020-08-20 10:57:52.904366 False
2020-08-20 19:17:45.079390 True
2020-08-20 23:38:41.369156 False
2020-08-21 12:21:54.340702 True
2020-08-24 19:42:13.458472 False
2020-08-24 23:09:39.369394 True
2020-08-25 16:35:05.059722 False
2020-08-26 01:31:29.243435 True
2020-08-26 03:28:25.418322 True
2020-08-27 12:42:43.905486 True
2020-08-31 10:35:57.143843 False
2020-09-02 11:32:54.219081 True
2020-09-02 14:07:05.544261 False
2020-09-03 08:05:32.133082 False
2020-09-10 15:28:46.725916 True
2020-09-12 00:57:58.558055 True
2020-09-13 21:28:02.450837 True
通过例如 day、 sum 和 plot重新采样
df.resample('D').sum().plot()
请注意,如果您有列名,这很有用:
content = """Date Condition
0 2020-08-15 16:34:15.838169 False
1 2020-08-17 14:25:08.778913 True
2 2020-08-19 07:44:07.514456 False
3 2020-08-19 14:48:29.160890 True
4 2020-08-20 03:26:00.479444 False
5 2020-08-20 10:57:52.904366 False
6 2020-08-20 19:17:45.079390 True
7 2020-08-20 23:38:41.369156 False
8 2020-08-21 12:21:54.340702 True
9 2020-08-24 19:42:13.458472 False
10 2020-08-24 23:09:39.369394 True
11 2020-08-25 16:35:05.059722 False
12 2020-08-26 01:31:29.243435 True
13 2020-08-26 03:28:25.418322 True
14 2020-08-27 12:42:43.905486 True
15 2020-08-31 10:35:57.143843 False
16 2020-09-02 11:32:54.219081 True
17 2020-09-02 14:07:05.544261 False
18 2020-09-03 08:05:32.133082 False
19 2020-09-10 15:28:46.725916 True
20 2020-09-12 00:57:58.558055 True
21 2020-09-13 21:28:02.450837 True
"""
from io import StringIO
df = pd.read_csv(StringIO(content), sep=" ", index_col=0)
print(df)
Date Condition
0 2020-08-15 16:34:15.838169 False
1 2020-08-17 14:25:08.778913 True
2 2020-08-19 07:44:07.514456 False
3 2020-08-19 14:48:29.160890 True
4 2020-08-20 03:26:00.479444 False
5 2020-08-20 10:57:52.904366 False
6 2020-08-20 19:17:45.079390 True
7 2020-08-20 23:38:41.369156 False
8 2020-08-21 12:21:54.340702 True
9 2020-08-24 19:42:13.458472 False
10 2020-08-24 23:09:39.369394 True
11 2020-08-25 16:35:05.059722 False
12 2020-08-26 01:31:29.243435 True
13 2020-08-26 03:28:25.418322 True
14 2020-08-27 12:42:43.905486 True
15 2020-08-31 10:35:57.143843 False
16 2020-09-02 11:32:54.219081 True
17 2020-09-02 14:07:05.544261 False
18 2020-09-03 08:05:32.133082 False
19 2020-09-10 15:28:46.725916 True
20 2020-09-12 00:57:58.558055 True
21 2020-09-13 21:28:02.450837 True
和
df = df.set_index(pd.DatetimeIndex(df['Date']))
df.drop(["Date"], 1, inplace=True)
df
Condition
Date
2020-08-15 16:34:15.838169 False
2020-08-17 14:25:08.778913 True
2020-08-19 07:44:07.514456 False
2020-08-19 14:48:29.160890 True
2020-08-20 03:26:00.479444 False
2020-08-20 10:57:52.904366 False
2020-08-20 19:17:45.079390 True
2020-08-20 23:38:41.369156 False
2020-08-21 12:21:54.340702 True
2020-08-24 19:42:13.458472 False
2020-08-24 23:09:39.369394 True
2020-08-25 16:35:05.059722 False
2020-08-26 01:31:29.243435 True
2020-08-26 03:28:25.418322 True
2020-08-27 12:42:43.905486 True
2020-08-31 10:35:57.143843 False
2020-09-02 11:32:54.219081 True
2020-09-02 14:07:05.544261 False
2020-09-03 08:05:32.133082 False
2020-09-10 15:28:46.725916 True
2020-09-12 00:57:58.558055 True
2020-09-13 21:28:02.450837 True
df.resample('D').sum().plot()
推荐阅读
- python - 如何基于 pytorch 执行分布式模型?
- javascript - JavaScript:尝试将数组对象的内容显示到 InnerHTML
- c - 裸机STM32
- javascript - 如何使用样式化的 MUI 基于道具制作不同的组件变体?
- android - Android Navigation Component, onCreateAnimation() 弹回时获取 nextAnim '0'
- qt - 如何在 QML 中创建 CandlestickSet 对象实例?
- list - 如何使用地图而不是列表理解(Haskell)
- vaadin - vaadin 流程 ConfirmDialog addCancelListener
- html - 如何在 HTML/CSS 中创建列?
- python - 在 python (web2py) 上为所有具有相似名称的变量创建一个循环给我未排序的值