xcen
xcen

Reputation: 692

Unable convert the data time of this column (object to time)

I have a data frame data types like below

    usr_id  year
0   t961    00:50:03.158000
1   t964    03:25:57
2   t335    00:55:00
3   t829    00:04:25.714000


usr_id    object
year      object
dtype: object

I want to convert the year column data type to a datetime. I used the below code.

timefmt = "%H:%M"
test['year'] = pd.to_datetime(
    test['year'], format=timefmt, errors='coerce').dt.time

I get below output

    usr_id  year
0   t961    NaT
1   t964    NaT
2   t335    NaT
3   t829    NaT

How can I convert the data time of this column (object to datetime)? How can I drop seconds & microseconds?

Expected output

    usr_id   year
0   t961    00:50
1   t964    03:25
2   t335    00:55
3   t829    00:04

Upvotes: 2

Views: 66

Answers (2)

jezrael
jezrael

Reputation: 863341

Use to_datetime with Series.dt.strftime:

timefmt = "%H:%M"
test['year'] = pd.to_datetime(test['year'], errors='coerce').dt.strftime(timefmt)

print (test)
  usr_id   year
0   t961  00:50
1   t964  03:25
2   t335  00:55
3   t829  00:04

Or you can use Series.str.rsplit with n=1 for split by last : and select first lists by indexing:

test['year'] = test['year'].str.rsplit(':', n=1).str[0]
print (test)
  usr_id   year
0   t961  00:50
1   t964  03:25
2   t335  00:55
3   t829  00:04

Or solution by @Akira:

test['year'] = test['year'].astype(str).str[:5] 

Upvotes: 3

Philip
Philip

Reputation: 974

As there is currently no actual date in your year column, you need to set a default one. Then you you can pass a format to pandas to_datetime function.

This could be done in a one-liner like this:

test['year'] = pd.to_datetime(test['year'].apply(lambda x: '1900-01-01 '+ x),format='%Y-%m-%d %H:%M:%S')

Upvotes: 0

Related Questions