Dates arrive as text: "2024-03-07", sometimes "03/07/24", sometimes a typo. Until they are turned into real timestamps they are just strings, you cannot subtract them, group them by month, or tell that March comes before April only because of the spelling. Once they are timestamps, a whole vocabulary opens up: pull out the year or the weekday, count the days between two events, total sales per month, smooth a noisy series with a moving average.
This question builds those five time-series tools: parsing with bad values handled, calendar features, a monthly total that does not skip empty months, a moving average, and a day count between two columns of dates.
Implement parse_dates(s), calendar_features(dates), monthly_total(df, date_col, value_col), rolling_average(s, window) and days_between(start, end). The signatures and docstrings are already in the editor.
parse_dates(s) converts text in the exact format YYYY-MM-DD to a datetime Series with s's index. A value that is not a valid date in that format (a typo, "2024-02-30", a missing value) becomes NaT instead of raising.calendar_features(dates) takes a datetime Series with no missing values and returns a frame with dates's index and the integer columns year, month (1 to 12) and dayofweek (Monday = 0), plus the boolean column is_weekend (Saturday or Sunday), in that order.monthly_total(df, date_col, value_col) takes a frame whose date_col holds datetimes and returns a Series of the sum of value_col per calendar month, indexed by the first day of each month at midnight, covering every month from the earliest to the latest one including months with no rows (total 0). The Series is named value_col.rolling_average(s, window) returns the mean of each value and the window - 1 before it. The first window - 1 results are NaN (a window must be full). s is indexed by dates.days_between(start, end) takes two datetime Series with the same index (no missing values) and returns the whole number of days from start to end as an integer Series (negative if end is earlier).pd.to_datetime has format and errors arguments. One of the errors options turns failures into NaT.
The .dt accessor of a datetime Series has year, month, dayofweek and more.
resample("MS") groups a datetime-indexed Series into calendar months labelled by their first day, and creates the empty months too.
No results yet
Run your code to check it against the examples, or submit to run every test.