How to know a group of dates are daily, weekly or monthly in Pandas Python?
Question: I have a dataframe in Pandas with the date as index. "YYYY-MM-DD" format. I
have a lot of rows in this dataframe which means a lot of date indexes.

For all of these dates, most of them are daily continuous, some of them are
weekly dates, some are yearly.

Example:

    
    
    2015-01-05,
    2015-01-06,
    2015-01-07,
    2015-01-08,
    2015-01-09,
    2015-01-16,
    2015-01-23,
    2015-01-30,
    2015-02-28,
    2015-03-30
    

So Some of them are daily dates and maybe then follow by several monthly dates
or weekly dates, yearly dates.

**So how can I know in which dates duration, it is daily, weekly, monthly and
yearly?**

Remark: the daily one only have working day dates (Monday - Friday). For
Weekly dates, the Friday dates will be displayed. For Monthly/Quarterly/Yearly
dates, the last day of this month/quarter/year will be displayed.


Answer: Using the Pandas type [Timedelta](http://pandas.pydata.org/pandas-
docs/stable/timedeltas.html).

> Starting in v0.15.0, we introduce a new scalar type Timedelta, which is a
> subclass of datetime.timedelta, and behaves in a similar manner, but allows
> compatibility with np.timedelta64 types as well as a host of custom
> representation, parsing, and attributes.

First value in col `days` is `NaN`, which is corrected by `df = df.fillna(1)`.

    
    
    df['days'] = (df['date']-df['date'].shift()).dt.days
    

Other solutions for timedelta column `days` are:

    
    
    df['days'] = (df['date']-df['date'].shift()).fillna(0)
    df['days'] = df['date'].diff()
    

Then I consolidate different days of months and years and group by them.

    
    
    import pandas as pd
    import io
    
    temp=u"""2015-01-05
    2015-01-06
    2015-01-07
    2015-01-08
    2015-01-09
    2015-01-16
    2015-01-23
    2015-01-30
    2015-02-28
    2015-03-31
    2015-04-30
    2016-04-30"""
    
    df = pd.read_csv(io.StringIO(temp), parse_dates=True, index_col=[0], header=None)
    
    df['date'] = df.index
    df['days'] = (df['date']-df['date'].shift()).dt.days
    
    #normalize days of months for grouping to 30
    #normalize days of years for grouping to 365
    df['days_normalize'] = df['days'].replace([28,29,31], 30)
    df['days_normalize'] = df['days_normalize'].replace(366, 365)
    #replace firs value to 1 - correct unshifted first value
    df = df.fillna(1)
    print df
    
    #                 date  days  days_normalize
    #0
    #2015-01-05 2015-01-05     1               1
    #2015-01-06 2015-01-06     1               1
    #2015-01-07 2015-01-07     1               1
    #2015-01-08 2015-01-08     1               1
    #2015-01-09 2015-01-09     1               1
    #2015-01-16 2015-01-16     7               7
    #2015-01-23 2015-01-23     7               7
    #2015-01-30 2015-01-30     7               7
    #2015-02-28 2015-02-28    29              30
    #2015-03-31 2015-03-31    31              30
    #2015-04-30 2015-04-30    30              30
    #2016-04-30 2016-04-30   366             365
    
    grouped = df.groupby("days_normalize")
    
    for name, group in grouped:
        print(group)
    

