Search code examples
pythonpython-3.xpandaspandas-groupbypython-datetime

How can I group dates into pandas


    Datos
2015-01-01  58
2015-01-02  42
2015-01-03  41
2015-01-04  13
2015-01-05  6
...     ...
2020-06-18  49
2020-06-19  41
2020-06-20  23
2020-06-21  39
2020-06-22  22

2000 rows × 1 columns

I have this df which is made up of a column whose data represents the average temperature of each day in an interval of years. I would like to know how to get the maximum of each day (taking into account that the year has 365 days) and obtain a df similar to this:

        Datos
1   40
2   50
3   46
4   8
5   26
...     ...
361     39
362     23
363     23
364     37
365     25

365 rows × 1 columns

Forgive my ignorance and thank you very much for the help.


Solution

  • You can do this:

    df['Date'] = pd.to_datetime(df['Date'])
    df = df.groupby(by=pd.Grouper(key='Date', freq='D')).max().reset_index()
    df['Day'] = df['Date'].dt.dayofyear
    print(df)
    
               Date  Temp  Day
    0    2015-01-01  58.0    1
    1    2015-01-02  42.0    2
    2    2015-01-03  41.0    3
    3    2015-01-04  13.0    4
    4    2015-01-05   6.0    5
    ...         ...   ...  ...
    1995 2020-06-18  49.0  170
    1996 2020-06-19  41.0  171
    1997 2020-06-20  23.0  172
    1998 2020-06-21  39.0  173
    1999 2020-06-22  22.0  174