首页 > 解决方案 > 获取包含python中一个字符的年份月份

问题描述

我正在尝试获取带有年份的月份列表,例如 [“2019M10”、“2019M11”、“2019M12”、“2020M01”、“2020M02”、“2020M03”、“2020M04”、“2020M05”、“2020M06”、“ 2020M07”、“2020M08”、“2020M09”、“2020M10”、“2020M11”、“2020M12”、“2021M01”、“2021M02”、“2021M03”、“2021M04”、“2021M05”] 以下代码示例我正在使用

import datetime
import json
from_year = 2018
last_year = datetime.datetime.now().year
print(last_year)
year_list = list(range(from_year, last_year))
new_month = []
for all_year in year_list:
    all_months = [str(all_year)+'M'+str(i) for i in list(range(1,13))]
    all_months.extend(all_months)
print(all_months )
months = json.dumps(all_months)
print(months)

我没有得到想要的输出。

标签: pythonpandasdataframenumpybackend

解决方案


用于Timestamp.to_period实际年份和月份,创建PeriodIndexbyperiod_range然后将值转换为 format YYYYMmmby PeriodIndex.strftime

from_year = 2018
last_year = pd.to_datetime('now').to_period('m')
print(last_year)
2021-07

months = pd.period_range(from_year, last_year, freq='M').strftime('%YM%m').tolist()
print (months)
['2018M01', '2018M02', '2018M03', '2018M04', '2018M05', '2018M06', '2018M07', '2018M08',
 '2018M09', '2018M10', '2018M11', '2018M12', '2019M01', '2019M02', '2019M03', '2019M04',
 '2019M05', '2019M06', '2019M07', '2019M08', '2019M09', '2019M10', '2019M11', '2019M12', 
 '2020M01', '2020M02', '2020M03', '2020M04', '2020M05', '2020M06', '2020M07', '2020M08', 
 '2020M09', '2020M10', '2020M11', '2020M12', '2021M01', '2021M02', '2021M03', '2021M04', 
 '2021M05', '2021M06', '2021M07']

如果需要所有月份添加明年,然后切片的最后一个值months

from_year = 2018
last_year = pd.to_datetime('now').year + 1
print(last_year)
2022

months = pd.period_range(from_year, last_year, freq='M')[:-1].strftime('%YM%m').tolist()
print (months)
['2018M01', '2018M02', '2018M03', '2018M04', '2018M05', '2018M06', '2018M07', '2018M08', 
 '2018M09', '2018M10', '2018M11', '2018M12', '2019M01', '2019M02', '2019M03', '2019M04',
 '2019M05', '2019M06', '2019M07', '2019M08', '2019M09', '2019M10', '2019M11', '2019M12',
 '2020M01', '2020M02', '2020M03', '2020M04', '2020M05', '2020M06', '2020M07', '2020M08', 
 '2020M09', '2020M10', '2020M11', '2020M12', '2021M01', '2021M02', '2021M03', '2021M04', 
 '2021M05', '2021M06', '2021M07', '2021M08', '2021M09', '2021M10', '2021M11', '2021M12']

您使用扁平化嵌套列表理解的解决方案:

from_year = 2018
last_year = datetime.datetime.now().year
print(last_year)
2021

year_list = list(range(from_year, last_year))

months = [f'{all_year}M{i:02}' for all_year in year_list for i in list(range(1,13))]
print (months)

['2018M01', '2018M02', '2018M03', '2018M04', '2018M05', '2018M06', '2018M07', '2018M08',
 '2018M09', '2018M10', '2018M11', '2018M12', '2019M01', '2019M02', '2019M03', '2019M04', 
 '2019M05', '2019M06', '2019M07', '2019M08', '2019M09', '2019M10', '2019M11', '2019M12', 
 '2020M01', '2020M02', '2020M03', '2020M04', '2020M05', '2020M06', '2020M07', '2020M08', 
 '2020M09', '2020M10', '2020M11', '2020M12', '2021M01', '2021M02', '2021M03', '2021M04',
 '2021M05', '2021M06', '2021M07', '2021M08', '2021M09', '2021M10', '2021M11', '2021M12']

推荐阅读