首页 > 解决方案 > 使用python从给定列表中每个月的最后一个交易日

问题描述

日期=['2010-01-11''2010-01-12''2010-01-13''2010-01-14''2010-01-15''2010-01-16''2010-01- 17''2010-01-18''2010-01-19''2010-01-20''2010-01-21''2010-01-22''2010-01-23''2010-01-24 ' '2010-01-25' '2010-01-26' '2010-01-27' '2010-01-28' '2010-01-29' '2010-01-30' '2010-01-31' '2010-02-01' '2010-02-02' '2010-02-03' '2010-02-04' '2010-02-05' '2010-02-06' '2010-02-07' ' 2010-02-08' '2010-02-09' '2010-02-10' '2010-02-11' '2010-02-12' '2010-02-13' '2010-02-14' '2010 -02-15' '2010-02-16' '2010-02-17' '2010-02-18' '2010-02-19' '2010-02-20' '2010-02-21' '2010-02-22' '2010-02-23' '2010-02-24' '2010-02-25' '2010-02-26' '2010 -02-27' '2010-02-28' '2010-03-01' '2010-03-02' '2010-03-03' '2010-03-04' '2010-03-05' '2010- 03-06' '2010-03-07' '2010-03-08' '2010-03-09' '2010-03-10' '2010-03-11' '2010-03-12' '2010-03 -13''2010-03-14''2010-03-15''2010-03-16''2010-03-17''2010-03-18''2010-03-19''2010-03- 20''2010-03-21''2010-03-22''2010-03-23''2010-03-24''2010-03-25''2010-03-26''2010-03-27' ' '2010-03-28' '2010-03-29' '2010-03-30' '2010-03-31' '2010-04-01''2010-04-02' '2010-04-03' '2010-04-04' '2010-04-05' '2010-04-06' '2010-04-07' '2010-04-08' ' 2010-04-09' '2010-04-10' '2010-04-11' '2010-04-12' '2010-04-13' '2010-04-14' '2010-04-15' '2010 -04-16' '2010-04-17' '2010-04-18' '2010-04-19' '2010-04-20' '2010-04-21' '2010-04-22' '2010- 04-23' '2010-04-24' '2010-04-25' '2010-04-26' '2010-04-27' '2010-04-28' '2010-04-29' '2010-04 -30''2010-05-01''2010-05-02''2010-05-03''2010-05-04''2010-05-05''2010-05-06''2010-05- 07' '2010-05-08' '2010-05-09' '2010-05-10' '2010-05-11' '2010-05-12' '2010-05-13' '2010-05-14' '2010-05-15' '2010-05-16' '2010-05-17' '2010-05-18' '2010 -05-19' '2010-05-20' '2010-05-21' '2010-05-22' '2010-05-23' '2010-05-24' '2010-05-25' '2010- 05-26' '2010-05-27' '2010-05-28' '2010-05-29' '2010-05-30' '2010-05-31' '2010-06-01' '2010-06 -02''2010-06-03''2010-06-04''2010-06-05''2010-06-06''2010-06-07''2010-06-08''2010-06- 09' '2010-06-10' '2010-06-11' '2010-06-12' '2010-06-13' '2010-06-14' '2010-06-15' '2010-06-16 ' '2010-06-17' '2010-06-18' '2010-06-19' '2010-06-20' '2010-06-21''2010-06-22' '2010-06-23' '2010-06-24' '2010-06-25' '2010-06-26' '2010-06-27' '2010-06-28' ' 2010-06-29' '2010-06-30']

似乎无法弄清楚上述列表中提取每个月最后一天的编码。请注意,上述列表中每个月的最后一天不一定等于每个日历月的最后一天。

预期输出:['2010-01-29', '2010-02-26', '2010-03-31', '2010-04-30', '2010-05-28', '2010-06-30 ']

看到一些解决方案如下,但它没有返回有效结果: date = date - pd.tseries.offsets.MonthEnd()

标签: python-3.xdatetimenumpy-slicing

解决方案


previous_month = '01'
last_trading_days = []

for index, day in enumerate(date):
    # Extract month from date
    month = day[5:7]
    
    # If this is the first day of the new month, append the day that came before it
    if month != previous_month:
        previous_month = month
        last_trading_days.append(date[index - 1])
    
    # Also append the last day
    if index == len(date) - 1:
        last_trading_days.append(day)
        
print(last_trading_days)

这是如果您知道第一个月将是一月,否则您可以使用previous_month = date[0][5:7]从列表中第一个日期的月份开始。


推荐阅读