Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas 熊猫:从截止级别选择最后日期_Pandas_Date_Datetime_Grouping - Fatal编程技术网

Pandas 熊猫:从截止级别选择最后日期

Pandas 熊猫:从截止级别选择最后日期,pandas,date,datetime,grouping,Pandas,Date,Datetime,Grouping,我有一份日期清单和截止日期:3月31日。我想查看3月31日之前的第一个日期,并将其作为一年中的最后一个日期,然后选择之前的最后3个日期,并将其放入1年。(例如:如果我们看1997年,1997年3月之前的第一个日期是:索引8,日期:1996-12-13。现在我想回顾3个日期,把它们放在同一年,1997年,这意味着索引7、6和5,与1995年相同,1995年3月之前的第一个日期是索引0,日期:1994-12-15,但索引0之前没有可用的数据) 我的数据如下所示: date 0 1994-

我有一份日期清单和截止日期:3月31日。我想查看3月31日之前的第一个日期,并将其作为一年中的最后一个日期,然后选择之前的最后3个日期,并将其放入1年。(例如:如果我们看1997年,1997年3月之前的第一个日期是:索引8,日期:1996-12-13。现在我想回顾3个日期,把它们放在同一年,1997年,这意味着索引7、6和5,与1995年相同,1995年3月之前的第一个日期是索引0,日期:1994-12-15,但索引0之前没有可用的数据)

我的数据如下所示:

    date
0   1994-12-15
1   1995-07-06
2   1995-09-13
3   1995-12-12
4   1996-03-14
5   1996-07-01
6   1996-09-17
7   1996-11-12
8   1996-12-13
9   1997-06-25
10  1997-09-10
11  1997-12-12
我想让它看起来如下:

    date        year
0   1994-12-15  1995
1   1995-07-06  1996
2   1995-09-13  1996
3   1995-12-12  1996
4   1996-03-14  1996
5   1996-07-01  1997
6   1996-09-17  1997
7   1996-11-12  1997
8   1996-12-13  1997
9   1997-06-25  1998
10  1997-09-10  1998
11  1997-12-12  1998
请让我知道,如果你有任何想法,我可以这样做

与helper
数据框一起使用,该数据框由最小年和最大年创建,并在按列
df1['year']截断之前回补值。

y = df['date'].dt.year
#added +-1 year (not necessary)
miny = y.min() - 1
maxy = y.max() + 2

df1 = pd.DataFrame({'date':pd.date_range(f'{miny}-03-31', f'{maxy}-03-31', freq='A-Mar')})
df1['year'] = df1['date'].dt.year  + 1
print (df1)
        date  year
0 1993-03-31  1994
1 1994-03-31  1995
2 1995-03-31  1996
3 1996-03-31  1997
4 1997-03-31  1998
5 1998-03-31  1999
6 1999-03-31  2000

df = pd.merge_asof(df,df1,on='date')
print (df)
         date  year
0  1994-12-15  1995
1  1995-07-06  1996
2  1995-09-13  1996
3  1995-12-12  1996
4  1996-03-14  1996
5  1996-07-01  1997
6  1996-09-17  1997
7  1996-12-12  1997
8  1996-12-13  1997
9  1997-06-25  1998
10 1997-09-10  1998
11 1997-12-12  1998