Answers for "split date in python"

-1

split datetime to date and time pandas

df = pd.DataFrame({'my_timestamp': pd.date_range('2016-1-1 15:00', periods=5)})

>>> df
         my_timestamp
0 2016-01-01 15:00:00
1 2016-01-02 15:00:00
2 2016-01-03 15:00:00
3 2016-01-04 15:00:00
4 2016-01-05 15:00:00

df['new_date'] = [d.date() for d in df['my_timestamp']]
df['new_time'] = [d.time() for d in df['my_timestamp']]

>>> df
         my_timestamp    new_date  new_time
0 2016-01-01 15:00:00  2016-01-01  15:00:00
1 2016-01-02 15:00:00  2016-01-02  15:00:00
2 2016-01-03 15:00:00  2016-01-03  15:00:00
3 2016-01-04 15:00:00  2016-01-04  15:00:00
4 2016-01-05 15:00:00  2016-01-05  15:00:00
Posted by: Guest on June-05-2020
-2

how to separate date and time in python

import pandas as pd
df = pd.DataFrame({'datetime':pd.date_range('2020-01-01 07:10',periods=6)})
print("DataFrame is:n", df)
df['date'] = pd.to_datetime(df['datetime']).dt.date
df['time'] = pd.to_datetime(df['datetime']).dt.time
print("Date-time-hour-minutes :n", df)
Posted by: Guest on August-24-2021

Python Answers by Framework

Browse Popular Code Answers by Language