'Anyway to group or sort dates by month and/or year using python?

Looking at a fruit and veg dataset with prices and dates. However when I try to plot anything with the date there are way too many instances as the date feature does it by each week. Is there anyway to either group the dates by month or something? The date format is like 2022-02-11.



Solution 1:[1]

A simple way is to add a month column and group by it. We use pandas.DataFrame.groupby and pandas.DatetimeIndex to do this.

df['Month'] = pd.DatetimeIndex(df.date).month
df.groupby(['Month']).sum().plot()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohammad Ayoub