0

how do i split the 'datetime' column into 'date' column and 'time' column? I would like to have the 'date' as column 1 and 'time' as column 2. The rest of the columns will have the same header.

 datetime                  C     H     L     O  OI  V    WAP  
0  2017-03-13 19:00:00  8.22  8.22  8.10  8.10   4  6  8.143   
1  2017-03-13 19:01:00  8.22  8.22  8.22  8.22   0  0  8.220   
2  2017-03-13 19:02:00  8.22  8.22  8.22  8.22   0  0  8.220   
3  2017-03-13 19:03:00  8.22  8.22  8.22  8.22   0  0  8.220   
4  2017-03-13 19:04:00  8.22  8.22  8.22  8.22   0  0  8.220   


print(df.head())
new_date_and_time = lambda x : x.split()
df['new_date_and_time'] = df['datetime'].apply(new_date_and_time)
print(df.head())
Serenity
  • 35,289
  • 20
  • 120
  • 115
Ross Demtschyna
  • 333
  • 5
  • 18
  • Possible duplicate of [Splitting timestamp column into seperate date and time columns](http://stackoverflow.com/questions/35595710/splitting-timestamp-column-into-seperate-date-and-time-columns) – chinskiy Apr 02 '17 at 11:30

1 Answers1

0

Assuming the column is already a "datetime" datatype you could just call the dataframe method "dt" on the original column and reassign to new columns:

df['date'] = df['datetime'].dt.date
df['time'] = df['datetime'].dt.time
# optional, drop original column
df.drop('datetime', axis=1, inplace=True)

Obviously dropping the date time column is optional.

Andrew L
  • 6,618
  • 3
  • 26
  • 30