Skip to content

Commit

Permalink
Merge f6e6f95 into 7088ea4
Browse files Browse the repository at this point in the history
  • Loading branch information
twheys committed Sep 24, 2019
2 parents 7088ea4 + f6e6f95 commit 61a0251
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
28 changes: 28 additions & 0 deletions fireant/tests/widgets/test_pandas.py
Expand Up @@ -30,6 +30,9 @@ def _format_float(x):
return '{:,.0f}'.format(x)


pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)


class PandasTransformerTests(TestCase):
maxDiff = None
Expand Down Expand Up @@ -433,6 +436,31 @@ def test_pivoted_dimx2_date_str_with_sort_metric_asc(self):

pandas.testing.assert_frame_equal(expected, result)

def test_pivoted_dimx1_metricx2(self):
result = Pandas(mock_dataset.fields.votes, mock_dataset.fields.wins, pivot=[mock_dataset.fields.timestamp]) \
.transform(dimx2_date_str_df, mock_dataset,
[mock_dataset.fields.timestamp, mock_dataset.fields.political_party], [])

expected = dimx2_date_str_df.copy()[[f('votes'), f('wins')]]
expected = expected.unstack(level=0)
expected.index.names = ['Party']
expected.columns = pd.MultiIndex.from_product(
[
['Votes', 'Wins'],
pd.DatetimeIndex(['1996-01-01',
'2000-01-01',
'2004-01-01',
'2008-01-01',
'2012-01-01',
'2016-01-01']),
],
names=['Metrics', 'Timestamp'],
)

expected = expected.applymap(_format_float)

pandas.testing.assert_frame_equal(expected, result)

def test_pivoted_dimx2_date_str_with_sort_second_metric_desc(self):
result = Pandas(mock_dataset.fields.votes,
pivot=[mock_dataset.fields.political_party],
Expand Down
8 changes: 5 additions & 3 deletions fireant/widgets/pandas.py
@@ -1,14 +1,13 @@
import pandas as pd
from functools import partial
from typing import Iterable

import pandas as pd
from fireant import formats
from fireant.dataset.fields import Field
from fireant.utils import (
alias_selector,
wrap_list,
)

from .base import (
ReferenceItem,
TransformableWidget,
Expand Down Expand Up @@ -163,7 +162,10 @@ def _get_f_display(item):
return format_df

for item in items:
key = item.label
f_display = _get_f_display(item)
format_df[item.label] = format_df[item.label].apply(f_display)
format_df[key] = format_df[key].apply(f_display) \
if isinstance(format_df[key], pd.Series) \
else format_df[key].applymap(f_display)

return format_df

0 comments on commit 61a0251

Please sign in to comment.