1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import pandas as pd
from datetime import datetime, timedelta
data =pd.read_csv('C:/Users/admin/Desktop/test.csv')
def time_format(x):
dt = datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f')
mod = (dt.minute // 15)
if mod == 0:
if dt.minute == 0:
return datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute,
dt.second, dt.microsecond)
else:
minute = mod * 15
return datetime(dt.year, dt.month, dt.day, dt.hour, minute,
dt.second, dt.microsecond) + timedelta(minutes=15)
else:
minute = mod * 15
return datetime(dt.year, dt.month, dt.day, dt.hour, minute,
dt.second, dt.microsecond) + timedelta(minutes=15)
df = data.fillna(0)
df['DateTimeKey'] = df['DateTimeKey'].apply(time_format)
print(df.groupby('DateTimeKey', as_index=False).sum()) |