Grouping with anonymous functions with a DatetimeIndex
Using DataFrames with a DatetimeIndex opens the door to many new and different operations as seen with several recipes in this chapter.
In this recipe, we will show the versatility of using the .groupby method for DataFrames that have a DatetimeIndex.
How to do it…
- Read in the Denver crime hdf5 file, place the
REPORTED_DATEcolumn in the index, and sort it:>>> crime = (pd.read_hdf('data/crime.h5', 'crime') ... .set_index('REPORTED_DATE') ... .sort_index() ... ) - The
DatetimeIndexhas many of the same attributes and methods as a pandasTimestamp. Let's take a look at some that they have in common:>>> common_attrs = (set(dir(crime.index)) & ... set(dir(pd.Timestamp))) >>> [attr for attr in common_attrs if attr[0] != '_'] ['tz_convert', 'is_month_start', 'nanosecond...