简体   繁体   English

无法重新采样然后绘制熊猫数据框

[英]Unable to resample then plot a Pandas Data Frame

I have been trying to plot a simple resampled data that is coming from a Pandas dataframe. 我一直在尝试绘制来自熊猫数据帧的简单resampled数据。 Here is my initial code: 这是我的初始代码:

import pandas as pd
import numpy as np
from datetime import datetime, timedelta

# Extra plotly bits
import plotly
import plotly.plotly as py
import plotly.graph_objs as go

date_today = datetime.now()
days = pd.date_range(date_today, date_today + timedelta(56), freq='D')

np.random.seed(seed=1111)
data = np.random.randint(1, high=100, size=len(days))
df = pd.DataFrame({'date': days, 'value': data})

When I do print df I get this: 当我print df我得到了:

                         date  value
0  2017-10-28 17:13:23.867396     29
1  2017-10-29 17:13:23.867396     56
2  2017-10-30 17:13:23.867396     82
3  2017-10-31 17:13:23.867396     13
4  2017-11-01 17:13:23.867396     35
5  2017-11-02 17:13:23.867396     53
6  2017-11-03 17:13:23.867396     25
7  2017-11-04 17:13:23.867396     23
8  2017-11-05 17:13:23.867396     21
9  2017-11-06 17:13:23.867396     12
10 2017-11-07 17:13:23.867396     15
...
48 2017-12-15 17:13:23.867396      1
49 2017-12-16 17:13:23.867396     88
50 2017-12-17 17:13:23.867396     94
51 2017-12-18 17:13:23.867396     48
52 2017-12-19 17:13:23.867396     26
53 2017-12-20 17:13:23.867396     65
54 2017-12-21 17:13:23.867396     53
55 2017-12-22 17:13:23.867396     54
56 2017-12-23 17:13:23.867396     76

And I can plot this easily (the red line in the example image below). 而且我可以轻松地绘制它(下面的示例图中的红线)。 However, the problems start when I attempt to create an extra data layer, which is a down-sampled version of the value/date relation, as in skipping every 5 days and and then plotting that. 但是,当我尝试创建一个额外的数据层时,问题就开始了,该数据层是值/日期关系的下采样版本,例如每隔5天跳过一次,然后作图。

For that, I create a sampled copy of my data frame with: 为此,我使用以下方法创建数据框的样本副本:

df_sampled = df.set_index('date').resample('5D').mean()

And when I do print df_sampled , I get: 当我print df_sampled ,我得到:

                            value
date
2017-10-28 17:32:39.622881   43.0
2017-11-02 17:32:39.622881   26.8
2017-11-07 17:32:39.622881   26.6
2017-11-12 17:32:39.622881   59.4
2017-11-17 17:32:39.622881   66.8
2017-11-22 17:32:39.622881   33.6
2017-11-27 17:32:39.622881   27.8
2017-12-02 17:32:39.622881   64.4
2017-12-07 17:32:39.622881   43.2
2017-12-12 17:32:39.622881   64.4
2017-12-17 17:32:39.622881   57.2
2017-12-22 17:32:39.622881   65.0

And after that, I cannot really plot this anymore, the column seems to be broken. 之后,我再也无法真正绘制该图了,该列似乎已损坏。 With the plotly: 随着:

    x = df_sampled['date'],
    y = df_sampled['value'],

I get this error: 我收到此错误:

File "interpolation.py", line 36, in <module>
    x = df_sampled['date'],
...
KeyError: 'date'

How can I fix this this. 我该如何解决这个问题。 Basically, I am trying to create this image. 基本上,我正在尝试创建此图像。 Red line is my original data and the blue one is the down-sampled, and smoothed version. 红线是我的原始数据,蓝线是降采样后的平滑版本。

在此处输入图片说明

--- UPDATE --- -更新-

The Answer provided below works, and I am getting the following result: 下面提供的答案有效,并且我得到以下结果:

在此处输入图片说明

date is not column, but index , so need: date不是列,而是index ,因此需要:

x = df_sampled.index
y = df_sampled['value']

Or create column from index by reset_index : 或者通过reset_indexindex创建列:

df_sampled = df.set_index('date').resample('5D').mean().reset_index()
#alternative
#df_sampled = df.resample('5D', on='date').mean().reset_index()

x = df_sampled['date']
y = df_sampled['value']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM