'Converting datetime only to time in pandas

I would like to ask a question regarding converting datetime only to time. I have values 'Date Created" that include Dates and Times in one column and I would like to create two columns: one with only Date and one with Time. I wrote a code but in Time column I got a default date of 1900-01-01 along with time.

I wonder now how I could create a column only with Time in format: Hours:Minutes:Seconds

I will be grateful for any tips.

    Date Created      Date         Time
2016-02-20 09:26:45 2016-02-20  1900-01-01 09:26:45
2016-02-19 19:30:25 2016-02-19  1900-01-01 19:30:25
2016-02-19 18:13:39 2016-02-19  1900-01-01 18:13:39
2016-03-01 14:15:36 2016-03-01  1900-01-01 14:15:36
2016-03-04 14:47:57 2016-03-04  1900-01-01 14:47:57

I wrote a code like the one below to extract dat

Data.loc[:,'Date Created'] = pd.to_datetime(Data.loc[:,'Date Created'], format="%Y-%m-%d %H:%M:%S")
Data.loc[:,'Date'] = pd.to_datetime(Data.loc[:,'Date'], format="%Y-%m-%d")
Data.loc[:,'Time'] = pd.to_datetime(Data.loc[:,'Time'], format="%H:%M:%S")


Solution 1:[1]

How about that?

>>> df['TimeOnly']=df['Date Created'].dt.strftime('%H:%M:%S')

>>> df
         Date Created  TimeOnly
0 2016-02-20 09:26:45  09:26:45
1 2016-02-19 19:30:25  19:30:25
2 2016-02-19 18:13:39  18:13:39
3 2016-03-01 14:15:36  14:15:36
4 2016-03-04 14:47:57  14:47:57

Solution 2:[2]

After you convert Date Created to pd.datetime, you can use it to get the other two:

>>> Data.loc[:,'Date Created'] = pd.to_datetime(Data.loc[:,'Date Created'], format="%Y-%m-%d %H:%M:%S")
>>> Data['Date'] = Data['Date Created'].dt.date
>>> Data['Time'] = Data['Date Created'].dt.time
>>> Data
         Date Created        Date      Time
0 2016-02-20 09:26:45  2016-02-20  09:26:45
1 2016-02-19 19:30:25  2016-02-19  19:30:25
2 2016-02-19 18:13:39  2016-02-19  18:13:39
3 2016-03-01 14:15:36  2016-03-01  14:15:36
4 2016-03-04 14:47:57  2016-03-04  14:47:57

Then you get:

>>> Data['Time'][0]
datetime.time(9, 26, 45)
>>> Data['Date'][0]
datetime.date(2016, 2, 20)

Solution 3:[3]

pandas has no separate datatypes for date and time. if you only want your columns to show date or time resp., you could format to string (strftime). Ex:

import pandas as pd

Data = pd.DataFrame({'Date Created': ["2016-02-20 09:26:45", "2016-02-19 19:30:25", "2016-02-19 18:13:39"]})

Data['Date Created'] = pd.to_datetime(Data['Date Created'])
Data['Date'] = Data['Date Created'].dt.strftime("%Y-%m-%d")
Data['Time'] = Data['Date Created'].dt.strftime("%H:%M:%S")

Data
         Date Created        Date      Time
0 2016-02-20 09:26:45  2016-02-20  09:26:45
1 2016-02-19 19:30:25  2016-02-19  19:30:25
2 2016-02-19 18:13:39  2016-02-19  18:13:39

Python documentation / strftime

Solution 4:[4]

You could do something like that:

from datetime import datetime

now = datetime.now() # current date and time

hour = now.strftime("%H")# current hour 

But customise as you wish, you check the documentation

Solution 5:[5]

By using dt.strftime('%H:%M:%S') the format is no longer datetime64[ns] and became an "object", which make one unable to utilize datetime functions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Danail Petrov
Solution 2
Solution 3
Solution 4 Gaƫtan GR
Solution 5 RiveN