Readings That Can't Be Right

Data with PythonMedium+10 XP+10

Readings That Can't Be Right

A garden weather station saves its readings to a CSV file. Sometimes a sensor glitches and writes an impossible number, like a humidity of 140%. Some cells are also empty, where a reading was missed.

1hour,temp_c,humidity,wind_kmh
26,8.5,91,4
37,9.1,,6
48,10.4,140,5

Before cleaning anything, you want a quick look at which columns contain nonsense.

The starter code gives you write_text(path, text), which saves text into a file called path.

Task: write out_of_range_columns(path, csv_text, limits).

  • Save csv_text to path with write_text, then load it with pandas.
  • limits is a dictionary like {"humidity": [0, 100]}. Each key is a column name, and its value is [lowest allowed, highest allowed].
  • A column is out of range if its smallest reading is below its lowest allowed value, or its largest reading is above its highest allowed value. A reading exactly on a limit is fine.
  • Empty cells are missing readings. Ignore them.
  • Return a list of the out-of-range column names, in the same order as the keys of limits. If every column is fine, return [].

Every column named in limits holds numbers and has at least one reading.

Examples

path = "garden_mon.csv"
csv_text = "hour,temp_c,humidity,wind_kmh\n6,8.5,91,4\n7,9.1,,6\n8,10.4,140,5\n9,12.0,85,7\n"
limits = {"temp_c": [-30, 45], "humidity": [0, 100], "wind_kmh": [0, 150]}
returns ['humidity']
path = "garden_tue.csv"
csv_text = "hour,temp_c,humidity,wind_kmh\n6,-999,88,3\n7,7.2,90,-2\n8,8.8,100,4\n"
limits = {"humidity": [0, 100], "wind_kmh": [0, 150], "temp_c": [-30, 45]}
returns ['wind_kmh', 'temp_c']

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run