Pandas Rolling Window - datetime64[ns] are not implemented( pandas 滚动窗口 - datetime64[ns] 未实现)
问题描述
我正在尝试使用 Python/Pandas 构建一些图表.我有每秒采样的数据.这是一个示例:
索引、时间、值31362, 1975-05-07 07:59:18, 36.15161231363, 1975-05-07 07:59:19, 36.18136831364, 1975-05-07 07:59:20, 36.19719531365, 1975-05-07 07:59:21, 36.15141331366, 1975-05-07 07:59:22, 36.13800931367, 1975-05-07 07:59:23, 36.14296231368, 1975-05-07 07:59:24, 36.122680我需要创建各种窗口来查看数据.10、100、1000 等.不幸的是,当我尝试窗口化整个数据框时,出现以下错误...
NotImplementedError: 此 dtype datetime64[ns] 的滚动操作未实现我查看了以下文档:
I'm attempting to use Python/Pandas to build some charts. I have data that is sampled every second. Here is a sample:
Index, Time, Value
31362, 1975-05-07 07:59:18, 36.151612
31363, 1975-05-07 07:59:19, 36.181368
31364, 1975-05-07 07:59:20, 36.197195
31365, 1975-05-07 07:59:21, 36.151413
31366, 1975-05-07 07:59:22, 36.138009
31367, 1975-05-07 07:59:23, 36.142962
31368, 1975-05-07 07:59:24, 36.122680
I need to create a variety of windows to look at the data. 10, 100, 1000 etc. Unfortunately when I attempt to window the entire data frame I get the error below...
NotImplementedError: ops for Rolling for this dtype datetime64[ns] are not implemented
I checked out these docs: http://pandas.pydata.org/pandas-docs/stable/computation.html as a reference, and they appear to be doing this on date ranges. I did notice that the data type between what they have and what I have is different.
Is there an easy way to do this?
This is ideally what I'm trying to do:
tmp = data.rolling(window=2)
tmp.mean()
I'm using plotly to plot the raw data and then the windowed data on top of it. My goal is to find ideal windows for identifying cleaner trends in the data removing some of the noise.
Thanks!
Additional Notes:
I think I need to take my data from this format:
pandas.core.series.Series to this one:
pandas.tseries.index.DatetimeIndex
Setup
from StringIO import StringIO
import pandas as pd
text = """Index,Time,Value
31362,1975-05-07 07:59:18,36.151612
31363,1975-05-07 07:59:19,36.181368
31364,1975-05-07 07:59:20,36.197195
31365,1975-05-07 07:59:21,36.151413
31366,1975-05-07 07:59:22,36.138009
31367,1975-05-07 07:59:23,36.142962
31368,1975-05-07 07:59:24,36.122680"""
df = pd.read_csv(StringIO(text), index_col=0, parse_dates=[1])
df.rolling(2).mean()
NotImplementedError: ops for Rolling for this dtype datetime64[ns] are not implemented
First off, this is confirmation of @BrenBarn's comment and he should get the credit if he decides to post an answer. BrenBarn, if you decide to answer, I'll delete this post.
Explanation
Pandas has no idea what a rolling mean of date values ought to be. df.rolling(2).mean() is attempting to roll and average over both the Time and Value columns. The error is politely (or impolitely, depending on your perspective) telling you that you're trying something non-sensical.
Solution
Move the Time column to the index and then... well that's it.
df.set_index('Time').rolling(2).mean()
这篇关于 pandas 滚动窗口 - datetime64[ns] 未实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:pandas 滚动窗口 - datetime64[ns] 未实现
基础教程推荐
- 在 Python 中将货币解析为数字 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
