Num day to Name day with Pandas
Asked Answered
C

4

15

If I use this funtion pd.DatetimeIndex(dfTrain['datetime']).weekday I get number of the day, but I don't find any function which give the name of de day... So I need to convert 0 to Monday, 1 to Tuestday and so on.

Here is an example of my dataframe:

            datetime    season holiday workingday weather   temp    atemp   humidity    windspeed   count
    0   2011-01-01 00:00:00 1   0   0   1   9.84    14.395  81  0.0000  16
    1   2011-01-01 01:00:00 1   0   0   1   9.02    13.635  80  0.0000  40
    2   2011-01-01 02:00:00 1   0   0   1   9.02    13.635  80  0.0000  32
    3   2011-01-01 03:00:00 1   0   0   1   9.84    14.395  75  0.0000  13
    4   2011-01-01 04:00:00 1   0   0   1   9.84    14.395  75  0.0000  1
    5   2011-01-01 05:00:00 1   0   0   2   9.84    12.880  75  6.0032  1
    6   2011-01-01 06:00:00 1   0   0   1   9.02    13.635  80  0.0000  2
    7   2011-01-01 07:00:00 1   0   0   1   8.20    12.880  86  0.0000  3
    8   2011-01-01 08:00:00 1   0   0   1   9.84    14.395  75  0.0000  8
    9   2011-01-01 09:00:00 1   0   0   1   13.12   17.425  76  0.0000  14

Another question more, which is the difference between pandas.DatetimeIndex.dayofweekand pandas.DatetimeIndex.weekday?

Cabal answered 17/3, 2015 at 10:11 Comment(2)
so why not use a 7-tuple with the names of the day?Muggy
You can get an array of localised day names from calendar.day_nameMcintosh
M
18

One method, so long as datetime is already a datetime column is to apply datetime.strftime to get the string for the weekday:

In [105]:

df['weekday'] = df[['datetime']].apply(lambda x: dt.datetime.strftime(x['datetime'], '%A'), axis=1)
df
Out[105]:
             datetime  season  holiday  workingday  weather   temp   atemp  \
0 2011-01-01 00:00:00       1        0           0        1   9.84  14.395   
1 2011-01-01 01:00:00       1        0           0        1   9.02  13.635   
2 2011-01-01 02:00:00       1        0           0        1   9.02  13.635   
3 2011-01-01 03:00:00       1        0           0        1   9.84  14.395   
4 2011-01-01 04:00:00       1        0           0        1   9.84  14.395   
5 2011-01-01 05:00:00       1        0           0        2   9.84  12.880   
6 2011-01-01 06:00:00       1        0           0        1   9.02  13.635   
7 2011-01-01 07:00:00       1        0           0        1   8.20  12.880   
8 2011-01-01 08:00:00       1        0           0        1   9.84  14.395   
9 2011-01-01 09:00:00       1        0           0        1  13.12  17.425   

   humidity  windspeed  count   weekday  
0        81     0.0000     16  Saturday  
1        80     0.0000     40  Saturday  
2        80     0.0000     32  Saturday  
3        75     0.0000     13  Saturday  
4        75     0.0000      1  Saturday  
5        75     6.0032      1  Saturday  
6        80     0.0000      2  Saturday  
7        86     0.0000      3  Saturday  
8        75     0.0000      8  Saturday  
9        76     0.0000     14  Saturday  

As to your other question, there is no difference between dayofweek and weekday.

It will be quicker to define a map of the weekday to String equivalent and call map on the weekday:

dayOfWeek={0:'Monday', 1:'Tuesday', 2:'Wednesday', 3:'Thursday', 4:'Friday', 5:'Saturday', 6:'Sunday'}
df['weekday'] = df['datetime'].dt.dayofweek.map(dayOfWeek)

For version prior to 0.15.0 the following should work:

import datetime as dt
df['weekday'] = df['datetime'].apply(lambda x: dt.datetime.strftime(x, '%A'))

Version 0.18.1 and newer

There is now a new convenience method dt.weekday_name to do the above

Version 0.23.0 and newer

weekday_name is now depricated in favour of dt.day_name.

Mayan answered 17/3, 2015 at 10:32 Comment(0)
W
24

Using dt.weekday_name is deprecated since pandas 0.23.0, instead, use dt.day_name():

df.datetime.dt.day_name()

0    Saturday
1    Saturday
2    Saturday
3    Saturday
4    Saturday
5    Saturday
6    Saturday
7    Saturday
8    Saturday
9    Saturday
Name: datetime, dtype: object
Wanyen answered 10/8, 2018 at 23:19 Comment(0)
L
21

