我有一张桌子,我需要在15分钟的滚动时间内计算“最后一个值”的最大值。预期列是“ MAX”列。我想在15分钟的时间间隔内获得最后值的最大值。我没有谈论石斑鱼15分钟。但如您在[Max]栏中看到的那样,滚动超过15分钟,这是我的预期输出。我希望这是在python中。
我尝试过:
df2 = df.resample('15min').fillna(np.nan)
df2 = df2['Last'].rolling('15min').max()
df.join(df2,rsuffix='2')
但是我得到fillna错误,还有一些我缺少逻辑的地方。不知道可能在哪里。
row Timestamp Last MAX
1 1/20/19 12:15
2 1/20/19 12:17 3071.56
3 1/20/19 12:17 3097.82
4 1/20/19 12:18 3097.82
5 1/20/19 12:19 3000
6 1/20/19 12:20 3002
7 1/20/19 12:21 3001
8 1/20/19 12:22 3000
9 1/20/19 12:23 3001
10 1/20/19 12:24 3001
11 1/20/19 12:25 3002
12 1/20/19 12:26 3003
13 1/20/19 12:27 3004
14 1/20/19 12:29 3003
15 1/20/19 12:29 3003
16 1/20/19 12:30 2000 3097.82(o2:o16)
17 1/20/19 12:31 3000 3097.82(o3:o17)
18 1/20/19 12:32 3000 3097.82(o4:o18)
错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-94-20b68e6d5add> in <module>()
8 #df[c] = df['ChangeDirection'].eq(c).rolling('15min').sum()
9 #df.loc[:df.index[0] + pd.Timedelta(16*60, unit="s"), cols] = np.nan
---> 10 df2 = df.resample('15min').fillna(np.nan)
11 #df2 = df2['Last'].rolling('15min').max()
12 #df.join(df2,rsuffix='2')
/usr/local/lib/python3.6/site-packages/pandas/core/resample.py in fillna(self, method, limit)
679 .. [1] https://en.wikipedia.org/wiki/Imputation_(statistics)
680 """
--> 681 return self._upsample(method, limit=limit)
682
683 @Appender(_shared_docs['interpolate'] % _shared_docs_kwargs)
/usr/local/lib/python3.6/site-packages/pandas/core/resample.py in _upsample(self, method, limit, fill_value)
966 else:
967 result = obj.reindex(res_index, method=method,
--> 968 limit=limit, fill_value=fill_value)
969
970 result = self._apply_loffset(result)
/usr/local/lib/python3.6/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
184 @wraps(func)
185 def wrapper(*args, **kwargs):
--> 186 return func(*args, **kwargs)
187
188 if not PY2:
/usr/local/lib/python3.6/site-packages/pandas/core/frame.py in reindex(self, *args, **kwargs)
3561 kwargs.pop('axis', None)
3562 kwargs.pop('labels', None)
-> 3563 return super(DataFrame, self).reindex(**kwargs)
3564
3565 @Appender(_shared_docs['reindex_axis'] % _shared_doc_kwargs)
/usr/local/lib/python3.6/site-packages/pandas/core/generic.py in reindex(self, *args, **kwargs)
3648 # construct the args
3649 axes, kwargs = self._construct_axes_from_arguments(args, kwargs)
-> 3650 method = missing.clean_reindex_fill_method(kwargs.pop('method', None))
3651 level = kwargs.pop('level', None)
3652 copy = kwargs.pop('copy', True)
/usr/local/lib/python3.6/site-packages/pandas/core/missing.py in clean_reindex_fill_method(method)
595
596 def clean_reindex_fill_method(method):
--> 597 return clean_fill_method(method, allow_nearest=True)
598
599
/usr/local/lib/python3.6/site-packages/pandas/core/missing.py in clean_fill_method(method, allow_nearest)
92 msg = ('Invalid fill method. Expecting {expecting}. Got {method}'
93 .format(expecting=expecting, method=method))
---> 94 raise ValueError(msg)
95 return method
96
ValueError: Invalid fill method. Expecting pad (ffill), backfill (bfill) or nearest. Got nan