Skip to content

Instantly share code, notes, and snippets.

@DalyaG
Created May 9, 2020 20:31
Show Gist options
  • Save DalyaG/19bd114a27a82b340b15a2c86eeabd3d to your computer and use it in GitHub Desktop.
Save DalyaG/19bd114a27a82b340b15a2c86eeabd3d to your computer and use it in GitHub Desktop.
Group DataFrame by weekday
df['weekday_num'] = pd.DatetimeIndex(df['date_of_birth']).weekday
df['weekday_name'] = pd.DatetimeIndex(df['date_of_birth']).weekday_name
df_grouped = (df.groupby(['weekday_num', 'weekday_name'])
.size()
.reset_index(name="n_pets"))
df_grouped
@jaslatendresse
Copy link

I am getting 'AttributeError: module 'pandas' has no attribute 'DateTimeIndex'' at line 1. After some googling, I could not really find the cause for it as DateTimeIndex doesn't seem to be deprecated. Any suggestions? (Yes, pandas is imported correctly)

@DalyaG
Copy link
Author

DalyaG commented Apr 29, 2021

mmm maybe we are using different versions?
This is a part of a blog post - https://codeburst.io/plotting-with-pandas-dates-and-bar-plots-227f25351274
Not sure what my version was when I wrote this, but according to this it should be working: https://pandas.pydata.org/docs/reference/api/pandas.DatetimeIndex.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment