Martin Bachtold
Martin Bachtold

Reputation: 49

Parse DateTime from Object with Pandas

I would like to parse the year column to datetime.

    name    id  nametype    recclass    mass (g)    fall    year                    
0   Aachen  1   Valid       L5         21.0         Fell    01/01/1880 12:00:00 AM

... reclat      reclong     GeoLocation
... 50.77500    6.08333    (50.775000, 6.083330)

df['year'].apply(dateutil.parser.parse) and that parses as 1880-01-01 00:00:00 but i can use that for selecting dates. Do anyone have a tip for me?

Upvotes: 0

Views: 357

Answers (2)

akilat90
akilat90

Reputation: 5696

Since you have converted the year column to datetime, you can use:

df['year'] = df['year'].dt.date
# year    
# 1880-01-01

However, for datetime casting, note that pandas has an inbuilt datetime parser that is easy to use than dateutil IMO.

Upvotes: 0

jezrael
jezrael

Reputation: 862441

I think need to_datetime:

df = pd.DataFrame({'year':['01/01/1880 12:00:00 AM']})

df['year'] = pd.to_datetime(df['year'])
print (df)
        year
0 1880-01-01

Upvotes: 2

Related Questions