Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I am trying to extract the year,month,date,day,minutes from column
I have 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
any help would be appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading