9

In Pandas, I am using dates with string format YYYY-MM-DD What is the quickest way to increment the date with the result in YYYY-MM-DD format?

d1 = '2018-02-10'

I want to increment it by 1 and get the result back as a string:

d1_inc = '2018-02-11'
jpp
  • 159,742
  • 34
  • 281
  • 339
afshin
  • 1,783
  • 7
  • 22
  • 39
  • 1
    What do you mean by "quick"? What have you tried so far? Please share some code. Make sure to clearify the question so that it is not oppinion based – Sven-Eric Krüger May 15 '18 at 10:44

5 Answers5

18

Pure Python

You can use the datetime module, part of the standard library. There are 3 steps:

  1. Convert string to datetime object via strptime.
  2. Add a day via timedelta.
  3. Convert resulting datetime object back to string via strftime.

Here's a demo:

from datetime import datetime, timedelta

x = '2017-05-15'
res = (datetime.strptime(x, '%Y-%m-%d') + timedelta(days=1)).strftime('%Y-%m-%d')

print(res)    
# 2017-05-16

Pandas

The equivalent steps can be performed using 3rd party Pandas:

x = '2017-05-15'

# choose some combination of below methods
res = (pd.Timestamp(x) + pd.DateOffset(days=1)).strftime('%Y-%m-%d')
res = (pd.to_datetime(x) + pd.Timedelta('1 day')).strftime('%Y-%m-%d')

print(res)
# 2017-05-16
jpp
  • 159,742
  • 34
  • 281
  • 339
3

Using pd.to_datetime, pd.TimeDelta and strftime:

fmt = '%Y-%m-%d'
(pd.to_datetime(<your series or column>, format=fmt) + pd.Timedelta('1 days')).dt.strftime(date_format=fmt)

Example

df = pd.DataFrame({'date': ['2017-04-02', '2017-04-23']})
fmt = '%Y-%m-%d'
>>> (pd.to_datetime(df.date, format=fmt) + pd.Timedelta('1 days')).dt.strftime(date_format=fmt)
0    2017-04-03
1    2017-04-24
Name: date, dtype: object
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
1

You can perform arithmetic operations with datetime and timedelta objects.

from datetime import datetime, timedelta

d = datetime(year=2018, month=3, day=1)
t = timedelta(days=1)

d + t
# datetime.datetime(2018, 3, 2, 0, 0)

d + t + t
# datetime.datetime(2018, 3, 3, 0, 0)

for i in range(30):
    d += 1

print(d)
# datetime.datetime(2018, 3, 31, 0, 0)
BcK
  • 2,548
  • 1
  • 13
  • 27
0

You mean like this

date = datetime.date(2015,5,15)
date += datetime.timedelta(days=1)
Anand Thati
  • 173
  • 1
  • 1
  • 9
0

It depends on what you mean by quickest. I'm assuming you mean the quickest way to program this, not the shortest program execution time (which might be relevant when you're doing lots of these).

from datetime import datetime, timedelta

original_date = '2018-5-15'

print('The original date is {}, the date one day later is: {}'.
       format(original_date, (datetime.strptime(original_date, '%Y-%m-%d') + 
       timedelta(days=1)).strftime('%Y-%m-%d')

Step by step version:

Create a datetime object from the string, note the string that shows python the formatting (see the documentation for more information)

dt = datetime.strptime(original_date, '%Y-%m-%d')

Add a day

dt += timedelta(days=1)

Reformat it back to the requested string

print(dt.strftime('%Y-%m-%d'))

That's all!

  • I meant the quickest execution time. – afshin May 15 '18 at 13:28
  • Ah cool, [check this answer](https://stackoverflow.com/questions/13468126/a-faster-strptime) for some ideas on how to speed up strp/ftime. Also parallelization will help a great deal (if you're not doing anything intrinsically serial with the data). Depending on your scope it might be worth investigating avoiding date/datetime objects altogether. And just hardcode the whole thing, including leap years. – Johan Rensink May 16 '18 at 05:38