def test_apply_to_two_dims_over_second(self): share = Share(mock_dataset.fields.votes, over=mock_dataset.fields.political_party) result = share.apply(dimx2_date_str_totals_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series( [ 49.79, 7.07, 43.12, 100.0, 49.78, 50.21, 100.0, 48.83, 51.16, 100.0, 55.42, 44.57, 100.0, 60.39, 39.60, 100.0, 26.60, 73.39, 100.0, ], name=f_metric_key, index=dimx2_date_str_totals_df.index, ) pandas.testing.assert_series_equal(expected, result, rtol=0.5e-3)
def test_apply_to_zero_dims(self): share = Share(mock_dataset.fields.votes) result = share.apply(dimx0_metricx1_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series([100.], name=f_metric_key) pandas.testing.assert_series_equal(expected, result)
def test_apply_to_zero_dims(self): share = Share(slicer.metrics.votes) result = share.apply(single_metric_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) expected = pd.Series([100.], name=f_metric_key) pandas.testing.assert_series_equal(expected, result)
def test_apply_to_two_dims_over_first(self): share = Share(mock_dataset.fields.votes, over=mock_dataset.fields.timestamp) result = share.apply(dimx2_date_str_totalsx2_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) metric_series = dimx2_date_str_totalsx2_df[f_metric_key] expected = 100 * metric_series / metric_series.iloc[-1] pandas.testing.assert_series_equal(expected, result, rtol=0.5e-3)
def test_apply_to_one_dim_over_none(self): share = Share(mock_dataset.fields.votes) result = share.apply(dimx1_str_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series([100.] * 3, name=f_metric_key, index=dimx1_str_df.index) pandas.testing.assert_series_equal(expected, result, rtol=0.5e-3)
def test_apply_to_one_dim_over_first(self): share = Share(mock_dataset.fields.votes, over=mock_dataset.fields.political_party) result = share.apply(dimx1_str_totals_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series([48.8487, 0.9638, 50.1873, 100.0], name=f_metric_key, index=dimx1_str_totals_df.index) pandas.testing.assert_series_equal(expected, result, rtol=0.5e-3)
def test_apply_to_two_dims_over_first(self): share = Share(slicer.metrics.votes, over=slicer.dimensions.timestamp) result = share.apply(cont_uni_dim_all_totals_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) metric_series = cont_uni_dim_all_totals_df[f_metric_key] expected = 100 * metric_series / metric_series.iloc[-1] pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_apply_to_two_dims_over_none(self): share = Share(mock_dataset.fields.votes) result = share.apply(dimx2_date_str_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series([100.] * 13, name=f_metric_key, index=dimx2_date_str_df.index) pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_apply_to_one_dim_over_none(self): share = Share(slicer.metrics.votes) result = share.apply(cat_dim_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) expected = pd.Series([100.] * 3, name=f_metric_key, index=cat_dim_df.index) pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_apply_to_one_dim_over_first(self): share = Share(slicer.metrics.votes, over=slicer.dimensions.political_party) result = share.apply(cat_dim_totals_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) expected = pd.Series([48.849, 0.964, 50.187, 100.0], name=f_metric_key, index=cat_dim_totals_df.index) pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_apply_to_two_dims_over_second_with_one_row_per_group(self): raw_df = dimx2_date_str_totals_df.iloc[[0, 3, 4, 6]] share = Share(mock_dataset.fields.votes, over=mock_dataset.fields.political_party) result = share.apply(raw_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series([49.79, 100.0, 49.78, 100.0], name=f_metric_key, index=raw_df.index) pandas.testing.assert_series_equal(expected, result, rtol=0.5e-3)
def test_apply_to_two_dims_over_second_with_one_row_per_group(self): raw_df = cont_uni_dim_totals_df.iloc[[0, 2, 3, 5]] share = Share(slicer.metrics.votes, over=slicer.dimensions.state) result = share.apply(raw_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) expected = pd.Series([36.624, 100., 37.411, 100.], name=f_metric_key, index=raw_df.index) pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_apply_to_two_dims_over_second(self): share = Share(slicer.metrics.votes, over=slicer.dimensions.state) result = share.apply(cont_uni_dim_totals_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) expected = pd.Series([ 36.624, 63.376, 100., 37.411, 62.589, 100., 37.521, 62.479, 100., 37.606, 62.394, 100., 38.294, 61.706, 100., 27.705, 72.295, 100. ], name=f_metric_key, index=cont_uni_dim_totals_df.index) pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_share_for_references_with_delta(self): dataset = MagicMock() dataset.table._table_name = "table" value_field = Field("value", None) over_field = Field("dim-over", None) share = Share(value_field, over_field) reference = Reference(value_field, WeekOverWeek, delta=True, delta_percent=False) df = pd.DataFrame.from_dict({ "$value_wow": [10, 15, 20, 5, 50], "$value": [12, 16, 14, 8, 50], "$share(value,dim-over)": [24, 32, 28, 16, 100], "$dim-over": ["A", "B", "C", "D", "~~totals"] }).set_index('$dim-over') result = share.apply(df, reference) np.testing.assert_array_equal(([4, 2, -12, 6, 0]), result.values)