Skip to content

Instantly share code, notes, and snippets.

@Tryum
Created June 29, 2022 08:17
Show Gist options
  • Save Tryum/a4fddf78f6901aa38e4b0bc2afd229b5 to your computer and use it in GitHub Desktop.
Save Tryum/a4fddf78f6901aa38e4b0bc2afd229b5 to your computer and use it in GitHub Desktop.
# %%
import json
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
def datetime_from_js_isoformat(string: str) -> datetime:
"""Creates a datetime object from a JavaScript ISO format string."""
if string.endswith('Z'):
return datetime.fromisoformat(string[:-1])
return datetime.fromisoformat(string)
with open('./cashless.json', 'r', encoding="utf-8") as f:
json_data = json.load(f)
data = []
for t in json_data:
if t['type'] == "transaction":
date = datetime_from_js_isoformat(t['date'])
location = t['location_name']
for r in t['rows'] :
if len(r['payments']) == 0:
continue
item_name = r['item']
if item_name == "Frais d'activation":
continue
assert len(r['totals']) == 1
price = r['totals'][0]['amount'] / -100
quantity = r['quantity']
assert r['totals'][0]['decimal_places'] == 2
data.append((date, location, item_name, price, quantity))
df = pd.DataFrame(data, columns=['date', 'location', 'name', 'price', 'quantity'])
df['price'].sum()
# %%
df.groupby(pd.Grouper(key='date',freq='D')).sum().plot.bar()
# %%
df[(df['date'] > '2022-06-19 04:00') & (df['date'] < '2022-06-20 23:59')].sort_index(ascending=False)
# %%
df.groupby(['location']).sum().sort_values(by=['price'], ascending=False)
# %%
df.groupby(['name']).sum().sort_values(by=['price'], ascending=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment