Skip to content

Commit

Permalink
Add row names to pivot and denormalize output. Closes wireservice#515.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed Feb 26, 2016
1 parent 188f122 commit 91f9777
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
31 changes: 27 additions & 4 deletions agate/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,9 @@ def pivot(self, key, pivot=None, aggregation=None, computation=None, default_val
| asian | 0 | 1 |
+---------+---------+--------+
If one or more keys are specified then the resulting table will
automatically have `row_names` set to those keys.
See also the related method :meth:`Table.denormalize`.
:param key:
Expand Down Expand Up @@ -1270,13 +1273,21 @@ def normalize(self, key, properties, property_column='property', value_column='v
new_column_names = key + [property_column, value_column]
new_column_types = [self.column_types[self.column_names.index(name)] for name in key] + [Text(), Text()]

row_names = []

for row in self.rows:
left_row = [row[n] for n in key]
k = tuple(row[n] for n in key)
left_row = list(k)

if len(k) == 1:
row_names.append(k[0])
else:
row_names.append(k)

for f in properties:
new_rows.append(Row(tuple(left_row + [f, row[f]]), new_column_names))

return Table(new_rows, new_column_names, new_column_types)
return Table(new_rows, new_column_names, new_column_types, row_names=row_names)

@allow_tableset_proxy
def denormalize(self, key=None, property_column='property', value_column='value', default_value='default'):
Expand Down Expand Up @@ -1311,12 +1322,16 @@ def denormalize(self, key=None, property_column='property', value_column='value'
| Joe | male | black | 28 |
+---------+----------+--------+-------+
If one or more keys are specified then the resulting table will
automatically have `row_names` set to those keys.
This is the opposite of :meth:`Table.normalize`.
:param key:
A column name or a sequence of column names that should be
maintained as they are in the normalized table. Typically these
are the tables unique identifiers and any metadata about them.
are the tables unique identifiers and any metadata about them. Or,
:code:`None` if there are no key columns.
:param field_column:
The column whose values should become column names in the new table.
:param property_column:
Expand Down Expand Up @@ -1363,10 +1378,16 @@ def denormalize(self, key=None, property_column='property', value_column='value'
new_column_types = [self.column_types[self.column_names.index(name)] for name in key] + data_types

new_rows = []
row_names = []

for k, v in row_data.items():
row = list(k)

if len(k) == 1:
row_names.append(k[0])
else:
row_names.append(k)

for f in field_names:
if f in v:
row.append(v[f])
Expand All @@ -1375,7 +1396,9 @@ def denormalize(self, key=None, property_column='property', value_column='value'

new_rows.append(Row(row, new_column_names))

return Table(new_rows, new_column_names, new_column_types)
print(new_rows[0])

return Table(new_rows, new_column_names, new_column_types, row_names=row_names)

@allow_tableset_proxy
def group_by(self, key, key_name=None, key_type=None):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,7 @@ def test_pivot(self):
)

self.assertColumnNames(pivot_table, ['race', 'male', 'female'])
self.assertRowNames(pivot_table, ['white', 'black', 'latino', 'asian'])
self.assertColumnTypes(pivot_table, [Text, Number, Number])
self.assertRows(pivot_table, pivot_rows)

Expand Down Expand Up @@ -2061,6 +2062,13 @@ def test_pivot_multiple_keys(self):

self.assertRows(pivot_table, pivot_rows)
self.assertColumnNames(pivot_table, ['race', 'gender', '20', '25'])
self.assertRowNames(pivot_table, [
('white', 'male'),
('white', 'female'),
('black', 'male'),
('latino', 'male'),
('asian', 'female'),
])
self.assertColumnTypes(pivot_table, [Text, Text, Number, Number])

def test_pivot_multiple_keys_no_pivot(self):
Expand Down Expand Up @@ -2240,6 +2248,7 @@ def test_denormalize(self):
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['first_name', 'gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Text, Text])
self.assertRowNames(normalized_table, ['Jane', 'Jim'])

def test_denormalize_no_key(self):
table = Table(self.rows, self.column_names, self.column_types)
Expand Down Expand Up @@ -2269,6 +2278,7 @@ def test_denormalize_multiple_keys(self):
self.assertRows(normalized_table, normal_rows)
self.assertColumnNames(normalized_table, ['first_name', 'last_name', 'gender', 'age'])
self.assertColumnTypes(normalized_table, [Text, Text, Text, Text])
self.assertRowNames(normalized_table, [('Jane', 'Code'), ('Jim', 'Program'), ('Jim', 'Bytes')])

def test_denormalize_default_value(self):
table = Table(self.rows, self.column_names, self.column_types)
Expand Down

0 comments on commit 91f9777

Please sign in to comment.