1

I've got a dataframe that has a column of dates that are formatted like this:

"1/1/2016"

I would like to create a for-loop that starts from that date and goes to "1/2/2016", "1/3/2016", and so on.

I start with this Python code:

df = pd.read_csv('myfile.csv')
df['dates'] = pd.to_datetime(df['dates'])

Which turns the date format to this: '01-01-2016' Next:

start_date = '01-01-2016'

Finally, the for-loop:

for j in range (1,30)
    start_date + j...

But you can't add an integer to that date format, of course. What should I do in the for-loop to just go to the next day?

pandashelp
  • 13
  • 1
  • 3

4 Answers4

2

I'm not sure what your final outcome wants to be, but you can use pd.date_range:

start_date = pd.to_datetime('01-01-2016')

for j in pd.date_range(start_date, periods=30):
    print(j)

Which gives you:

2016-01-01 00:00:00
2016-01-02 00:00:00
2016-01-03 00:00:00
2016-01-04 00:00:00
2016-01-05 00:00:00
2016-01-06 00:00:00
...
sacuL
  • 49,704
  • 8
  • 81
  • 106
  • This and some other answers below seem to be working for me if I try them in the interpreter, or as a python script. They don't work if I use the Jupyter notebook, which is what I'm hoping to use (though I can make do without); any idea why? I'm using Jupyter on Cocalc. – pandashelp Sep 11 '18 at 20:08
  • I don't use Jupyter, so I'm not sure, but what is the error that comes up? – sacuL Sep 11 '18 at 20:11
  • I get: "TypeError: periods must be a number, got 30" (I used your exact code line). Works fine in a script, doesn't work in Jupyter. – pandashelp Sep 11 '18 at 23:47
  • That is very strange... Based on the [source code](https://github.com/pandas-dev/pandas/blob/master/pandas/core/indexes/period.py), that error is supposed to show up if the argument is non-numeric, but I have no idea why you're getting it if you pass it `30`. It seems to be reading `30` as a string or something like that... Wish I could help more, but I'm stumped – sacuL Sep 12 '18 at 00:03
1

To increase the date by one day if your are pandas' lover:

df['dates'].iloc[0] +pd.to_timedelta(1,'d')

Or you can try this :

from datetime import timedelta
df['dates'].iloc[0] + timedelta(days=1)  #on the left side is the date you want to increase

You can find more infos here https://stackoverflow.com/a/6871054/8433650

0

By using numpy time delta either you can add day or subtract from time stamp

import numpy as np
import pandas as pd
pd.to_datetime('01-01-2016') +np.timedelta64(1,'D')
Naga kiran
  • 4,528
  • 1
  • 17
  • 31
0

You can add a day with timedelta()

Import datetime

for j in range (1,30):
    start_date + datetime.timedelta(days=1)

Here is a LINK to the docs for your reference

Dodge
  • 3,219
  • 3
  • 19
  • 38