I have a data frame called data
, which has a column Dates
like this,
Dates
0 2015-05-13 23:53:00
1 2015-05-13 23:53:00
2 2015-05-13 23:33:00
3 2015-05-13 23:30:00
4 2015-05-13 23:30:00
I know how to add a column to data frame, but how to divide Dates
to
Day Time
0 2015-05-13 23:53:00
1 2015-05-13 23:53:00
2 2015-05-13 23:33:00
3 2015-05-13 23:30:00
4 2015-05-13 23:30:00
If your series is s
, then this will create such a DataFrame:
pd.DataFrame({
'date': pd.to_datetime(s).dt.date,
'time': pd.to_datetime(s).dt.time})
as once you convert the series using pd.to_datetime
, then the dt
member can be used to extract the parts.
Example
import pandas as pd
s = pd.Series(['2015-05-13 23:53:00', '2015-05-13 23:53:00'])
>>> pd.DataFrame({
'date': pd.to_datetime(s).dt.date,
'time': pd.to_datetime(s).dt.time})
date time
0 2015-05-13 23:53:00
1 2015-05-13 23:53:00
If your Dates
column is a string:
data['Day'], data['Time'] = zip(*data.Dates.str.split())
>>> data
Dates Day Time
0 2015-05-13 23:53:00 2015-05-13 23:53:00
1 2015-05-13 23:53:00 2015-05-13 23:53:00
2 2015-05-13 23:33:00 2015-05-13 23:33:00
3 2015-05-13 23:33:00 2015-05-13 23:33:00
4 2015-05-13 23:33:00 2015-05-13 23:33:00
If it is a timestamp:
data['Day'], data['Time'] = zip(*[(d.date(), d.time()) for d in data.Dates])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With