Interview Query
~2 hrs
AnalyticsData Structures & AlgorithmsDevOps

CleanSpark Python Exercise: Electricity Bill Calculation

Write two Python functions that calculate the energy and demand charges on a commercial electricity bill from one month of 15-minute consumption data, using a time-of-use tariff.

Cleanspark llcCleanspark llc
Data AnalystData Scientist
Updated 7 days agoReviewed byIQIQ Team

Overview

This exercise is for the Junior Data Scientist/Analyst position at CleanSpark. You are asked to implement two Python functions that calculate the energy and demand charges on an electricity bill, using real 15-minute interval consumption data. Please read this entire document before beginning.


Setup

Use Python 3 and any packages you like. pandas with openpyxl will read the data file:

import pandas as pd
df = pd.read_excel('data.xlsx')

The file has two columns: DateTime (the start of each 15-minute interval) and kWh (energy consumed in that interval).


Background: Energy and Power

At CleanSpark, electricity bills are analyzed every day. An electricity bill is composed of two primary parts: energy charges and maximum-power (demand) charges. To understand these, you first need to understand the difference between energy and power.

Analogy: Think of driving from San Diego to San Francisco (~500 miles). To know how long the trip takes, you need to know your speed. The relationship is:

$$\text{speed} = \frac{\text{distance}}{\text{time}}$$

Extending this to electricity: energy is to distance as power is to speed. Energy is measured in kWh and power is measured in kW:

$$\text{power} = \frac{\text{energy}}{\text{time}}$$

For example, if a building consumed 50 kWh over a 30-minute (0.5 hour) period, the average power was:

$$\text{power} = \frac{50 \text{ kWh}}{0.5 \text{ h}} = 100 \text{ kW}$$


Electricity Bills

An electricity bill has two primary components:

Energy Charges

  • Units: $/kWh
  • Calculated at every 15-minute interval
  • Example: If the energy rate is $0.01/kWh and the building consumed 100 kWh in a 15-minute interval, the charge for that interval is $1.00.
  • All interval charges are summed over the month to produce the total energy charge.

Note: This operation is very similar to a dot product.

Max-Power (Demand) Charges

  • Units: $/kW
  • Calculated once per month based on the maximum average power (demand) in any single 15-minute interval during the month.
  • Example: If the monthly maximum demand is 100 kW and the demand rate is $20/kW, the customer is charged $2,000 for that month.

Problem

You are given one month of electricity consumption data (kWh) in 15-minute intervals for a commercial facility located in San Diego, CA. This data is stored in data.xlsx. Each interval is labeled with the time it began; all intervals are 15 minutes long.

Your task is to write two functions:

  1. energy_charge: calculates the total energy charge for the month
  2. demand_charge: calculates the total demand charge for the month

Your functions must use the electricity consumption data and the pricing scheme (electricity tariff) below.

You may write as many helper functions as you need.

Electricity Tariff

Energy Rates

PeriodRate
Weekends (all hours)$0.05/kWh
Weekdays, 12:00 am – 4:00 pm$0.20/kWh
Weekdays, 4:00 pm – 9:00 pm$0.30/kWh
Weekdays, 9:00 pm – 12:00 am$0.10/kWh

Demand Rate

PeriodRate
Monthly maximum$20/kW

Testing Your Solution

Write your own unit tests with pytest, including small hand-calculated cases for weekends, rate boundaries and the demand charge.


Deliverable

Once you have finished the exercise, include all of your code: your functions and your tests: embedded within a single PDF. Your embedded code must be complete and executable, capable of regenerating all outputs. All results, discussion, and code should appear within this one PDF file.

Download project files