An orders table arrives with its dates stored as text such as "2024-01-15". You want real dates, a few parts of each date for grouping later, the shipping delay in days, and only the orders placed in a given period.
Write add_date_parts(df, start, end).
df has the columns order_id, order_date and ship_date. Both date columns hold text in the form YYYY-MM-DD, and nothing is missing. start and end are dates in the same text form, with start on or before end.
Return a new DataFrame with:
order_id, unchanged;order_date and ship_date, converted to pandas datetime columns;year: the year of order_date, as a whole number;month: the month of order_date, as a whole number from 1 to 12;weekday: the English name of the weekday of order_date, for example "Monday";days_to_ship: the whole number of days from order_date to ship_date.The columns come in the order listed. As the last step, keep only the rows whose order_date is from start to end, with both of those days included. The rows stay in their original order.
df. Its date columns must still hold text afterwards. Start from df.copy()..copy() on the filtered rows before you do. Assigning to a selection taken from another table can make pandas 2 warn that the change may not reach the original table.