-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cec2c5d
commit c71f260
Showing
4 changed files
with
35 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,41 @@ | ||
|
||
```markdown | ||
@author jackzhenguo | ||
@desc | ||
@desc 完成数据下采样,步长小时调整为天 | ||
@tag | ||
@version | ||
@date 2020/03/20 | ||
``` | ||
|
||
182 如何完成数据下采样,调整步长由小时为天? | ||
|
||
步长为小时的时间序列数据,有没有小技巧,快速完成下采样,采集成按天的数据呢? | ||
先生成测试数据: | ||
```python | ||
import pandas as pd | ||
import numpy as np | ||
``` | ||
|
||
```python | ||
df = pd.DataFrame(np.random.randint(1,10,size=(240,3)), \ | ||
columns = ['商品编码','商品销量','商品库存']) | ||
``` | ||
|
||
```python | ||
df.index = pd.util.testing.makeDateIndex(240,freq='H') | ||
df | ||
``` | ||
|
||
生成 240 行步长为小时间隔的数据: | ||
|
||
![](../img/182-1.png) | ||
|
||
小技巧,使用 resample 方法,合并为天(D) | ||
```python | ||
day_df = df.resample("D")["商品销量"].sum().to_frame() | ||
day_df | ||
``` | ||
|
||
结果如下,10行,240小时,正好为 10 days: | ||
|
||
![](../img/182-2.png) |