Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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
Python 熊猫:生成一个充满一年中最后一天的时间序列_Python_Pandas_Time Series_Pandas Groupby - Fatal编程技术网

Python 熊猫:生成一个充满一年中最后一天的时间序列

Python 熊猫:生成一个充满一年中最后一天的时间序列,python,pandas,time-series,pandas-groupby,Python,Pandas,Time Series,Pandas Groupby,假设我有一个不规则间隔的时间序列 2010-01-04 88.82 2010-11-29 90.70 2010-12-01 90.09 2011-02-26 90.10 2011-08-01 90.55 2011-09-21 89.50 2012-04-01 89.06 2012-04-30 90.22 2012-05-03 90.21 我想从索引中创建另一个timeseries,其中列由一年的最后一个日期填充。因此,对于2010年的

假设我有一个不规则间隔的时间序列

2010-01-04   88.82 
2010-11-29   90.70 
2010-12-01   90.09  
2011-02-26   90.10 
2011-08-01   90.55  
2011-09-21   89.50  
2012-04-01   89.06 
2012-04-30   90.22  
2012-05-03   90.21
我想从索引中创建另一个timeseries,其中列由一年的最后一个日期填充。因此,对于2010年的日期,它将显示2010-12-01,对于2011年的日期,它将显示2011-09-21,等等。所需的输出是

2010-01-04   2010-12-01
2010-11-29   2010-12-01
2010-12-01   2010-12-01 
2011-02-26   2011-09-21
2011-08-01   2011-09-21 
2011-09-21   2011-09-21  
2012-04-01   2012-05-03
2012-04-30   2012-05-03 
2012-05-03   2012-05-03
我可以提取出索引并按年份进行分组

end_dates=[]
df_idx = df.index
year_df = df_idx.groupby(df_idx.year)
for yr in year_df.keys():
    end_dates.append(max(year_df[yr]))

这给了我一个年度结束日期的列表。但是,如何将这些结束日期与原始索引相关联,以获得所需的输出?

确保您的索引是datetimeindex对象

如果您有熊猫系列,您可以使用:

s.to_frame().assign(end_dates=s.groupby(s.index.year).transform(lambda x: x.index.max()))
或者,如果您已经有一个数据帧:

df.assign(end_dates=df.groupby(df.index.year)['A'].transform(lambda x: x.index.max()))
输出:

                1  end_dates
0                           
2010-01-04  88.82 2010-12-01
2010-11-29  90.70 2010-12-01
2010-12-01  90.09 2010-12-01
2011-02-26  90.10 2011-09-21
2011-08-01  90.55 2011-09-21
2011-09-21  89.50 2011-09-21
2012-04-01  89.06 2012-05-03
2012-04-30  90.22 2012-05-03
2012-05-03  90.21 2012-05-03

谢谢你,斯科特。我从赋值函数中得到这个错误。ValueError:传递的项目数错误2,放置意味着1。我的原始数据帧实际上是两列,因此我必须修改您的代码以提取一列并在赋值之前将其转换回数据帧,即df.assign(end_dates=df.iloc[:,0.)。to_frame().groupby(df.index.year)。transform(lambda x:x.index.max())是索引中的日期,如果不是,则可以设置_index,然后运行第二条语句。然后重置索引。是的,日期在索引中。如果日期不是索引,我不太明白你在想什么。感谢您的耐心。当您按原样运行第二条语句时会发生什么?它为assign提供了以下错误。ValueError:传递的项目数错误2,放置意味着1。