Python Pandas Group by date using datetime data

Python Pandas Group by date using datetime data

In this article, Python Pandas Group by date using datetime data, it's time to dig into it and understand it! Grouping data is one of the most used functions in business intelligence,

import pandas as pd

edges = pd.to_datetime([x for year in df.index.year.unique() 
                        for x in [f'{year}-04-20', f'{year}-05-29']])

def min_idx(x):
    return x.index.min()
def max_idx(x):
    return x.index.max()

df.groupby(pd.cut(df.index, bins=edges)).agg([min_idx, max_idx, min, max]).loc[::2, :]

 

df.resample('D', on='Date_Time').mean()

              B
Date_Time      
2020-10-01  4.5
2020-10-02  6.0

 

Grouper

 

df.set_index('Date_Time').groupby(pd.Grouper(freq='D')).mean()

              B
Date_Time      
2020-10-01  4.5
2020-10-02  6.0

 

You can use groupby by dates of column Date_Time by dt.date

 

df = pd.DataFrame({'Date_Time': pd.date_range('10/1/2020 10:00:00', periods=3, freq='10H'),
                   'B':[4,5,6]})

print (df)
   B           Date_Time
0  4 2020-10-01 10:00:00
1  5 2020-10-01 20:00:00
2  6 2020-10-02 06:00:00

print (df['Date_Time'].dt.date)
0    2020-10-01
1    2020-10-01
2    2020-10-02
Name: Date_Time, dtype: object

df = df.groupby([df['Date_Time'].dt.date])['B'].mean()
print(df)
Date_Time
2020-10-01    4.5
2020-10-02    6.0
Name: B, dtype: float64

 

df = df.set_index('Date_Time').resample('D')['B'].mean()

print(df)
Date_Time
2020-10-01    4.5
2020-10-02    6.0
Freq: D, Name: B, dtype: float64

 

 

2021-10-03