Super learner
This user is a Super Learner. To become a Super Learner, you need to reach Level 8.
Last answered:

26 Nov 2023

Posted on:

03 Oct 2023

0

Resolved: weekday function vs dt.weekday

is there any difference between:
df_reason_mod['Day of the Week'] = df_reason_mod['Date'].apply(date_to_weekday)
vs
df_reason_mod['Day of the Week'] = df_reason_mod['Date'].dt.weekday
?

3 answers ( 1 marked as helpful)
Instructor
Posted on:

10 Nov 2023

0

Hi David!

Thanks for reaching out.

Please accept my apologies for the delayed response.

Yes. The difference is that with .apply() you are applying a pre-defined function (... from a Python module or a function you had created earlier in your script) to a certain subset of your DataFrame. Instead, with .dt.weekday you are only accessing a certain part of the data. More precisely, you are using the .dt accessor to access/retrieve the datetimelike values of the DataFrame/Series values.

Hope this helps.
Kind regards,
Martin

Super learner
This user is a Super Learner. To become a Super Learner, you need to reach Level 8.
Posted on:

11 Nov 2023

0

Thanks for the answer!

additional question tho, why bother to write the date_to_weekday function if we can just get what we want through dt.accessor?

Is there any hidden difference or just want to demonstrate that we can process the data with a function?

Instructor
Posted on:

26 Nov 2023

0

Hi David!

Thank you very much for your reply.

the .dt accessor only allows you to access the date values from the table column in question. Then, there are various functions, methods and operations you may want to do with that data (once you have accessed it).

Hope this helps.
Kind regards,
Martin


Submit an answer