4 回答
TA贡献1831条经验 获得超4个赞
period_range
与将日期字符串转换为日期时间一起使用:
start = '1905' # May 2019
end = '2003' # March 2020
p = pd.period_range(pd.to_datetime(start, format="%y%m"),
pd.to_datetime(end, format="%y%m"), freq='M')
print (p)
PeriodIndex(['2019-05', '2019-06', '2019-07', '2019-08', '2019-09', '2019-10',
'2019-11', '2019-12', '2020-01', '2020-02', '2020-03'],
dtype='period[M]', freq='M')
然后用于PeriodIndex.strftime
自定义格式:
print (p.strftime('%B'))
Index(['May', 'June', 'July', 'August', 'September', 'October', 'November',
'December', 'January', 'February', 'March'],
dtype='object')
for val in p:
print (val)
TA贡献1847条经验 获得超11个赞
一个简单的解决方案是这样的:
months = {
'01': 'Jan',
'02': 'Feb',
'03': 'Mar',
'04': 'Apr',
'05': 'May',
'06': 'Jun',
'07': 'Jul',
'08': 'Aug',
'09': 'Sep',
'10': 'Oct',
'11': 'Nov',
'12': 'Dec'
}
start = '1905'
end = '2003'
for i, j in enumerate(range(int(start[0:2]), int(end[0:2])+1)):
if i == 0 and start[0:2] != end[0:2]:
s = int(start[2:4])
e = 12
if i > 0 and start[0:2] != end[0:2] and j != int(end[0:2]):
s = 1
e = 12
if i > 0 and start[0:2] != end[0:2] and j == int(end[0:2]):
s = 1
e = int(end[2:4]) + 1
if i == 0 and start[0:2] == end[0:2]:
s = int(start[2:4])
e = int(end[2:4]) + 1
for p in range(s, e):
index = '0' + str(p) if p < 10 else str(p)
print(f"Year: {j}, month {months[index]}")
print(f"{j}{index}")
输出:
Year: 19, month May
1905
Year: 19, month Jun
1906
Year: 19, month Jul
1907
Year: 19, month Aug
1908
Year: 19, month Sep
1909
Year: 19, month Oct
1910
Year: 19, month Nov
1911
Year: 20, month Jan
2001
Year: 20, month Feb
2002
Year: 20, month Mar
2003
TA贡献1824条经验 获得超5个赞
您可以使用date
及其格式:
from datetime import date, datetime
def parse(strg):
return datetime.strptime(strg, "%y%m").date()
start = parse("1905")
end = parse("2003")
dt = start
while dt <= end:
print(f"{dt:%y%m} - {dt:%B %Y}")
dt = date(year=dt.year + dt.month // 12, month=(dt.month % 12) + 1, day=1)
这将产生字符串
1905 - May 2019
1906 - June 2019
1907 - July 2019
1908 - August 2019
1909 - September 2019
1910 - October 2019
1911 - November 2019
1912 - December 2019
2001 - January 2020
2002 - February 2020
2003 - March 2020
date使用格式字符串格式化对象"%y%m"将返回您想要的格式。
TA贡献1775条经验 获得超11个赞
像这样的生成器函数应该可以解决问题。
def parse_yymm_string(yymm_string):
# Parse an `YYMM` string into a tuple of (Year, Month)
return (2000 + int(yymm_string[:2], 10), int(yymm_string[2:], 10))
def ym_pair_to_yymm(ym_pair):
# Convert a (Year, Month) tuple into an `YYMM` string
return f"{ym_pair[0] - 2000:02d}{ym_pair[1]:02d}"
def generate_yms(start, end):
parsed_end = parse_yymm_string(end)
curr_year, curr_month = parse_yymm_string(start)
while (curr_year, curr_month) <= parsed_end:
yield (curr_year, curr_month)
curr_month += 1
if curr_month > 12:
curr_year += 1
curr_month = 1
start = '1905' # May 2019
end = '2003' # March 2020
for ym in generate_yms('1905', '2003'):
print(ym, ym_pair_to_yymm(ym))
输出是
(2019, 5) 1905
(2019, 6) 1906
(2019, 7) 1907
(2019, 8) 1908
(2019, 9) 1909
(2019, 10) 1910
(2019, 11) 1911
(2019, 12) 1912
(2020, 1) 2001
(2020, 2) 2002
(2020, 3) 2003
添加回答
举报