Examples of how to create a datetime column from year, month and day columns in pandas:
Create a dataframe
Let's first create a dataframe in pandas with 3 columns "Year", "Month" and "Day":
import pandas as pdimport randomimport itertoolsyear_list = []month_list = []day_list = []for combination in itertools.product([2015], [1,2,3,4], [5,15,25]):#print(combination)year_list.append( combination[0] )month_list.append( combination[1] )day_list.append( combination[2] )data = {'Year':year_list,'Month':month_list,'Day':day_list,'Random Integer':[random.randrange(1, 10) for i in range(len(year_list))]}df = pd.DataFrame(data=data)print(df)
returns for example
Year Month Day Random Integer0 2015 1 5 71 2015 1 15 52 2015 1 25 63 2015 2 5 44 2015 2 15 15 2015 2 25 26 2015 3 5 47 2015 3 15 98 2015 3 25 39 2015 4 5 110 2015 4 15 311 2015 4 25 7
Create a datetime column
To create a new datetime column using 'Year', 'Month' and 'Day' columns, a solution is to use to_datetime():
df['Datetime'] = pd.to_datetime( df[['Year', 'Month', 'Day']])
returns
Year Month Day Random Integer Datetime0 2015 1 5 7 2015-01-051 2015 1 15 5 2015-01-152 2015 1 25 6 2015-01-253 2015 2 5 4 2015-02-054 2015 2 15 1 2015-02-155 2015 2 25 2 2015-02-256 2015 3 5 4 2015-03-057 2015 3 15 9 2015-03-158 2015 3 25 3 2015-03-259 2015 4 5 1 2015-04-0510 2015 4 15 3 2015-04-1511 2015 4 25 7 2015-04-25
Set datetime column as the dataframe index
Now, if you want to set the datetime column as the dataframe index,a solution is to use set_index():
df.set_index('Datetime', inplace=True)
returns
Year Month Day Random IntegerDatetime2015-01-05 2015 1 5 72015-01-15 2015 1 15 52015-01-25 2015 1 25 62015-02-05 2015 2 5 42015-02-15 2015 2 15 12015-02-25 2015 2 25 22015-03-05 2015 3 5 42015-03-15 2015 3 15 92015-03-25 2015 3 25 32015-04-05 2015 4 5 12015-04-15 2015 4 15 32015-04-25 2015 4 25 7
Can be used to plot a time series:

Note to reset the index:
df.reset_index(inplace=True)
