我每十分钟有一份日内价格的文件。一天0:41次。每次约会重复42次。下面的多个索引应该会将重复的日期“折叠”为一个时间。
[date, time, price]。换句话说,09:30是每天的第一次,16:20是最后一次:I不能在16:20-09:30之间的价格天之间的重叠差。差异应该从09:40-09:30开始,然后结束为16:20 - 16:10,对于dataframe中的每个唯一日期。
这是我的尝试。如有任何建议,将不胜感激。
def diffSeries(rounded,data):
'''This function accepts a column called rounded from 'data'
The 2nd input 'data' is a dataframe
'''
df=rounded.shift(1)
idf=data.set_index(['date', 'time'])
data['diff']=['000']
for i in range(0,length(rounded)):
for day in idf.index.levels[0]:
for time in idf.index.levels[1]:
if idf.index.levels[1]!=1620:
data['diff']=rounded[i]-df[i]
else:
day+=1
time+=2
data[['date','time','price','II','diff']].to_csv('final.csv')
return data['diff']然后我打电话:
data=read_csv('file.csv')
rounded=roundSeries(data['price'],5)
diffSeries(rounded,data)在追踪上-我得到了一个Assertion Error。
发布于 2012-11-17 01:52:15
您可以使用groupby,然后应用它来实现您想要的目标:
diffs = data.groupby(lambda idx: idx[0]).apply(lambda row: row - row.shift(1))对于完整的示例,假设您为11月14日至16日创建了一个测试数据集:
import pandas as pd
from numpy.random import randn
from datetime import time
# Create date range with 10 minute intervals, and filter out irrelevant times
times = pd.bdate_range(start=pd.datetime(2012,11,14,0,0,0),end=pd.datetime(2012,11,17,0,0,0), freq='10T')
filtered_times = [x for x in times if x.time() >= time(9,30) and x.time() <= time(16,20)]
prices = randn(len(filtered_times))
# Create MultiIndex and data frame matching the format of your CSV
arrays = [[x.date() for x in filtered_times]
,[x.time() for x in filtered_times]]
tuples = zip(*arrays)
m_index = pd.MultiIndex.from_tuples(tuples, names=['date', 'time'])
data = pd.DataFrame({'prices': prices}, index=m_index)您应该得到一个类似于这样的DataFrame:
prices
date time
2012-11-14 09:30:00 0.696054
09:40:00 -1.263852
09:50:00 0.196662
10:00:00 -0.942375
10:10:00 1.915207如前所述,然后可以通过将第一个索引分组,然后减去每一行的前一行来获得差异:
diffs = data.groupby(lambda idx: idx[0]).apply(lambda row: row - row.shift(1))这给了你这样的东西:
prices
date time
2012-11-14 09:30:00 NaN
09:40:00 -1.959906
09:50:00 1.460514
10:00:00 -1.139036
10:10:00 2.857582由于您是按日期分组,所以该函数不应用于16:20-09:30。
您可能需要考虑使用TimeSeries而不是DataFrame,因为它将使您在处理此类数据时具有更大的灵活性。假设您已经从CSV文件中加载了DataFrame,则可以轻松地将其转换为TimeSeries并执行类似的函数以获得差异:
dt_index = pd.DatetimeIndex([datetime.combine(i[0],i[1]) for i in data.index])
# or dt_index = pd.DatetimeIndex([datetime.combine(i.date,i.time) for i in data.index])
# if you don't have an multi-level index on data yet
ts = pd.Series(data.prices.values, dt_index)
diffs = ts.groupby(lambda idx: idx.date()).apply(lambda row: row - row.shift(1))但是,您现在可以访问内置的时间序列函数,例如重采样。有关熊猫时间序列的更多信息,请参见这里。
发布于 2012-12-10 23:07:33
@MattiJohn的建筑给出了一个经过过滤的长度为86,772-当运行超过1/3/2007-8/30/2012 42次(每隔10分钟)。观察数据清理问题。
在这里,来自csv的价格数据是长度: 62,034。因此,简单地从.csv导入是有问题的,如下所示:
filtered_times = [x for x in times if x.time() >= time(9,30) and x.time() <= time(16,20)]
DF=pd.read_csv('MR10min.csv')
prices = DF.price
# I.E. rather than the generic: prices = randn(len(filtered_times)) above.实际数据没有达到它“应该”的长度这一事实意味着存在数据清理问题。我们通常没有完整的时间,因为bdate_time会生成(市场上的半天,等等,节假日)。
你的解决方案很优雅。但我不知道如何克服实际数据与事先规定的数据之间的不匹配。
第二个TimesSeries建议似乎仍然需要构造一个与第一个类似的日期时间索引。例如,如果我使用以下两行来获取实际感兴趣的数据:
DF=pd.read_csv('MR10min.csv')
data=pd.DF.set_index(['date','time'])
dt_index = pd.DatetimeIndex([datetime.combine(i[0],i[1]) for i in data.index])它将产生一个:
TypeError: combine() argument 1 must be datetime.date, not str如何使bdate_time数组完全了解实际数据可用?
谢谢(@MattiJohn)和任何有兴趣继续这一讨论的人。
https://stackoverflow.com/questions/13416344
复制相似问题