【问题标题】:Python: Max date for each month from the list [duplicate]Python:列表中每个月的最大日期[重复]
【发布时间】:2021-01-10 03:01:32
【问题描述】:

我有一个包含日期的列表,该列表如下所示:

list = ['2020-04-05',
 '2020-04-12',
 '2020-04-19',
 '2020-04-26',
 '2020-05-03',
 '2020-05-10',
 '2020-05-17',
 '2020-05-24',
 '2020-05-31',
 '2020-06-07',
 '2020-06-14',
 '2020-06-21',
 '2020-06-28',
 '2020-07-05',
 '2020-07-12',
 '2020-07-19',
 '2020-07-26',
 '2020-08-02',
 '2020-08-09',
 '2020-08-16',
 '2020-08-23',
 '2020-08-30',
 '2020-09-06',
 '2020-09-20',
 '2020-09-13']

我想从列表中提取每个月的最高日期。我想要的输出如下所示:

Desired list = ['2020-04-26','2020-05-31','2020-06-28','2020-07-26','2020-08-30','2020-09-20']

我尝试使用Max() 查找当月的最大日期,但它给出了列表中的总体最大日期。有没有办法从列表中找到每个月的最大日期。

【问题讨论】:

  • 这是我能想到的最简洁的解决方案(使用 itertools.groupby 和 datetime.datetime)。 [list(g)[-1] for _, g in groupby(l, key=lambda x: datetime.strptime(x[:-3], '%Y-%m'))]

标签: python python-3.x pandas datetime


【解决方案1】:

方法如下:

dates = ['2020-04-05',
         '2020-04-12',
         '2020-04-19',
         '2020-04-26',
         '2020-05-03',
         '2020-05-10',
         '2020-05-17',
         '2020-05-24',
         '2020-05-31',
         '2020-06-07',
         '2020-06-14',
         '2020-06-21',
         '2020-06-28',
         '2020-07-05',
         '2020-07-12',
         '2020-07-19',
         '2020-07-26',
         '2020-08-02',
         '2020-08-09',
         '2020-08-16',
         '2020-08-23',
         '2020-08-30',
         '2020-09-06',
         '2020-09-20',
         '2020-09-13']


dct = {date.rsplit('-', 1)[0]: [] for date in dates} # Dict with keys as all the dates without the day number, and values as empty lists
for date in dates:
    y, m, d = date.split('-')
    dct[f'{y}-{m}'].append(int(d)) # Add all the days from the months into the empty lists
        
print([f'{ym}-{max(dct[ym])}' for ym in dct]) # Concat the year and months with the greatest date

输出:

['2020-04-26', '2020-05-31', '2020-06-28', '2020-07-26', '2020-08-30', '2020-09-20']

【讨论】:

    【解决方案2】:

    你可以

    • 使用date.fromisoformat将每个字符串日期转换为date对象
    • 按月份对值进行分组
    • 为每个月检索最大值
    • 转换回string
    from datetime import date
    from itertools import groupby
    from operator import methodcaller
    
    dates = map(date.fromisoformat, values)
    dates_pre_month = groupby(sorted(dates, key=lambda x: x.month), key=lambda x: x.month)
    
    max_per_month = (max(month_values) for month, month_values in dates_pre_month)
    max_per_month = list(map(methodcaller("isoformat"), max_per_month))
    

    【讨论】:

      【解决方案3】:

      你可以的

      s = pd.Series(pd.to_datetime(l))
      new_list = s.groupby(s.dt.strftime('%Y-%m')).max().tolist()
      new_list
      [Timestamp('2020-04-26 00:00:00'), Timestamp('2020-05-31 00:00:00'), Timestamp('2020-06-28 00:00:00'), Timestamp('2020-07-26 00:00:00'), Timestamp('2020-08-30 00:00:00'), Timestamp('2020-09-20 00:00:00')]
      

      【讨论】:

        猜你喜欢
        • 2020-10-30
        • 1970-01-01
        • 2015-12-13
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多