Payne Payne - 1 year ago 142
Python Question

creating date range on csv using python-pandas

How to create a date range in python using pandas in Y-M-D?

Answer Source
import pandas as pd

df = pd.DataFrame([['2015-07-07','2016-09-22'],['2012-02-03','2013-02-19'],['2013-02-17','2013-03-22']],columns = ['start','end'])

#change strings to date format 
df['start'] = [pd.to_datetime(x) for x in df['start']]
df['end'] = [pd.to_datetime(x) for x in df['end']]

df['range'] = df['end']-df['start']


Output should be:

    start       end         range
0   2015-07-07  2016-09-22  443 days
1   2012-02-03  2013-02-19  382 days
2   2013-02-17  2013-03-22  33 days

In case you want to read from csv, switch the beginning to:

df = pd.read_csv('file_name.csv')

in case you want a concatenated column:

 df['details'] = [str(x)+' - '+str(y)+' has '+str(z)[:-9] for x,y,z in zip(df['start'],df['end'],df['range'])] 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download