Okroshiashvili
Okroshiashvili

Reputation: 4149

Split date in pandas

In excel file I have columns "date" which contains dates in the following format: 11/17/2017 2:40:40 PM this translates to month/day/year H:M:S

When importing this file in Pandas its dtype is <M8[ns]

So, I want to split the values. Desired output have to be 11/17/2017 and it has to be in the same format as it would be if we do this split in excel file. I mean, I need the values to be like 11/17/2017 but format this date to show only day and month like this:17-Nov

I tried several methods but didn't work. Can anyone help me?

Upvotes: 1

Views: 3868

Answers (1)

jezrael
jezrael

Reputation: 862691

It seems you need strftime because if need 17-Nov as datetime it is not possible in python:

df = pd.DataFrame({'date':pd.to_datetime(['11/17/2017 2:40:40 PM','11/17/2017 2:40:40 PM'])})
print (df)
                 date
0 2017-11-17 14:40:40
1 2017-11-17 14:40:40

print (df['date'].dt.strftime('%d/%m/%Y'))
0    17/11/2017
1    17/11/2017
Name: date, dtype: object

print (df['date'].dt.strftime('%d-%b'))
0    17-Nov
1    17-Nov
Name: date, dtype: object

Upvotes: 2

Related Questions