andrDev
andrDev

Reputation: 23

Splitting timestamp column into separate date and time columns using pandas

I have a pandas dataframe with over 2 million timestamps (below) that I would like to use to create a scatter plot of:

06/22/2006 09:54:11 PM

I'm having a hard time splitting this time stamp into 2 columns: date and time.

The following is the sample of contents of the dataframe.

{'STARFIRE_INCIDENT_ID': {0: 500127850130001.0, 1: 500133070120471.0, 2: 500103630140001.0, 3: 500175150150001.0, 4: 500171620150003.0}, 'INCIDENT_DATETIME': {0: '01/01/2005 12:00:01 AM', 1: '01/01/2005 12:00:03 AM', 2: '01/01/2005 12:01:02 AM', 3: '01/01/2005 12:01:42 AM', 4: '01/01/2005 12:01:45 AM'}, 'ALARM_BOX_BOROUGH': {0: 'RICHMOND / STATEN ISLAND', 1: 'BRONX', 2: 'BROOKLYN', 3: 'QUEENS', 4: 'QUEENS'}}

What I want to do is split INCIDENT_DATETIME to INCIDENT_DATE and INCIDENT_TIME.

I have tried df['time'] = df['INCIDENT_DATETIME'].dt.time but I get the following error.

AttributeError: Can only use .dt accessor with datetimelike values

Upvotes: 0

Views: 790

Answers (1)

user17242583
user17242583

Reputation:

You need to convert the INCIDENT_DATETIME column into a datetime column first, before you can access the individual datetime components:

df['INCIDENT_DATETIME'] = df['INCIDENT_DATETIME'].astype('datetime64[ns]')
df['INCIDENT_DATE'] = df['INCIDENT_DATETIME'].dt.date
df['INCIDENT_TIME'] = df['INCIDENT_DATETIME'].dt.time

Upvotes: 1

Related Questions