def test_contribution(self): df = DataFrame({ DTTM_ALIAS: [ datetime(2020, 7, 16, 14, 49), datetime(2020, 7, 16, 14, 50), ], "a": [1, 3], "b": [1, 9], }) # cell contribution across row row_df = proc.contribution(df, PostProcessingContributionOrientation.ROW) self.assertListEqual(df.columns.tolist(), [DTTM_ALIAS, "a", "b"]) self.assertListEqual(series_to_list(row_df["a"]), [0.5, 0.25]) self.assertListEqual(series_to_list(row_df["b"]), [0.5, 0.75]) # cell contribution across column without temporal column df.pop(DTTM_ALIAS) column_df = proc.contribution( df, PostProcessingContributionOrientation.COLUMN) self.assertListEqual(df.columns.tolist(), ["a", "b"]) self.assertListEqual(series_to_list(column_df["a"]), [0.25, 0.75]) self.assertListEqual(series_to_list(column_df["b"]), [0.1, 0.9])
def test_contribution(): df = DataFrame( { DTTM_ALIAS: [ datetime(2020, 7, 16, 14, 49), datetime(2020, 7, 16, 14, 50), datetime(2020, 7, 16, 14, 51), ], "a": [1, 3, nan], "b": [1, 9, nan], "c": [nan, nan, nan], } ) with pytest.raises(QueryObjectValidationError, match="not numeric"): contribution(df, columns=[DTTM_ALIAS]) with pytest.raises(QueryObjectValidationError, match="same length"): contribution(df, columns=["a"], rename_columns=["aa", "bb"]) # cell contribution across row processed_df = contribution( df, orientation=PostProcessingContributionOrientation.ROW, ) assert processed_df.columns.tolist() == [DTTM_ALIAS, "a", "b", "c"] assert_array_equal(processed_df["a"].tolist(), [0.5, 0.25, nan]) assert_array_equal(processed_df["b"].tolist(), [0.5, 0.75, nan]) assert_array_equal(processed_df["c"].tolist(), [0, 0, nan]) # cell contribution across column without temporal column df.pop(DTTM_ALIAS) processed_df = contribution( df, orientation=PostProcessingContributionOrientation.COLUMN ) assert processed_df.columns.tolist() == ["a", "b", "c"] assert_array_equal(processed_df["a"].tolist(), [0.25, 0.75, 0]) assert_array_equal(processed_df["b"].tolist(), [0.1, 0.9, 0]) assert_array_equal(processed_df["c"].tolist(), [nan, nan, nan]) # contribution only on selected columns processed_df = contribution( df, orientation=PostProcessingContributionOrientation.COLUMN, columns=["a"], rename_columns=["pct_a"], ) assert processed_df.columns.tolist() == ["a", "b", "c", "pct_a"] assert_array_equal(processed_df["a"].tolist(), [1, 3, nan]) assert_array_equal(processed_df["b"].tolist(), [1, 9, nan]) assert_array_equal(processed_df["c"].tolist(), [nan, nan, nan]) assert processed_df["pct_a"].tolist() == [0.25, 0.75, 0]
def test_contribution(self): df = DataFrame({ DTTM_ALIAS: [ datetime(2020, 7, 16, 14, 49), datetime(2020, 7, 16, 14, 50), ], "a": [1, 3], "b": [1, 9], }) with pytest.raises(QueryObjectValidationError, match="not numeric"): proc.contribution(df, columns=[DTTM_ALIAS]) with pytest.raises(QueryObjectValidationError, match="same length"): proc.contribution(df, columns=["a"], rename_columns=["aa", "bb"]) # cell contribution across row processed_df = proc.contribution( df, orientation=PostProcessingContributionOrientation.ROW, ) self.assertListEqual(processed_df.columns.tolist(), [DTTM_ALIAS, "a", "b"]) self.assertListEqual(processed_df["a"].tolist(), [0.5, 0.25]) self.assertListEqual(processed_df["b"].tolist(), [0.5, 0.75]) # cell contribution across column without temporal column df.pop(DTTM_ALIAS) processed_df = proc.contribution( df, orientation=PostProcessingContributionOrientation.COLUMN) self.assertListEqual(processed_df.columns.tolist(), ["a", "b"]) self.assertListEqual(processed_df["a"].tolist(), [0.25, 0.75]) self.assertListEqual(processed_df["b"].tolist(), [0.1, 0.9]) # contribution only on selected columns processed_df = proc.contribution( df, orientation=PostProcessingContributionOrientation.COLUMN, columns=["a"], rename_columns=["pct_a"], ) self.assertListEqual(processed_df.columns.tolist(), ["a", "b", "pct_a"]) self.assertListEqual(processed_df["a"].tolist(), [1, 3]) self.assertListEqual(processed_df["b"].tolist(), [1, 9]) self.assertListEqual(processed_df["pct_a"].tolist(), [0.25, 0.75])