以pandas时间格式存储日期时,可以通过如下方式获取一个datetimeIndex内每个月的最后一天

# daily_price是一个以datetimeIndex为index的DataFrame或Series
tmp_dict = daily_price.index.groupby(daily_price.index.month)
[max(tmp_dict[month]) for month in tmp_dict]

如果是以datatimeIndex作为index,想在数据上操作(比如取一批数据在每个月末的平均值),过滤出每个月最后一天的相关数据

from pandas.tseries.offsets import MonthEnd
offset = MonthEnd()
daily_price.groupby(offset.rollforward).apply(lambda t: t[t.index == t.index.max()])

相关文章:

  • 2021-06-16
  • 2021-09-23
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-17
  • 2021-08-27
  • 2022-12-23
  • 2021-09-11
  • 2022-12-23
  • 2021-12-03
  • 2022-12-23
相关资源
相似解决方案