Skip to content

Instantly share code, notes, and snippets.

@git-shogg
git-shogg / Google_Timeline_Summary-Step_01.py
Last active July 25, 2021 03:31
Google_Timeline_Summary-Medium_Steps
###############################################################################
# 1. Importing Libraries #
###############################################################################
import json
import datetime
import pandas as pd
from geopy import distance
@git-shogg
git-shogg / Google_Timeline_Summary-Step_02.py
Created July 25, 2021 03:32
Google_Timeline_Summary-Step_02.py
###############################################################################
# 2. User Inputs #
###############################################################################
start_year, start_month, start_day = 2021, 6, 30
end_year, end_month, end_day = 2021, 7, 25
point_of_interest = (-77.85, 166.64) # Latitude, Longitude
distance_from_point_of_interest = 0.1
aggregate_by = 'Day' # This can be 'Year', 'Month', 'Day', 'Hour', 'Second'
weekday_exceptions = ['Saturday','Sunday'] # A list of weekdays that should excluded.
@git-shogg
git-shogg / Google_Timeline_Summary-Step_03.py
Created July 25, 2021 03:33
Google_Timeline_Summary-Step_03.py
###############################################################################
# 3. Get Data #
###############################################################################
json_file = "Location History.json"
data = json.load(open(json_file)) # Read the file
locations = data['locations']
@git-shogg
git-shogg / Google_Timeline_Summary-Step_04.py
Created July 25, 2021 03:34
Google_Timeline_Summary-Step_04.py
###############################################################################
# 4. Unpack the Data #
###############################################################################
df_rows = []
for key in locations:
timestamp = float(key['timestampMs'])
latitude = float(key['latitudeE7'])/10000000
longitude = float(key['longitudeE7'])/10000000
if timestamp >= start_datetime and timestamp <= end_datetime:
@git-shogg
git-shogg / Google_Timeline_Summary-Step_05.py
Last active July 26, 2021 10:43
Google_Timeline_Summary-Step_05.py
###############################################################################
# 5. Setup Useful Columns #
###############################################################################
# Determine the distance of each GPS coordinate from your point of interest.
distances_list = []
for lat, long in zip(df['latitude'],df['longitude']):
distances_list.append(distance.geodesic(point_of_interest,(lat,long)).km)
df['distance'] = distances_list
df = df[df['distance'] < distance_from_point_of_interest]
@git-shogg
git-shogg / Google_Timeline_Summary-Step_06.py
Last active July 26, 2021 10:17
Google_Timeline_Summary-Step_06.py
###############################################################################
# 6. Remove Weekday Exceptions and Aggregate the Results #
###############################################################################
# Remove Weekday Exceptions
if weekday_exceptions != None:
df = df[~df.weekday.isin(weekday_exceptions)]
# Aggregate the Results
df = df.groupby(aggregate_list).agg({'timestamp':['count'],'distance':['mean']})
#--- Import the Required Libraries ---
import requests
import json
import pandas as pd
import open_figi_key
import time
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
[{"name":"PAULSON & CO. INC.", "cik":"0001035674"},
{"name":"BAUPOST GROUP LLC", "cik":"0001061768"},
{"name":"BERKSHIRE HATHAWAY INC", "cik":"0001067983"},
{"name":"GREENLIGHT CAPITAL INC", "cik":"0001079114"},
{"name":"LONE PINE CAPITAL LLC", "cik":"0001061165"},
{"name":"SOROS FUND MANAGEMENT LLC", "cik":"0001029160"},
{"name":"APPALOOSA LP", "cik":"0001656456"},
{"name":"GOLDMAN SACHS GROUP INC", "cik":"0000886982"},
{"name":"SCION ASSET MANAGEMENT, LLC", "cik":"0001649339"},
{"name":"BRIDGEWATER ASSOCIATES, LP", "cik":"0001350694"},
# --- Function for running quick statistics on the dataframe ---
def dataframe_statistics(df, manager_name:str, cik:str):
'''
Build out the holdings dataframes with some basic statistics.
'''
df['Portfolio percentage'] = (df['Holding value'] / df['Holding value'].sum()) * 100
df['Manager Name'] = manager_name
df['CIK'] = cik
return df
# --- Functions for mapping CUSIPs to Tickers ---
def write_cusips_to_txt(cusip_mappings, cusip_mapping_file = 'cusips.txt'):
'''
Write cusip mappings to txt. Minimizing the need for API calls.
'''
with open(cusip_mapping_file, 'w') as f:
f.write(str(cusip_mappings))
return
def read_cusips_from_txt(cusip_mapping_file='cusips.txt'):