Flying pig
Flying pig

Reputation: 612

How to merge date and time in Pandas?

I am a beginner of Pandas, so my question is very basic. I have a dataset, which has seperate columns for date and time as following

    Date        Time    Ooen    High    Low     Close   Vol
0   2000.12.22  12:00   0.91810 0.92620 0.91650 0.92320 2244
1   2000.12.22  16:00   0.92260 0.92520 0.92220 0.92310 1688
2   2000.12.22  20:00   0.92300 0.92580 0.92260 0.92420 955
3   2000.12.23  00:01   0.92410 0.92450 0.92270 0.92320 168
4   2000.12.25  00:00   0.92300 0.92460 0.92300 0.92420 260

The type of Date and Time are

type(df['Date'])
pandas.core.series.Series

type(df['Time'])
pandas.core.series.Series

I don't know how to merge them into a timestamp like

'2015-07-04 00:00:00'

Could anyone please help me?

Upvotes: 0

Views: 113

Answers (2)

Sajan
Sajan

Reputation: 1267

You could try something like this -

df['Datetime'] = pd.to_datetime(df['Date'] + ' ' + df['Time'], format='%Y.%m.%d %H:%M') 
df
     Date       Time   Open    High    Low    Close    Vol    Datetime
0  2000.12.22  12:00  0.9181  0.9262  0.9165  0.9232  2244 2000-12-22 12:00:00
1  2000.12.22  16:00  0.9226  0.9252  0.9222  0.9231  1688 2000-12-22 16:00:00
2  2000.12.22  20:00  0.9230  0.9258  0.9226  0.9242   955 2000-12-22 20:00:00
3  2000.12.23  00:01  0.9241  0.9245  0.9227  0.9232   168 2000-12-23 00:01:00
4  2000.12.25  00:00  0.9230  0.9246  0.9230  0.9242   260 2000-12-25 00:00:00

Upvotes: 3

Dhiraj
Dhiraj

Reputation: 9

try given below code.

df['datetime'] = df[df.columns[1:]].apply(lambda x: ','.join(x.dropna().astype(str)),axis=1)

Upvotes: -1

Related Questions