5

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
jpp
  • 159,742
  • 34
  • 281
  • 339
Kevin217
  • 724
  • 1
  • 10
  • 20

4 Answers4

4

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
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
  • Can you save the time column values in 12 hours format instead of a 24 hour format??? – sanster9292 Jan 03 '19 at 15:33
  • @sanster9292 See [this](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.strftime.html). – Ami Tavory Jan 03 '19 at 17:32
  • Ami, I am not great with this. I have data in a pandas columns that looks like this. 1999-01-01T00:00:29.75 I want to split and save it and Eastern Daylight Time. How would I handle the T in there? This is what I am doing so far, – sanster9292 Jan 03 '19 at 17:56
  • df3['Day']=pd.to_datetime(df['Date'], format='%Y-%m-%d %H:%M:%S.%f',errors='coerce').dt.tz_localize('UTC') – sanster9292 Jan 03 '19 at 18:02
  • @sanster9292 Could you please ask this as a question? Comments are not meant for this stuff. Plenty of people will help you out. – Ami Tavory Jan 03 '19 at 18:14
  • Thanks Ami. Let me post this as a question – sanster9292 Jan 03 '19 at 18:52
  • @sanster9292 If you'd like, place here a link to your question, and I'll be glad to have a look. – Ami Tavory Jan 03 '19 at 20:13
  • Ami, here is the link: https://stackoverflow.com/questions/54030619/how-to-get-hours-minute-seconds-from-iso-8601-date-time-format – sanster9292 Jan 03 '19 at 22:23
1

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])
Alexander
  • 105,104
  • 32
  • 201
  • 196
1

If type of column Dates is string, convert it by to_datetime. Then you can use dt.date, dt.time and last drop original column Dates:

print df['Dates'].dtypes
object
print type(df.at[0, 'Dates'])
<type 'str'>

df['Dates'] = pd.to_datetime(df['Dates'])

print df['Dates'].dtypes
datetime64[ns]

print df
                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

df['Date'] = df['Dates'].dt.date
df['Time'] = df['Dates'].dt.time

df = df.drop('Dates', axis=1)
print df
         Date      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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

attrgetter + pd.concat + join

You can use operator.attrgetter with pd.concat to add an arbitrary number of datetime attributes to your dataframe as separate series:

from operator import attrgetter

fields = ['date', 'time']
df = df.join(pd.concat(attrgetter(*fields)(df['Date'].dt), axis=1, keys=fields))

print(df)

                 Date        date      time
0 2015-05-13 23:53:00  2015-05-13  23:53:00
1 2015-01-13 15:23:00  2015-01-13  15:23:00
2 2016-01-13 03:33:00  2016-01-13  03:33:00
3 2018-02-13 20:13:25  2018-02-13  20:13:25
4 2017-05-12 06:52:00  2017-05-12  06:52:00
jpp
  • 159,742
  • 34
  • 281
  • 339