I am using pandas dataframe. there is a specific column has time information.
the raw data likes this:
5:15am
5:28am
6:15am
so I need to convert the raw data into datetime format:
format = '%I:%M%p'
dataset['TimeStamp'] = pd.to_datetime(dataset['TimeStamp'],format)
However, I got:
2014-07-04 05:15:00
2014-07-04 05:28:00
2014-07-04 06:15:00
I don't want the year and date information, just want time. How can I remove it. Thanks.
The following will convert what you have to datetime.time() objects:
Output
dataset['TimeStamp']=dataset['TimeStamp'].str.slice(11,18)
Since version
0.17.0
you can just doFor versions older than
0.17.0
:You can just call
apply
and access thetime
function on the datetime object create the column initially like this without the need for post processing:Just use the
datetime.time()
functionThis will return a
datetime.time
object and you can access the data with thetime.hour
time.minute
andtime.second
attributes.your_date_df.dt.time
Lets say that your column with the date ans time is
df['arrived_date']
:Whith pandas, you just need to do:
The new column
df['arrived_time']
will look like this:Observe that the new column,
df['arrived_time']
, is no longer adatetime64
type, the type of the column is just a pandasobject
There's a simpler way to do it using pandas, although most, if not all solutions are correct