コード例 #1
0
ファイル: test_data_array.py プロジェクト: nismod/smif
    def test_multi_dim_order(self):
        spec = Spec(name='test',
                    coords={
                        'lad': ['c', 'a', 'b'],
                        'interval': [4, 2]
                    },
                    dims=['lad', 'interval'],
                    dtype='float')
        data = numpy.array(
            [
                # 4  2
                [1, 2],  # c
                [5, 6],  # a
                [9, 0]  # b
            ],
            dtype='float')
        da = DataArray(spec, data)

        df = pd.DataFrame([
            {
                'test': 6.0,
                'lad': 'a',
                'interval': 2
            },
            {
                'test': 0.0,
                'lad': 'b',
                'interval': 2
            },
            {
                'test': 2.0,
                'lad': 'c',
                'interval': 2
            },
            {
                'test': 5.0,
                'lad': 'a',
                'interval': 4
            },
            {
                'test': 9.0,
                'lad': 'b',
                'interval': 4
            },
            {
                'test': 1.0,
                'lad': 'c',
                'interval': 4
            },
        ]).set_index(['lad', 'interval'])
        da_from_df = DataArray.from_df(spec, df)
        assert da_from_df == da

        da_to_df = da.as_df().sort_index()
        df = df.sort_index()
        pd.testing.assert_frame_equal(da_to_df, df)
コード例 #2
0
ファイル: test_data_array.py プロジェクト: nismod/smif
    def test_scalar(self):
        # should handle zero-dimensional case (numpy array as scalar)
        data = numpy.array(2.0)
        spec = Spec(name='test', dims=[], coords={}, dtype='float')
        da = DataArray(spec, data)
        df = pd.DataFrame([{'test': 2.0}])
        da_from_df = DataArray.from_df(spec, df)
        assert da_from_df == da

        df_from_da = da.as_df()
        pd.testing.assert_frame_equal(df_from_da, df)
コード例 #3
0
ファイル: test_data_array.py プロジェクト: nismod/smif
    def test_df_round_trip_2d(self):
        spec = Spec.from_dict({
            'name': 'two_d',
            'dims': ['a', 'z'],
            'coords': {
                'a': ['q', 'p'],
                'z': ['a', 'c', 'b'],
            },
            'dtype': 'float'
        })
        da = DataArray(spec, numpy.array([
            [5., 6., 7.],
            [8., 9., 0.],
        ]))
        df = pd.DataFrame([
            {
                'z': 'a',
                'a': 'p',
                'two_d': 8.
            },
            {
                'z': 'c',
                'a': 'q',
                'two_d': 6.
            },
            {
                'z': 'a',
                'a': 'q',
                'two_d': 5.
            },
            {
                'z': 'b',
                'a': 'q',
                'two_d': 7.
            },
            {
                'z': 'b',
                'a': 'p',
                'two_d': 0.
            },
            {
                'z': 'c',
                'a': 'p',
                'two_d': 9.
            },
        ])
        df = df.set_index(spec.dims)
        df_from_da = da.as_df()

        da_from_df = DataArray.from_df(spec, df_from_da)
        assert_array_equal(da.data, da_from_df.data)
コード例 #4
0
ファイル: test_data_array.py プロジェクト: nismod/smif
    def test_to_from_df(self):
        df = pd.DataFrame([{
            'test': 3,
            'region': 'oxford',
            'interval': 1
        }]).set_index(['region', 'interval'])

        spec = Spec(name='test',
                    dims=['region', 'interval'],
                    coords={
                        'region': ['oxford'],
                        'interval': [1]
                    },
                    dtype='int64')

        da = DataArray(spec, numpy.array([[3.]], dtype='int64'))
        da_from_df = DataArray.from_df(spec, df)
        assert da_from_df == da

        da_to_df = da.as_df()
        pd.testing.assert_frame_equal(da_to_df, df)
コード例 #5
0
ファイル: test_data_array.py プロジェクト: nismod/smif
    def test_df_round_trip(self):
        spec = Spec.from_dict({
            'name': 'multi_savings',
            'description': 'The savings from various technologies',
            'dims': ['technology_type'],
            'coords': {
                'technology_type':
                ['water_meter', 'electricity_meter', 'other', 'aaa']
            },
            'dtype': 'float',
            'abs_range': (0, 100),
            'exp_range': (3, 10),
            'unit': '%'
        })
        da = DataArray(spec, numpy.array([5., 6., 7., 8.]))
        df = pd.DataFrame([
            {
                'technology_type': 'water_meter',
                'multi_savings': 5.
            },
            {
                'technology_type': 'electricity_meter',
                'multi_savings': 6.
            },
            {
                'technology_type': 'other',
                'multi_savings': 7.
            },
            {
                'technology_type': 'aaa',
                'multi_savings': 8.
            },
        ])
        df = df.set_index(spec.dims)
        df_from_da = da.as_df()

        da_from_df = DataArray.from_df(spec, df_from_da)
        assert_array_equal(da.data, da_from_df.data)