【问题标题】:Pandas max dayofyear by yearPandas max dayofyear 逐年
【发布时间】:2019-01-01 21:00:52
【问题描述】:

我有一个带有日期时间索引的数据框。同一天有多个观察,但时间不同。

我熟悉 dayofyear 属性。有没有办法使用这个属性来确定每年的最大天数?结果会是这样的:

2015 252
2016 250
2017 251

【问题讨论】:

  • 你能提供一个示例数据吗?

标签: pandas datetime dataframe


【解决方案1】:

如果我理解您的问题,您希望查看日期列表并为每一年获取该年的最长日期。

 # Sample data
 df = pd.DataFrame({'date':pd.DatetimeIndex(start=pd.datetime(2018,12,24),end=pd.datetime(2019,1,2),freq='h')})
 df['dayofyear'] = df.date.dt.dayofyear
 df['year'] = df.date.dt.year
 df.groupby('year').dayofyear.max()

输出:

 year
 2018    365
 2019      2

【讨论】:

    猜你喜欢
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多