Skip to content

Instantly share code, notes, and snippets.

@marquicus
Created January 23, 2020 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marquicus/8fb73b3d1dbcdbf40dd7eeb127dab41d to your computer and use it in GitHub Desktop.
Save marquicus/8fb73b3d1dbcdbf40dd7eeb127dab41d to your computer and use it in GitHub Desktop.
Task: write a script to get a nice CSV file of natural gas prices.
# 1. Main data wanted is daily prices. (I downloaded the Henry_Hub_Natural_Gas_Spot_Price.csv with view history as daily)
import pandas as pd
dfg = pd.read_csv('Henry_Hub_Natural_Gas_Spot_Price.csv', skiprows=4)
dfg.index = pd.to_datetime(dfg["Day"],format='%m/%d/%Y')
dfg.to_csv('gas_byday.csv', index=False) # => first output gas_byday.csv file
# 1.1 Bonus points for doing other granularities (e.g. month) - do them in separate CSV files with sensible naming Resulting CSV should have two columns: Date and Price. You may need to normalize the data to get this and/or work out dates. For months the Date should be the first date of the month.
dfg_month = dfg['Henry Hub Natural Gas Spot Price Dollars per Million Btu'].resample('M').sum()
df = pd.DataFrame(dfg_month, index=dfg_month.index.strftime("%d/%m/%Y"))
df.to_csv('gas_bymonth.csv', index=True) # => second output gas_bymonth.csv file
# We want a script for this and we want this script to be in python (we'd allow node or bash or go script at a push but prefer python)
# Why a script? Ans: We'll want to run this again and again as they release new data. You could copy and paste data into Excel/Google Docs by hand, and then export the CSV. But that would be tedious, time consuming and error prone to do month after month
# Please use simple python libraries wherever possible rather than use a framework
# Bonus items (optional - extra kudos if you do either of these!):
# Make your repository into a Tabular Data Package - here's a guide
# Do a line graph visualization of the data in HTML + Javascript using e.g. vega or direct in D3
# Deploy your repo somewhere so this visualization is visitable online e.g. via github or gitlab pages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment