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)
vsdf_reason_mod['Day of the Week'] = df_reason_mod['Date'].dt.weekday
?
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
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?
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