我想根据时间(1天,2天)用不同的标签回填每一列。
这是代码:
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
import random
np.random.seed(11)
date_today = datetime.now()
ndays = 15
df = pd.DataFrame({'date': [date_today + timedelta(days=x) for x in range(ndays)],
'test': pd.Series(np.random.randn(ndays)), 'test2':pd.Series(np.random.randn(ndays))})
df = df.set_index('date')
df = df.mask(np.random.random(df.shape) < .7)
print(df) # this will be the dataset that I generate for this question
# my orginal data set have labels that is why I convert it to str
df['test']=df['test'].astype(str)
df['test2']=df['test2'].astype(str)
df.replace('nan', np.nan, inplace = True)
for I in df.dropna().index.values:
end=I
start=end-np.timedelta64(24,'h')
start2=end-np.timedelta64(48,'h')
df[(df.index >= start) & (df.index <= end)]=df[(df.index >= start) & (df.index <= end)].bfill()
我的初始数据集将如下所示:
test test2
date
2018-03-07 11:28:23.028856 NaN NaN
2018-03-08 11:28:23.028856 NaN NaN
2018-03-09 11:28:23.028856 -0.484565 1.574634
2018-03-10 11:28:23.028856 -2.653319 NaN
2018-03-11 11:28:23.028856 NaN NaN
2018-03-12 11:28:23.028856 NaN NaN
2018-03-13 11:28:23.028856 -0.536629 NaN
2018-03-14 11:28:23.028856 NaN 0.725752
2018-03-15 11:28:23.028856 NaN 1.549072
2018-03-16 11:28:23.028856 -1.065603 0.630080
2018-03-17 11:28:23.028856 NaN NaN
2018-03-18 11:28:23.028856 -0.475733 0.732271
2018-03-19 11:28:23.028856 NaN -0.642575
2018-03-20 11:28:23.028856 NaN -0.178093
2018-03-21 11:28:23.028856 NaN -0.573955
我想要得到的是这样的:
我尝试了不同的方法,但找不到 bfill 的方法,bfill 不获取任何值参数,而 fillna 只获取方法或值。
test test2
date
2018-03-07 11:28:23.028856 -0.484565_2D 1.574634_2D
2018-03-08 11:28:23.028856 -0.484565_D 1.574634_D
2018-03-09 11:28:23.028856 -0.484565 1.574634
2018-03-10 11:28:23.028856 -2.653319 NaN
2018-03-11 11:28:23.028856 -0.536629_2D NaN
2018-03-12 11:28:23.028856 -0.536629_D 0.725752_2D
2018-03-13 11:28:23.028856 -0.536629 0.725752_D
2018-03-14 11:28:23.028856 -1.065603_2D 0.725752
2018-03-15 11:28:23.028856 -1.065603_D 1.549072
2018-03-16 11:28:23.028856 -1.065603 0.630080
2018-03-17 11:28:23.028856 -0.475733_D 0.732271_D
2018-03-18 11:28:23.028856 -0.475733 0.732271
2018-03-19 11:28:23.028856 NaN -0.642575
2018-03-20 11:28:23.028856 NaN -0.178093
2018-03-21 11:28:23.028856 NaN -0.573955
Update:我的原始数据集的时间戳不统一,因此此代码创建类似的时间戳:
date_today = datetime.now()
ndays = 15
df = pd.DataFrame({'date': [date_today + timedelta(days=(abs(np.random.randn(1))*2)[0]*x) for x in range(ndays)],
'test': pd.Series(np.random.randn(ndays)), 'test2':pd.Series(np.random.randn(ndays))})
df1=pd.DataFrame({'date': [date_today + timedelta(hours=x) for x in range(ndays)],
'test': pd.Series(np.random.randn(ndays)), 'test2':pd.Series(np.random.randn(ndays))})
df2=pd.DataFrame({'date': [date_today + timedelta(days=x)-timedelta(seconds=100*x) for x in range(ndays)],
'test': pd.Series(np.random.randn(ndays)), 'test2':pd.Series(np.random.randn(ndays))})
df=df.append(df1)
df=df.append(df2)
df = df.set_index('date')
df = df.mask(np.random.random(df.shape) < .7)
print(df) # this will be the dataset that I generate for this question
# my orginal data set have labels that is why I convert it to str
df['test']=df['test'].astype(str)
df['test2']=df['test2'].astype(str)
df.replace('nan', np.nan, inplace = True)
如果有人能帮助我,我真的很感激
提前致谢。