Find the end of the month Pandas DataFrame Series

后端 未结 2 1850
半阙折子戏
半阙折子戏 2020-11-28 05:55

I have a series within a DataFrame that I read in initially as an object, and then need to convert it to a date in the form of yyyy-mm-dd where dd is the end of the month.

2条回答
  •  旧巷少年郎
    2020-11-28 06:27

    You can use pandas.tseries.offsets.MonthEnd:

    from pandas.tseries.offsets import MonthEnd
    
    df['Date'] = pd.to_datetime(df['Date'], format="%Y%m") + MonthEnd(1)
    

    The 1 in MonthEnd just specifies to move one step forward to the next date that's a month end. (Using 0 or leaving it blank would also work in your case). If you wanted the last day of the next month, you'd use MonthEnd(2), etc. This should work for any month, so you don't need to know the number days in the month, or anything like that. More offset information can be found in the documentation.

    Example usage and output:

    df = pd.DataFrame({'Date': [200104, 200508, 201002, 201602, 199912, 200611]})
    df['EndOfMonth'] = pd.to_datetime(df['Date'], format="%Y%m") + MonthEnd(1)
    
         Date EndOfMonth
    0  200104 2001-04-30
    1  200508 2005-08-31
    2  201002 2010-02-28
    3  201602 2016-02-29
    4  199912 1999-12-31
    5  200611 2006-11-30
    

提交回复
热议问题