Rahul rajan
Rahul rajan

Reputation: 1266

How to use weekday_name function for day/month/year format of datetime

I am trying to use Weekday function inside pandas to classify the actual dates with name of the day of the week but its not working. The format of my dates is day/month/year.

**My code**

data['date/time'] = pd.to_datetime(data['Actual Pickup date/time'])
data['Day_of_Week_AP'] =data['date/time'].dt.weekday_name


Input data

Actual Pickup date/time
8/5/2018 16:39
5/6/2018 17:17
5/6/2018 17:17
5/6/2018 16:53
5/6/2018 16:53
5/6/2018 16:53
5/6/2018 16:53
5/6/2018 16:53

I am getting the below output which wrong .'8/5/2018' is tuesday and ' 5/6/2018 ' is also tuesday

Current Output 

Actual Pickup date/time Day_of_Week_AP
8/5/2018 16:39  Sunday
5/6/2018 17:17  Sunday
5/6/2018 17:17  Sunday
5/6/2018 16:53  Sunday
5/6/2018 16:53  Sunday
5/6/2018 16:53  Sunday
5/6/2018 16:53  Sunday
5/6/2018 16:53  Sunday
5/6/2018 16:53  Sunday

How to fix this

Upvotes: 0

Views: 716

Answers (1)

BENY
BENY

Reputation: 323266

Adding the dayfirst

df['date/time'] = pd.to_datetime(df['Actual Pickup date/time'],dayfirst=True)
df['date/time'].dt.weekday_name
Out[814]: 
0    Tuesday
1    Tuesday
2    Tuesday
3    Tuesday
4    Tuesday
5    Tuesday
6    Tuesday
7    Tuesday
Name: date/time, dtype: object

Upvotes: 3

Related Questions