4 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊
period_range
與將日期字符串轉(zhuǎn)換為日期時(shí)間一起使用:
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貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊
一個(gè)簡(jiǎn)單的解決方案是這樣的:
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貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超5個(gè)贊
您可以使用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)
?
這將產(chǎn)生字符串
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使用格式字符串格式化對(duì)象"%y%m"將返回您想要的格式。

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
像這樣的生成器函數(shù)應(yīng)該可以解決問題。
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
添加回答
舉報(bào)