考虑这个简单的例子
data = pd.DataFrame({'mydate' : [pd.to_datetime('2016-06-06'),
pd.to_datetime('2016-06-02')],
'value' : [1, 2]})
data.set_index('mydate', inplace = True)
data
Out[260]:
value
mydate
2016-06-06 1
2016-06-02 2
我想迭代每一行,以便数据框在当前行的每个索引值(这是一个日期)周围“放大”几天(前2天,后2天)。
例如,如果你考虑第一行,我想告诉Pandas再添加4行,对应于天2016-06-04
,2016-06-05
,2016-06-07
和2016-06-07
。这些额外行的value
可能只是在value
中的那一行(在这种情况下:1)。每行应用此逻辑,最终数据帧是所有这些放大数据帧的串联。
我在apply(., axis = 1)
中尝试了以下功能:
def expand_onerow(df, ndaysback = 2, nhdaysfwd = 2):
new_index = pd.date_range(pd.to_datetime(df.name) - pd.Timedelta(days=ndaysback),
pd.to_datetime(df.name) + pd.Timedelta(days=nhdaysfwd),
freq='D')
newdf = df.reindex(index=new_index, method='nearest') #New df with expanded index
return newdf
但不幸的是,我运行data.apply(lambda x: expand_onerow(x), axis = 1)
给出:
File "pandas/_libs/tslib.pyx", line 1165, in pandas._libs.tslib._Timestamp.__richcmp__
TypeError: ("Cannot compare type 'Timestamp' with type 'str'", 'occurred at index 2016-06-06 00:00:00')
我尝试的另一种方法如下:我首先重置索引,
data.reset_index(inplace = True)
data
Out[339]:
mydate value
0 2016-06-06 1
1 2016-06-02 2
然后我稍微修改了我的功能
def expand_onerow_alt(df, ndaysback = 2, nhdaysfwd = 2):
new_index = pd.date_range(pd.to_datetime(df.mydate) - pd.Timedelta(days=ndaysback),
pd.to_datetime(df.mydate) + pd.Timedelta(days=nhdaysfwd),
freq='D')
newdf = pd.Series(df).reindex(index = new_index).T #New df with expanded index
return newdf
这使
data.apply(lambda x: expand_onerow_alt(x), axis = 1)
Out[338]:
2016-05-31 2016-06-01 2016-06-02 2016-06-03 2016-06-04 2016-06-05 2016-06-06 2016-06-07 2016-06-08
0 nan nan nan nan nan nan nan nan nan
1 nan nan nan nan nan nan nan nan nan
离得更近但还没到......
我不明白这里有什么问题。我错过了什么?我在这里寻找最潘迪克式的方法。
谢谢!
我修改了你的一点功能
def expand_onerow(df, ndaysback = 2, nhdaysfwd = 2):
new_index = pd.date_range(pd.to_datetime(df.index[0]) - pd.Timedelta(days=ndaysback),
pd.to_datetime(df.index[0]) + pd.Timedelta(days=nhdaysfwd),
freq='D')
newdf = df.reindex(index=new_index, method='nearest') #New df with expanded index
return newdf
pd.concat([expand_onerow(data.loc[[x],:], ndaysback = 2, nhdaysfwd = 2) for x ,_ in data.iterrows()])
Out[455]:
value
2016-05-31 2
2016-06-01 2
2016-06-02 2
2016-06-03 2
2016-06-04 2
2016-06-04 1
2016-06-05 1
2016-06-06 1
2016-06-07 1
2016-06-08 1
更多信息
基本上一行等于
l=[]
for x ,_ in data.iterrows():
l.append(expand_onerow(data.loc[[x],:], ndaysback = 2, nhdaysfwd = 2))# query out each row by using their index(x is the index for each row) and append then into a empty list
pd.concat(l)# concat the list to one df at the end