Last versions you can use dt.day_name:

df['weekday'] = df['datetime'].dt.day_name
print df
             datetime  season  holiday  workingday  weather   temp   atemp  \
0 2011-01-01 00:00:00       1        0           0        1   9.84  14.395   
1 2011-01-01 01:00:00       1        0           0        1   9.02  13.635   
2 2011-01-01 02:00:00       1        0           0        1   9.02  13.635   
3 2011-01-01 03:00:00       1        0           0        1   9.84  14.395   
4 2011-01-01 04:00:00       1        0           0        1   9.84  14.395   
5 2011-01-01 05:00:00       1        0           0        2   9.84  12.880   
6 2011-01-01 06:00:00       1        0           0        1   9.02  13.635   
7 2011-01-01 07:00:00       1        0           0        1   8.20  12.880   
8 2011-01-01 08:00:00       1        0           0        1   9.84  14.395   
9 2011-01-01 09:00:00       1        0           0        1  13.12  17.425   

   humidity  windspeed  count   weekday  
0        81     0.0000     16  Saturday  
1        80     0.0000     40  Saturday  
2        80     0.0000     32  Saturday  
3        75     0.0000     13  Saturday  
4        75     0.0000      1  Saturday  
5        75     6.0032      1  Saturday  
6        80     0.0000      2  Saturday  
7        86     0.0000      3  Saturday  
8        75     0.0000      8  Saturday  
9        76     0.0000     14  Saturday  
Loathe answered 10/5, 2016 at 8:14 Comment(2)
Link is broken (404).Inkerman
Series.dt.day_name is implemented as a method, not an attribute. As such you need to call it with () df['weekday'] = df['datetime'].dt.day_name()Luisaluise
M
18

One method, so long as datetime is already a datetime column is to apply datetime.strftime to get the string for the weekday:

In [105]:

df['weekday'] = df[['datetime']].apply(lambda x: dt.datetime.strftime(x['datetime'], '%A'), axis=1)
df
Out[105]:
             datetime  season  holiday  workingday  weather   temp   atemp  \
0 2011-01-01 00:00:00       1        0           0        1   9.84  14.395   
1 2011-01-01 01:00:00       1        0           0        1   9.02  13.635   
2 2011-01-01 02:00:00       1        0           0        1   9.02  13.635   
3 2011-01-01 03:00:00       1        0           0        1   9.84  14.395   
4 2011-01-01 04:00:00       1        0           0        1   9.84  14.395   
5 2011-01-01 05:00:00       1        0           0        2   9.84  12.880   
6 2011-01-01 06:00:00       1        0           0        1   9.02  13.635   
7 2011-01-01 07:00:00       1        0           0        1   8.20  12.880   
8 2011-01-01 08:00:00       1        0           0        1   9.84  14.395   
9 2011-01-01 09:00:00       1        0           0        1  13.12  17.425   

   humidity  windspeed  count   weekday  
0        81     0.0000     16  Saturday  
1        80     0.0000     40  Saturday  
2        80     0.0000     32  Saturday  
3        75     0.0000     13  Saturday  
4        75     0.0000      1  Saturday  
5        75     6.0032      1  Saturday  
6        80     0.0000      2  Saturday  
7        86     0.0000      3  Saturday  
8        75     0.0000      8  Saturday  
9        76     0.0000     14  Saturday  

As to your other question, there is no difference between dayofweek and weekday.

It will be quicker to define a map of the weekday to String equivalent and call map on the weekday:

dayOfWeek={0:'Monday', 1:'Tuesday', 2:'Wednesday', 3:'Thursday', 4:'Friday', 5:'Saturday', 6:'Sunday'}
df['weekday'] = df['datetime'].dt.dayofweek.map(dayOfWeek)

For version prior to 0.15.0 the following should work:

import datetime as dt
df['weekday'] = df['datetime'].apply(lambda x: dt.datetime.strftime(x, '%A'))

Version 0.18.1 and newer

There is now a new convenience method dt.weekday_name to do the above

Version 0.23.0 and newer

weekday_name is now depricated in favour of dt.day_name.

Mayan answered 17/3, 2015 at 10:32 Comment(0)
S
0

Adding to the previous correct answer from @jezrael, you can use this:

import calendar
df['weekday'] = pd.Series(pd.Categorical(df['datetime'].dt.weekday_name, categories=list(calendar.day_name)))

which also provides your new categorical variable with order (in this example: 'Monday', ..., 'Sunday') according to this. This will possibly be helpful on next steps of your analysis.

Sobriquet answered 4/1, 2019 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.