Created
May 9, 2020 20:31
-
-
Save DalyaG/19bd114a27a82b340b15a2c86eeabd3d to your computer and use it in GitHub Desktop.
Group DataFrame by weekday
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
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)