Pandas add one day to column

No need to turn into an index. Just using .apply() works:

df['newdate'] = pd.to_datetime(df['date']).apply(pd.DateOffset(1))

I think that the cleanest way to do this is a variant of szu's answer. Pandas has nearly full support datetime built into its functionality, so there is no need to load datetime; instead, if you are already using pandas, create the new column like this:

mondist['shifted_date'] = mondist.date + pd.Timedelta(days=1)

Try to use timedelta():

mondist['shifted_date']=mondist.date + datetime.timedelta(days=1)

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]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[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]: 
<class 'pandas.tseries.index.DatetimeIndex'>
[2013-11-21 01:00:00, 2013-11-22 01:00:00]
Length: 2, Freq: None, Timezone: Non

Tags:

Python

Pandas