python - 获取包含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)
我没有得到想要的输出。
解决方案
用于Timestamp.to_period
实际年份和月份,创建PeriodIndex
byperiod_range
然后将值转换为 format YYYYMmm
by 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']
推荐阅读
- android - 我是否需要在我的应用程序的每个活动中添加 Firebase 分析代码?
- python - kivy:滚动视图滑动事件中的轮播变得很难
- python - 当我在不同的文件中访问某个函数时,我收到一条错误消息,指出该函数未定义
- c - GDB 仍然显示变量
即使在 gcc (v7 & v9.3.1) -O0 优化关闭之后 - javascript - threejs glft 模型奇怪的线条
- python - Python 正则表达式精确匹配三个字符
- python - CNN 模型在达到 50% 的准确率后过度拟合数据
- react-native - React Native - 如何创建正确的依赖检查/漏洞报告并处理它?
- php - 无法在 Codeigniter 中更新 RAW 查询
- python - 如果满足日期条件,如何打印消息?