最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何獲取日期之間的周期?

如何獲取日期之間的周期?

至尊寶的傳說 2024-01-15 17:08:43
我有兩個月的時間,從開始到結束。我想迭代之間的所有月份,即我想將所有月份提取為循環(huán)中的字符串。start = '1905' # May 2019end = '2003' # March 2020for month in range(start, end):     # I want to get string of each month in same format here有人可以幫助我干凈有效地獲得這個嗎?我的一年總是在(2000-2099)之間
查看完整描述

4 回答

?
慕容708150

TA貢獻1831條經(jīng)驗 獲得超4個贊

period_range與將日期字符串轉(zhuǎn)換為日期時間一起使用:

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)


查看完整回答
反對 回復 2024-01-15
?
回首憶惘然

TA貢獻1847條經(jīng)驗 獲得超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


查看完整回答
反對 回復 2024-01-15
?
滄海一幻覺

TA貢獻1824條經(jīng)驗 獲得超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)

?

這將產(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使用格式字符串格式化對象"%y%m"將返回您想要的格式。


查看完整回答
反對 回復 2024-01-15
?
繁星淼淼

TA貢獻1775條經(jīng)驗 獲得超11個贊

像這樣的生成器函數(shù)應該可以解決問題。


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


查看完整回答
反對 回復 2024-01-15
  • 4 回答
  • 0 關注
  • 229 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號