This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import plotly.express as px | |
fig = px.line(df, x="date", y="close", template="plotly_white", | |
title=f"Stock price history for {symbol}") | |
fig.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resp = get_prices(symbol, iex_tkn, date_param=date_range) | |
if resp is not None: | |
prices_obj = json.loads(resp.text) | |
df = pd.DataFrame(prices_obj) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open("../../tokens/iex_token.txt", "r") as f: | |
iex_tkn = f.read().strip() | |
date_range = "3m" | |
symbol = "MSFT" | |
resp = get_prices(symbol, iex_tkn, date_param=date_range) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_prices(symbol, key, date_param='5d'): | |
url_prefix = "https://cloud.iexapis.com/stable/" | |
path = f'stock/{symbol}/chart/{date_param}?chartCloseOnly=True&&token={key}' | |
print(f"Fetching {date_param} data for {symbol}") | |
full_url = requests.compat.urljoin(url_prefix, path) | |
try: | |
resp = requests.get(full_url) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add column for cold/neutral/hot start - based on (n_streak-margin) / n_streak makes or misses) | |
shots_df = shots_df.assign(start="Unknown") | |
for pl in players: | |
logger.info(f"Processing data for {pl}") | |
dates = shots_df[shots_df.player == pl].date.unique() | |
for date in dates: | |
day_filter = ((shots_df.player == pl) & (shots_df.date == date)) | |
day_df = shots_df[day_filter].sort_values("tot_time") | |
if len(day_df) > n_streak: | |
if day_df[:n_streak]["shot_made"].sum() >= n_streak - margin: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ===== Group dataframe by player & game | |
players = shots_df.groupby("player").count()["date"].sort_values()[-200:].index | |
pl_df = shots_df[shots_df.player.isin(players)] | |
grp_pl_df = pl_df.groupby(["player", "date"]).shot_made.sum().reset_index() | |
grp_pl_df = grp_pl_df.assign(shot_count=pl_df.groupby(["player", "date"]).shot_made.count().values) | |
grp_pl_df = grp_pl_df.assign(shot_acc=grp_pl_df["shot_made"]/grp_pl_df["shot_count"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Merge income data & plot | |
county_df = county_df.join(data_df[["fips", "Median_Household_Income_2018", "POP_ESTIMATE_2018"]].set_index("fips"), on="fips", how="left") | |
county_df["POP_ESTIMATE_2018"].fillna(0, inplace=True) | |
fig = px.scatter_mapbox(county_df, lat="lat", lon="lon", color="Median_Household_Income_2018", size="POP_ESTIMATE_2018", | |
range_color=[40000, 70000], size_max=40, | |
color_continuous_scale=px.colors.diverging.RdYlBu, labels=labels | |
) | |
fig.update_layout(mapbox_style="carto-positron", | |
mapbox_zoom=3.9, mapbox_center={"lat": 37.0902, "lon": -95.7129}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> county_df.head() | |
fips lon lat | |
0 01001 -86.664383 32.524095 | |
1 01009 -86.633437 34.012316 | |
2 01017 -85.358299 32.918304 | |
3 01021 -86.697066 32.866796 | |
4 01033 -87.778236 34.729641 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig = px.choropleth_mapbox(tmp_df, locations="fips", color="Median_Household_Income_2018", | |
range_color=[20000, 40000], | |
geojson=counties, color_continuous_scale=px.colors.sequential.Oranges[::-1], labels=labels) | |
fig.update_layout(mapbox_style="carto-positron", | |
mapbox_zoom=3.9, mapbox_center={"lat": 37.0902, "lon": -95.7129}, | |
margin={"r": 0, "t": 0, "l": 0, "b": 0}) | |
fig.update_traces(marker=dict(line=dict(width=0.3, color="gray"))) | |
fig.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
county_data_list = list() | |
for tmp in counties["features"]: | |
tmp_fips = tmp["id"] | |
if tmp["geometry"]["type"] == 'Polygon': | |
tmp_crds = tmp["geometry"]["coordinates"][0] | |
elif tmp["geometry"]["type"] == 'MultiPolygon': | |
tmp_crds = tmp["geometry"]["coordinates"][0][0] | |
else: | |
logger.warning("Check geometry type") | |
x_crds = [i[0] for i in tmp_crds] |