Pandas add one day to column

后端 未结 6 1855
情歌与酒
情歌与酒 2020-12-24 05:10

I need to add 1 day to each date I want to get the begining date of the following month eg 2014-01-2014 for the 1st item in the dataframe. Tried:

montdist[\         


        
6条回答
  •  甜味超标
    2020-12-24 05:43

    Make it a DatetimeIndex first:

    pd.DatetimeIndex(montdist['date']) + pd.DateOffset(1)
    

    Note: I think there is a feature request that this could work with date columns...

    In action:

    In [11]: df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
    
    In [12]: df['date'] = pd.to_datetime(['21-11-2013', '22-11-2013'])
    
    In [13]: pd.DatetimeIndex(df.date) + pd.DateOffset(1)
    Out[13]: 
    
    [2013-11-22 00:00:00, 2013-11-23 00:00:00]
    Length: 2, Freq: None, Timezone: None
    
    In [14]: pd.DatetimeIndex(df.date) + pd.offsets.Hour(1)
    Out[14]: 
    
    [2013-11-21 01:00:00, 2013-11-22 01:00:00]
    Length: 2, Freq: None, Timezone: Non
    

提交回复
热议问题