Skip to content

Instantly share code, notes, and snippets.

@anderser
Last active January 19, 2016 20:32
Show Gist options
  • Save anderser/12d32a25f385f8a7f6d1 to your computer and use it in GitHub Desktop.
Save anderser/12d32a25f385f8a7f6d1 to your computer and use it in GitHub Desktop.
Agate compute method to generate streaks of consecutive values in a dataset. WIP
import agate
class Streaks(agate.Computation):
"""
Computes the streaks of consecutive values in a column.
Each streak will be given an increasing
integer value so that you can group by this later to
find longest consecutive streak.
"""
def __init__(self, column_name):
self._column_name = column_name
def get_computed_data_type(self, table):
"""
The return value is a numerical average.
"""
return agate.Number()
def run(self, table):
new_column = []
streak = 0
for i, row in enumerate(table.rows):
if i > 0:
if row[self._column_name] != table.rows[i - 1][self._column_name]:
streak = streak + 1
new_column.append(streak)
return new_column
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment