Example of Using the Utility Bill data in a Notebook


In [1]:
import pickle
import pandas as pd
import numpy as np
import benchmark         # the main benchmarking script file
import bench_util        # some utility functions related to benchmarking

# Just in case you want to plot something:
import matplotlib.pyplot as plt
 
# Show Plots in the Notebook
%matplotlib inline

# Increase the size of plots and their fonts
plt.rcParams['figure.figsize']= (10, 8)   # set Chart Size
plt.rcParams['font.size'] = 14            # set Font size in Chart

# Use a plotting style'style' the plot using 'bmh' style
plt.style.use('bmh')

Using Data from the Last Run of the Script

This saves about 4 minutes of time because reading the raw data and pre-processing it is avoided.


In [2]:
# Read the raw Utility Bill data into a DataFrame
df_raw = pd.read_pickle('df_raw.pkl')

# Read the pre-processed data
dfp = pd.read_pickle('df_processed.pkl')

# Read in the bench_util.Util object, which provides some other data that may 
# be useful.
util_obj = pickle.load(open('util_obj.pkl', 'rb'))

Take a look at what you got


In [3]:
df_raw.head(3)


Out[3]:
Site ID Site Name Vendor Code Vendor Name Account Number Bill Date Due Date Entry Date Invoice # Voucher # From Thru Service Name Item Description Meter Number Usage Cost Units Account Financial Code Site Financial Code
0 TRGR FNSB-Transit Garage VP287678 Sourdough Fuel (Petro Star) 00013297 (closed) 09/28/2010 09/28/2010 01/26/2011 NaN NaN 2008-11-19 2010-09-28 Oil #1 FED LUS TX NaN NaN 3.00 NaN NaN NaN
1 TRGR FNSB-Transit Garage VP287678 Sourdough Fuel (Petro Star) 00013297 (closed) 09/28/2010 09/28/2010 01/26/2011 NaN NaN 2008-11-19 2010-09-28 Oil #1 Fuel Oil #1 (Gallons) NaN 3000.0 7950.00 Gallons NaN NaN
2 TRGR FNSB-Transit Garage VP287678 Sourdough Fuel (Petro Star) 00013297 (closed) 09/30/2010 09/30/2010 01/26/2011 NaN NaN 2010-09-28 2010-09-30 Oil #1 FED LUS TX NaN NaN 1.31 NaN NaN NaN

In [4]:
dfp.head(3)


Out[4]:
site_id service_type cal_year cal_mo item_desc units cost days_served usage fiscal_year fiscal_mo mmbtu
0 03 Electricity 2005 12 Electricity charge kWh 1904.657880 49.5 14790.748577 2006 6 50.466034
1 03 Electricity 2006 1 Electricity charge kWh 5430.493797 93.0 42665.790911 2006 7 145.575679
2 03 Electricity 2006 2 Electricity charge kWh 5764.406730 84.0 45010.439348 2006 8 153.575619

In [5]:
# Get some other building info about the Big Dipper building
# A dictionary is returned.
util_obj.building_info('DIPMP1')


Out[5]:
{'acct_elec': '112651',
 'acct_nat_gas': '10282  (1920 LATHROP)',
 'acct_oil': '39384007  (BIG DIPPER)',
 'acct_refuse': '',
 'acct_sewer': '1995008500',
 'acct_steam': '',
 'acct_water': '1995008500',
 'address': nan,
 'city': nan,
 'dd_site': 'PAFA',
 'full_address': nan,
 'onsite_gen': nan,
 'primary_func': nan,
 'site_category': 'Other',
 'site_id': 'DIPMP1',
 'site_name': 'FNSB-Big Dipper',
 'source_elec': 'Golden Valley Electric',
 'source_nat_gas': 'Fairbanks Natural Gas',
 'source_oil': 'Sourdough Fuel  (Petro Star)',
 'source_refuse': '',
 'source_sewer': 'Golden Heart Utilities',
 'source_steam': '',
 'source_water': 'Golden Heart Utilities',
 'sq_ft': 63845.0,
 'year_built': nan}

Instead, Rerun the Preprocessing routine to get the DataFrames and Util object


In [6]:
# This calls the preprocessing function in the main benchmark script file.
# Values in the 'settings.py' file will determine which files are read.
# NOTE:  This will take 4 minutes to run, and the Jupyter Notebook does not 
# always show the messages that printed along the way, so it will look like
# your computer is frozen. Be patient.

# Uncomment the line below to run
# df_raw, dfp, util_obj = benchmark.preprocess_data()

Look at Some Data to Illustrate the Duplicate kW Demand Charge Problem


In [7]:
# You can't use the Pandas "query" method for columns that have spaces in
# their names.  So use the more general, but cumbersome syntax to filter 
# the "Site ID" column.
# NOTE: a quick rename of the columns to eliminate spaces is another approach:
#  df_raw.columns = [c.replace(' ', '_') for c in df_raw.columns]
df_dip = df_raw[df_raw['Site ID']=='DIPMP1']

df_dip.query("From < '2017-06-01' and From > '2017-01-01' and Units=='kW'")


Out[7]:
Site ID Site Name Vendor Code Vendor Name Account Number Bill Date Due Date Entry Date Invoice # Voucher # From Thru Service Name Item Description Meter Number Usage Cost Units Account Financial Code Site Financial Code
17620 DIPMP1 FNSB-Big Dipper VG354933 Golden Valley Electric 112651 02/13/2017 02/13/2017 10/09/2017 NaN NaN 2017-01-16 2017-02-13 Electricity Actual demand NaN 290.5 4151.53 kW 61831.0 NaN
17624 DIPMP1 FNSB-Big Dipper VG354933 Golden Valley Electric 112651 02/14/2017 02/14/2017 03/13/2017 NaN NaN 2017-01-16 2017-02-13 Electricity Demand Charge NaN 290.5 4151.53 kW 61831.0 NaN
17629 DIPMP1 FNSB-Big Dipper VG354933 Golden Valley Electric 112651 03/14/2017 03/14/2017 10/09/2017 NaN NaN 2017-02-13 2017-03-14 Electricity Actual demand NaN 264.9 3785.71 kW 61831.0 NaN
17633 DIPMP1 FNSB-Big Dipper VG354933 Golden Valley Electric 112651 03/15/2017 03/15/2017 03/22/2017 NaN NaN 2017-02-13 2017-03-14 Electricity Demand Charge NaN 264.9 3785.71 kW 61831.0 NaN
17638 DIPMP1 FNSB-Big Dipper VG354933 Golden Valley Electric 112651 04/14/2017 04/14/2017 10/09/2017 NaN NaN 2017-03-14 2017-04-14 Electricity Actual demand NaN 286.3 4090.94 kW 61831.0 NaN
17642 DIPMP1 FNSB-Big Dipper VG354933 Golden Valley Electric 112651 04/17/2017 04/17/2017 04/18/2017 NaN NaN 2017-03-14 2017-04-14 Electricity Demand Charge NaN 286.3 4090.94 kW 61831.0 NaN
17649 DIPMP1 FNSB-Big Dipper VG354933 Golden Valley Electric 112651 05/15/2017 05/15/2017 07/03/2017 NaN NaN 2017-04-14 2017-05-12 Electricity Demand Charge NaN 278.5 3979.48 kW 61831.0 NaN
17656 DIPMP1 FNSB-Big Dipper VG354933 Golden Valley Electric 112651 06/15/2017 06/15/2017 08/02/2017 NaN NaN 2017-05-12 2017-06-14 Electricity Demand Charge NaN 270.1 3860.01 kW 61831.0 NaN

In [8]:
# Look at the processed data to compare Actual Demand vs. Demand Charge
df_dip = dfp.query("site_id=='DIPMP1' and units=='kW'")
df_pivot = pd.pivot_table(df_dip, index=['cal_year', 'cal_mo'], values='usage', columns='item_desc', aggfunc=np.sum)

# Do the following to show all rows of the DataFrame
with pd.option_context('display.max_rows', None):
    print(df_pivot)


item_desc        Actual demand  Demand Charge   KW Charge  On peak demand
cal_year cal_mo                                                          
2006     6                 NaN            NaN  117.000000             NaN
         7                 NaN            NaN  234.000000             NaN
         8                 NaN            NaN  241.548387             NaN
         9                 NaN            NaN  287.701613             NaN
         10                NaN            NaN  320.078125             NaN
         11                NaN            NaN  289.821875             NaN
         12                NaN            NaN  285.521875             NaN
2007     1                 NaN            NaN  295.628125             NaN
         2                 NaN            NaN  271.307143             NaN
         3                 NaN            NaN  278.976190             NaN
         4                 NaN            NaN  266.223118             NaN
         5                 NaN            NaN  243.240423             NaN
         6                 NaN            NaN  232.734375             NaN
         7                 NaN            NaN  139.218750             NaN
         8                 NaN            NaN  154.800000             NaN
         9                 NaN            NaN  321.336000             NaN
         10                NaN            NaN  302.118545             NaN
         11                NaN            NaN  265.931661             NaN
         12                NaN            NaN  266.086520             NaN
2008     1                 NaN            NaN  254.467273             NaN
         2                 NaN            NaN  257.811724             NaN
         3                 NaN            NaN  277.312792             NaN
         4                 NaN            NaN  251.175484             NaN
         5                 NaN            NaN  250.860000             NaN
         6                 NaN            NaN  229.125000             NaN
         7                 NaN            NaN  245.175000             NaN
         8                 NaN            NaN  257.118750             NaN
         9                 NaN            NaN  267.881250             NaN
         10                NaN            NaN  270.300000             NaN
         11                NaN            NaN  262.706897             NaN
         12                NaN            NaN  255.940162             NaN
2009     1                 NaN            NaN  259.894320             NaN
         2                 NaN            NaN  249.558621             NaN
         3                 NaN            NaN  278.160000             NaN
         4                 NaN            NaN  279.329655             NaN
         5                 NaN            NaN  281.265517             NaN
         6                 NaN            NaN  236.276256             NaN
         7                 NaN            NaN  257.579286             NaN
         8                 NaN            NaN  271.126786             NaN
         9                 NaN            NaN  287.838362             NaN
         10                NaN            NaN  330.574138             NaN
         11                NaN            NaN  298.516129             NaN
         12                NaN            NaN  290.526058             NaN
2010     1                 NaN            NaN  296.922098             NaN
         2                 NaN            NaN  286.935714             NaN
         3                 NaN            NaN  301.627273             NaN
         4                 NaN            NaN  283.897727             NaN
         5                 NaN            NaN  306.066935             NaN
         6                 NaN            NaN  298.296774             NaN
         7                 NaN            NaN  301.288877             NaN
         8                 NaN            NaN  293.287565             NaN
         9                 NaN            NaN  307.093469             NaN
         10                NaN            NaN  325.981034             NaN
         11                NaN            NaN  283.015032             NaN
         12                NaN            NaN  280.706603      149.314516
2011     1                 NaN            NaN  275.290323      115.185484
         2                 NaN            NaN  244.673387             NaN
         3                 NaN            NaN  264.899194             NaN
         4                 NaN            NaN  263.432703             NaN
         5                 NaN            NaN  263.527478             NaN
         6                 NaN            NaN  245.523690             NaN
         7                 NaN            NaN  269.420507             NaN
         8                 NaN            NaN  247.130252             NaN
         9                 NaN            NaN  255.846349             NaN
         10                NaN            NaN  287.280311             NaN
         11                NaN            NaN  271.271413             NaN
         12                NaN            NaN  262.337221             NaN
2012     1                 NaN            NaN  261.817951             NaN
         2                 NaN            NaN  262.879310             NaN
         3                 NaN            NaN  278.175287             NaN
         4                 NaN            NaN  267.083333             NaN
         5                 NaN            NaN  265.451613             NaN
         6                 NaN            NaN  243.357762       16.860938
         7                 NaN     155.962069  124.290625       15.839063
         8                 NaN     135.837931  143.036765             NaN
         9                 NaN            NaN  134.863235      140.207143
         10                NaN            NaN         NaN      303.987857
         11                NaN            NaN  149.930000      125.505000
         12                NaN            NaN  282.904375             NaN
2013     1                 NaN            NaN  132.765625      166.137931
         2                 NaN            NaN         NaN      275.250000
         3                 NaN            NaN         NaN      292.560345
         4                 NaN            NaN         NaN      256.197037
         5                 NaN            NaN         NaN      256.054688
         6                 NaN            NaN         NaN      251.308065
         7                 NaN            NaN         NaN      263.641935
         8                 NaN            NaN         NaN      255.012121
         9                 NaN            NaN         NaN      122.637879
         10                NaN            NaN         NaN      162.800000
         11                NaN     161.472414         NaN      133.200000
         12                NaN     282.044253         NaN             NaN
2014     1                 NaN     288.817816         NaN             NaN
         2                 NaN     275.812069         NaN             NaN
         3                 NaN     282.358287         NaN             NaN
         4                 NaN     261.765161         NaN             NaN
         5                 NaN     278.330000         NaN             NaN
         6                 NaN     226.812500         NaN             NaN
         7                 NaN     207.847500         NaN             NaN
         8                 NaN     206.552500         NaN             NaN
         9                 NaN     238.946121         NaN             NaN
         10                NaN     287.567186         NaN             NaN
         11                NaN     284.424194         NaN             NaN
         12                NaN     258.832353         NaN             NaN
2015     1                 NaN     262.114199         NaN             NaN
         2                 NaN     265.621305         NaN             NaN
         3                 NaN     272.450893         NaN             NaN
         4                 NaN     255.181250         NaN             NaN
         5                 NaN     281.281034         NaN             NaN
         6                 NaN     264.492403         NaN             NaN
         7                 NaN     259.279788         NaN             NaN
         8                 NaN     249.701462         NaN             NaN
         9                 NaN     260.722899         NaN             NaN
         10                NaN     290.547414         NaN             NaN
         11                NaN     283.834677         NaN             NaN
         12                NaN     273.474698         NaN             NaN
2016     1                 NaN     283.831696         NaN             NaN
         2                 NaN     273.852679         NaN             NaN
         3                 NaN     285.196250         NaN             NaN
         4                 NaN     289.438448         NaN             NaN
         5                 NaN     294.307489         NaN             NaN
         6                 NaN     263.972933         NaN             NaN
         7                 NaN     274.933986         NaN             NaN
         8                 NaN     286.945378         NaN             NaN
         9                 NaN     278.840213         NaN             NaN
         10         169.862903     295.459455         NaN             NaN
         11         131.037097     294.216407         NaN             NaN
         12         139.729412     263.350101         NaN             NaN
2017     1          277.883088     277.883088         NaN             NaN
         2          271.271983     271.271983         NaN             NaN
         3          284.936485     284.936485         NaN             NaN
         4          124.679032     288.795104         NaN             NaN
         5                 NaN     273.988474         NaN             NaN
         6                 NaN     200.145455         NaN             NaN
         7                 NaN     243.350000         NaN             NaN
         8                 NaN     281.896875         NaN             NaN
         9                 NaN     115.003125         NaN             NaN