dt.weekday works but dt.isoweekday doesn't work

Viewed 105

I am trying to extract the year, month, date, day, minutes from column.

I have the following DataFrame:

2021-01-01 00:15:00
2021-01-02 00:30:00
2021-02-05 02:15:00
2021-02-20 00:15:00
2021-04-25 22:15:00
2021-12-30 00:10:00

I am trying:

df['date'] = pd.to_datetime(df['date'])
df['year'], df['month'], df['day'], df['minute'], df['hour'],df['weekday']=df['date'].dt.year, df['date'].dt.month, df['date'].dt.day, df['date'].dt.minute, df['date'].dt.hour,  df['date'].dt.isoweekday
df

Which works fine for all other attribute but dt.isoweekday is throwing error

AttributeError: 'DatetimeProperties' object has no attribute 'isoweekday'

If I replace it with dt.weekday it works fine, but I need monday=1. I have also tried dt.Timestamp.isoweekday, can any one please guide why isoweekday function is not working?

I am using pandas 1.3.4

1 Answers

To get the week day (1 being Monday), you need to use df['date'].dt.isocalendar().day:

0    5
1    6
2    5
3    6
4    7
5    4
Name: day, dtype: UInt32

Similarly, df['date'].dt.isocalendar().week would give you the ISO week number.

Note what with df['date'].dt.isocalendar() you directly obtain year/week/day:

   year  week  day
0  2020    53    5
1  2020    53    6
2  2021     5    5
3  2021     7    6
4  2021    16    7
5  2021    52    4
Related