Numbers of Day in Month

后端 未结 4 917
再見小時候
再見小時候 2020-12-11 05:03

I have a data frame with a date time index, and I would like to multiply some columns with the number of days in that month.

                   TUFNWGTP  TEL         


        
4条回答
  •  醉话见心
    2020-12-11 05:45

    pd.tslib.monthrange is an unadvertised / undocumented function that handles the days_in_month calculation (adjusting for leap years). This could/should prob be added as a property to Timestamp/DatetimeIndex.

    In [34]: df = DataFrame({'date' : pd.date_range('20120101',periods=15,freq='M') })
    
    In [35]: df['year'] = df['date'].dt.year
    
    In [36]: df['month'] = df['date'].dt.month
    
    In [37]: df['days_in_month'] = df.apply(lambda x: pd.tslib.monthrange(x['year'],x['month'])[1], axis=1)
    
    In [38]: df
    Out[38]: 
             date  year  month  days_in_month
    0  2012-01-31  2012      1             31
    1  2012-02-29  2012      2             29
    2  2012-03-31  2012      3             31
    3  2012-04-30  2012      4             30
    4  2012-05-31  2012      5             31
    5  2012-06-30  2012      6             30
    6  2012-07-31  2012      7             31
    7  2012-08-31  2012      8             31
    8  2012-09-30  2012      9             30
    9  2012-10-31  2012     10             31
    10 2012-11-30  2012     11             30
    11 2012-12-31  2012     12             31
    12 2013-01-31  2013      1             31
    13 2013-02-28  2013      2             28
    14 2013-03-31  2013      3             31
    

提交回复
热议问题