如何在dataframe中添加缺少的日期,并在python中填充其他相应的列?

如何在dataframe中添加缺少的日期,并在python中填充其他相应的列?,python,pandas,Python,Pandas,我有一个这样的数据帧 id Date number 0 1Y 2005-01-07 1.0 1 1Y 2008-01-07 1.6 2 5Y 2005-01-07 1.0 3 5Y 2008-01-07 1.7 4 6Y 2005-01-07 6.0 5 6Y 2008-01-07 1.0 6 10Y 2005-

我有一个这样的数据帧

   id           Date     number
0  1Y     2005-01-07        1.0
1  1Y     2008-01-07        1.6
2  5Y     2005-01-07        1.0
3  5Y     2008-01-07        1.7
4  6Y     2005-01-07        6.0
5  6Y     2008-01-07        1.0
6  10Y    2005-01-07        2.0
7  10Y    2008-01-07        7.1
8  30Y    2005-01-07        5.5
9  30Y    2008-01-07        8.6
...
我想在date列中为每个
id
添加截至今天的缺失日期(逐日)

我试过了

df.set_index(df.Date, inplace=True)
df.resample('D').ffill().reset_index() 
但我无法实现我描述的输出

此外,如果可能的话,假设添加日期的相应数量为
NaN
,我想将这些
NaN
指定为等于其正上方的数字

期望输出:

   id           Date             number
0  1Y     2005-01-07                1.0
0  1Y     in between dates          1.0
1  1Y     2008-01-07                1.6
1  1Y     dates after               1.6

2  5Y     2005-01-07                1.0
2  5Y     in between dates          1.0
3  5Y     2008-01-07                1.7
3  5Y     dates after               1.7
...
8  30Y    2005-01-07                5.5
8  30Y    in between dates          1.0
9  30Y    2008-01-07                8.6
9  30Y    dates after               1.7

说明问题的另一种方法是: 我还可以按如下方式重写数据帧:

   id    2005-01-07   2008-01-07    ...
0  1Y           1.0          1.6
1  5Y           1.0          1.7
2  6Y           6.0          1.0
3  10Y          2.0          7.1
4  30Y          5.5          8.6
我想实现:

   id    2005-01-07   Date missing    2008-01-07    ...    Date today
0  1Y           1.0            NaN           1.6                  NaN
1  5Y           1.0            NaN           1.7                  NaN
2  6Y           6.0            NaN           1.0                  NaN
3  10Y          2.0            NaN           7.1                  NaN
4  30Y          5.5            NaN           8.6                  NaN
然后用左边现有的数字填写NaN。

好的,我现在明白了

首先,我们需要根据每个ID将今天的日期添加到当前数据帧中。然后应用groupby和重采样操作

df = pd.read_clipboard(sep='\s+')
df['Date'] = pd.to_datetime(df['Date'])

df1 = pd.concat(
    [df, df[["id"]].drop_duplicates().assign(Date=pd.Timestamp("today").normalize())]
)
df1 = df1.set_index("Date")

df2 = df1.groupby(['id'],as_index=False).resample('D').bfill().ffill().reset_index(1)


如果我理解了您试图正确执行的操作,听起来您可以在Pandascan中使用方法测试
df1=df.groupby(['id'],as_index=False)。重新采样('D').ffill()
@datanovel我收到一条错误消息,“仅对DatetimeIndex TimedeltaIndex或PeriodIndex有效,但得到了'Int64Index'的实例。我以为您已将日期添加到索引中
df.set_index(df.Date,inplace=True)
还要确保它是一个datetime
df['Date']=pd.to_datetime(df['Date'])
@datanovel哦,如果忘记了
,那么是的。set_index
。是的,Date.dtype是datetime(我只是没有简单地将时间部分放在示例中)。这是可行的,但在我的原始df
max(df.date)
中填充所有日期,直到最后一个日期。我怎样才能把它填到今天的日期呢?它是有效的,但有一个问题。仅发生在今天的日期“2020-05-21”值没有填充,它们是NaN。
bfill
工作,除了数据帧的最后一个元素,如果将
.ffill().bfill()
的顺序颠倒到
.bfill().ffill()
则最后一个元素的问题得到解决。
df2[df2['id'] == '1Y']['Date'].max()
Timestamp('2020-05-21 00:00:00')