How do I combine columns of my dataframe to create one datetime column which I can use as my index?

Viewed 1354

I am using Python Pandas for data analysis.

I have a dataframe taken from an excel file with 6 columns describing the timestamp (year, month, day, hour, minute, second). I want to create a pandas.datetime variable but when I do so using the pd.to_datetime() function the following happens:

My dataframe (df) :

jaar maand  dag uur minuten seconden
2005    7   1   0   0        0
2005    7   1   0   10       0
2005    7   1   0   20       0
2005    7   1   0   30       0
2005    7   1   0   40       0
2005    7   1   0   50       0

What I did:

df['timestamp'] = pd.to_datetime(df['jaar'] + df['maand'] + df['dag'] + df['uur'] + df['minuten'] + df['seconden'])

But then the items of my df.['timestamp'] series will look like this:

1970-01-01 00:00:00.20050701000000
1970-01-01 00:00:00.20050701001000
1970-01-01 00:00:00.20050701002000

What is the correct way to combine dates and why does this 1970-01-01 thing happen to my datetime? I can't set up my own time range manually because there are missing date points here and there.

What I also tried:

I can combine them to get the timestamp of one row, but I have so much data that I just can't use loops to do this.

date00 = pd.datetime(df.iloc[0, 0], df.iloc[0, 1], df.iloc[0, 2], df.iloc[0, 3], df.iloc[0, 4], df.iloc[0, 5])

This is my first time posting here. I hope the editing is okay.

2 Answers
Related