def get_date_range():
    from_date_str, to_date_str = '2019-08-01', '2019-09-09'
    df_date_range = pd.date_range(from_date_str, to_date_str)
    df = pd.DataFrame(df_date_range, columns=['Date'])
    df['Date'] = df.apply(lambda x: x['Date'].strftime("%Y-%m-%d"), axis=1)
 
    from_date_str, to_date_str = '2019-08-01', '2019-09-09'
    df_date_range = pd.date_range(from_date_str, to_date_str, freq='MS')
    df = pd.DataFrame(df_date_range, columns=['Date'])
    df['Date'] = df.apply(lambda x: x['Date'].strftime("%Y-%m"), axis=1)
 
    from_date_str, to_date_str = '2019-08-01', '2019-09-09'
    df = pd.DataFrame()
    df['monday'] = pd.date_range(from_date_str, to_date_str, freq='W-MON')
    df['sunday'] = pd.date_range(from_date_str, to_date_str, freq='W-SUN')
    df['monday'] = df.apply(lambda x: x['monday'].strftime("%Y-%m-%d"), axis=1)
    df['sunday'] = df.apply(lambda x: x['sunday'].strftime("%Y-%m-%d"), axis=1)
 
 
if __name__ == '__main__':
    get_date_range()

参考:

https://stackoverflow.com/questions/53844922/pandas-date-range-on-a-weekly-basis-starting-with-a-particular-day-of-the-week

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.date_range.html

更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Jalen Chu 微信支付

微信支付

Jalen Chu 支付宝

支付宝

Jalen Chu 公众号

公众号