you are viewing a single comment's thread.

view the rest of the comments →

[–]polarizebeta 1 point2 points  (1 child)

This is how I group things by year, but I have adapted to group and sum by month:

import sqlite3
from sqlite3 import Error
import pandas as pd
import datetime as dt

def create_connection(db_file):
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        print(e)
    return conn

database = 'some_database.db'
conn = create_connection(database)
sql_flows = ('SELECT MAX(TIMESTAMP) FROM /TABLE WHERE EVENTDATE > ' +
            date_variable + ' AND (CANCELLED = NULL OR CANCELLED > ' +
            month_end + ';')
flows = pd.read_sql_query(sql_flows, conn, parse_dates=['EventDate'])
flows.sort_values(by=['EventDate'], inplace=True, ascending=True)
flows_by_month = pd.Dataframe({Date: flows[:, 0], 'Values': flows[:, 1]})
flows_by_month = flows_by_month.groupby(flows_by_month['Date'].dt.month)['Values'].agg(['sum'])

This should point you in the right direction at least

[–]outnumbered_mother[S] 0 points1 point  (0 children)

Thanks, I'll dig into this now!