Beispiel #1
0
    def test_multi_dimx2_date_str(self):
        query_dimensions = [mock_dataset.fields.timestamp, mock_dataset.fields.political_party]
        result = CSV(mock_dataset.fields.wins) \
            .transform(dimx2_date_str_df, mock_dataset, query_dimensions, [])

        expected = dimx2_date_str_df.copy()[[f('wins')]]
        expected.index.names = ['Timestamp', 'Party']
        expected.columns = ['Wins']

        self.assertEqual(expected.to_csv(**csv_options), result)
Beispiel #2
0
    def test_dimx2_date_str(self):
        dimensions = [mock_dataset.fields.timestamp, mock_dataset.fields.political_party]
        result = Pandas(mock_dataset.fields.wins).transform(dimx2_date_str_df, dimensions, [])

        expected = dimx2_date_str_df.copy()[[f('wins')]]
        expected.index.names = ['Timestamp', 'Party']
        expected.columns = ['Wins']
        expected.columns.name = 'Metrics'
        expected = expected.applymap(format_float)

        pandas.testing.assert_frame_equal(expected, result)
Beispiel #3
0
    def test_pivoted_multi_dimx2_date_str(self):
        result = CSV(mock_dataset.fields.wins, pivot=[mock_dataset.fields.political_party]) \
            .transform(dimx2_date_str_df,
                       [mock_dataset.fields.timestamp, mock_dataset.fields.political_party], [])

        expected = dimx2_date_str_df.copy()[[f('wins')]]
        expected = expected.unstack(level=[1])
        expected.index.names = ['Timestamp']
        expected.columns = ['Democrat', 'Independent', 'Republican']
        expected = expected.applymap(format_float_raw)

        self.assertEqual(expected.to_csv(**csv_options), result)
Beispiel #4
0
    def test_pivoted_dimx2_date_str(self):
        result = Pandas(mock_dataset.fields.wins, pivot=[mock_dataset.fields.political_party]) \
            .transform(dimx2_date_str_df, mock_dataset,
                       [mock_dataset.fields.timestamp, mock_dataset.fields.political_party], [])

        expected = dimx2_date_str_df.copy()[[f('wins')]]
        expected = expected.unstack(level=[1])
        expected.index.names = ['Timestamp']
        expected.columns = ['Democrat', 'Independent', 'Republican']
        expected.columns.names = ['Party']
        expected = expected.applymap(_format_float)

        pandas.testing.assert_frame_equal(expected, result)
Beispiel #5
0
    def test_hidden_dimx2_date_str(self):
        dimensions = [
            mock_dataset.fields.timestamp, mock_dataset.fields.political_party
        ]
        result = CSV(mock_dataset.fields.wins, hide=[mock_dataset.fields.political_party]) \
            .transform(dimx2_date_str_df, dimensions, [])

        expected = dimx2_date_str_df.copy()[[f('wins')]]
        expected.reset_index('$political_party', inplace=True, drop=True)
        expected.index.names = ['Timestamp']
        expected.columns = ['Wins']
        expected.columns.name = 'Metrics'

        self.assertEqual(expected.to_csv(**csv_options), result)
Beispiel #6
0
    def test_pivoted_dimx2_date_str_sort_index_level_1_desc(self):
        result = Pandas(mock_dataset.fields.wins, sort=[1], ascending=[False]).transform(
            dimx2_date_str_df, [mock_dataset.fields.timestamp, mock_dataset.fields.political_party], []
        )

        expected = dimx2_date_str_df.copy()[[f('wins')]]
        expected.index.names = ['Timestamp', 'Party']
        expected.columns = ['Wins']
        expected.columns.name = 'Metrics'

        expected = expected.reset_index().sort_values(['Party'], ascending=[False]).set_index(['Timestamp', 'Party'])
        expected = expected.applymap(format_float)

        pandas.testing.assert_frame_equal(expected, result)
Beispiel #7
0
    def test_fetch_only_dimx2_date_str(self):
        dimensions = [mock_dataset.fields.timestamp, mock_dataset.fields.political_party]
        dimensions[1].fetch_only = True
        result = Pandas(mock_dataset.fields.wins).transform(dimx2_date_str_df, dimensions, [])
        dimensions[1].fetch_only = False

        expected = dimx2_date_str_df.copy()[[f('wins')]]
        expected.reset_index('$political_party', inplace=True, drop=True)
        expected.index.names = ['Timestamp']
        expected.columns = ['Wins']
        expected.columns.name = 'Metrics'
        expected = expected.applymap(format_float)

        pandas.testing.assert_frame_equal(expected, result)
Beispiel #8
0
    def test_use_pandas_default_for_ascending_when_arg_empty_list(self):
        result = Pandas(
            mock_dataset.fields.votes, pivot=[mock_dataset.fields.political_party], sort=[0, 2], ascending=[]
        ).transform(dimx2_date_str_df, [mock_dataset.fields.timestamp, mock_dataset.fields.political_party], [])

        expected = dimx2_date_str_df.copy()[[f('votes')]]
        expected = expected.unstack(level=[1])
        expected.index.names = ['Timestamp']
        expected.columns = ['Democrat', 'Independent', 'Republican']
        expected.columns.names = ['Party']

        expected = expected.reset_index().sort_values(['Timestamp', 'Democrat'], ascending=None).set_index('Timestamp')
        expected = expected.applymap(format_float)

        pandas.testing.assert_frame_equal(expected, result)
Beispiel #9
0
    def test_pivoted_dimx2_date_str_with_sort_second_metric_desc(self):
        result = Pandas(
            mock_dataset.fields.votes, pivot=[mock_dataset.fields.political_party], sort=1, ascending=False
        ).transform(dimx2_date_str_df, [mock_dataset.fields.timestamp, mock_dataset.fields.political_party], [])

        expected = dimx2_date_str_df.copy()[[f('votes')]]
        expected = expected.unstack(level=[1])
        expected.index.names = ['Timestamp']
        expected.columns = ['Democrat', 'Independent', 'Republican']
        expected.columns.names = ['Party']

        expected = expected.reset_index().sort_values(['Democrat'], ascending=False).set_index('Timestamp')
        expected = expected.applymap(format_float)

        pandas.testing.assert_frame_equal(expected, result)
Beispiel #10
0
    def test_hide_data_frame_indexes_hides_found_aliases(self):
        widget = Widget()

        base_df = dimx2_date_str_df.copy()[[alias_selector('wins'), alias_selector('votes')]]
        result = base_df.copy()
        widget.hide_data_frame_indexes(
            result,
            [
                alias_selector(mock_dataset.fields.political_party.alias),
                alias_selector(mock_dataset.fields.votes.alias),
                alias_selector('unknown'),
            ],
        )

        expected = base_df.copy()
        expected.reset_index('$political_party', inplace=True, drop=True)
        del expected['$votes']

        pd.testing.assert_frame_equal(expected, result)
Beispiel #11
0
    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.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)