4

I have data look like this

        Date        Time     Open     High      Low    Close  Volume
0      2013.07.09   7:00  101.056  101.151  101.016  101.130    1822
1      2013.07.09   8:00  101.130  101.257  101.128  101.226    2286
2      2013.07.09   9:00  101.226  101.299  101.175  101.180    2685
3      2013.07.09  10:00  101.178  101.188  101.019  101.154    2980
4      2013.07.09  11:00  101.153  101.239  101.146  101.188    2623

How to combine Date column and Time column into one column which is Date Time. And I wonder if i do that whether I have to change the string to date. Thank in advance.

Tanakorn Taweepoka
  • 197
  • 2
  • 3
  • 14

2 Answers2

16

If you have 'Date' column as timestamp then you convert them to a string and add them , then convert them into timestamp i.e

df['Datetime'] = pd.to_datetime(df['Date'].apply(str)+' '+df['Time'])

Output :

        Date   Time     Open     High      Low    Close  Volume  \
0 2013-07-09   7:00  101.056  101.151  101.016  101.130    1822   
1 2013-07-09   8:00  101.130  101.257  101.128  101.226    2286   
2 2013-07-09   9:00  101.226  101.299  101.175  101.180    2685   
3 2013-07-09  10:00  101.178  101.188  101.019  101.154    2980   
4 2013-07-09  11:00  101.153  101.239  101.146  101.188    2623   

             Datetime  
0 2013-07-09 07:00:00  
1 2013-07-09 08:00:00  
2 2013-07-09 09:00:00  
3 2013-07-09 10:00:00  
4 2013-07-09 11:00:00  
Bharath M Shetty
  • 30,075
  • 6
  • 57
  • 108
1
dataframe["Date Time"] = dataframe["Date"].map(str) + dataframe["Time"]

Update

Formatting the date you can use

dataframe["Date Time"] =pd.to_datetime(dataframe["Date"].map(str) +'-'+ dataframe["Time"])

and instead of replace you can delete those Date and Time column from the dataframe. like this

del dataframe["Date"], dataframe["Time"]
R.A.Munna
  • 1,699
  • 1
  • 15
  • 29
  • Thank R.A.Munna but I think that this is not the date format isn't it. How to change to be in datetime format. And I want this new column will be replace Date and Time column in my dataframe – Tanakorn Taweepoka Aug 07 '17 at 03:40