Esempio n. 1
0
    def testFromDataFrameExecution(self):
        mdf = md.DataFrame({
            'angle': [0, 3, 4],
            'degree': [360, 180, 360]
        },
                           index=['circle', 'triangle', 'rectangle'])
        tensor_result = self.executor.execute_tensor(from_dataframe(mdf))
        tensor_expected = self.executor.execute_tensor(
            mt.tensor([[0, 360], [3, 180], [4, 360]]))
        np.testing.assert_equal(tensor_result, tensor_expected)

        # test up-casting
        mdf2 = md.DataFrame({'a': [0.1, 0.2, 0.3], 'b': [1, 2, 3]})
        tensor_result2 = self.executor.execute_tensor(from_dataframe(mdf2))
        np.testing.assert_equal(tensor_result2[0].dtype, np.dtype('float64'))
        tensor_expected2 = self.executor.execute_tensor(
            mt.tensor([[0.1, 1.0], [0.2, 2.0], [0.3, 3.0]]))
        np.testing.assert_equal(tensor_result2, tensor_expected2)

        raw = [[0.1, 0.2, 0.4], [0.4, 0.7, 0.3]]
        mdf3 = md.DataFrame(raw, columns=list('abc'), chunk_size=2)
        tensor_result3 = self.executor.execute_tensor(from_dataframe(mdf3),
                                                      concat=True)[0]
        np.testing.assert_array_equal(tensor_result3, np.asarray(raw))
        self.assertTrue(tensor_result3.flags['F_CONTIGUOUS'])
        self.assertFalse(tensor_result3.flags['C_CONTIGUOUS'])

        # test from series
        series = md.Series([1, 2, 3])
        tensor_result = series.to_tensor().execute()
        np.testing.assert_array_equal(tensor_result, np.array([1, 2, 3]))

        series = md.Series(range(10), chunk_size=3)
        tensor_result = series.to_tensor().execute()
        np.testing.assert_array_equal(tensor_result, np.arange(10))
Esempio n. 2
0
def test_from_dataframe_execution(setup):
    mdf = md.DataFrame({'angle': [0, 3, 4], 'degree': [360, 180, 360]},
                       index=['circle', 'triangle', 'rectangle'])
    tensor_result = from_dataframe(mdf).execute().fetch()
    tensor_expected = mt.tensor([[0, 360], [3, 180], [4, 360]]).execute().fetch()
    np.testing.assert_equal(tensor_result, tensor_expected)

    # test up-casting
    mdf2 = md.DataFrame({'a': [0.1, 0.2, 0.3], 'b': [1, 2, 3]})
    tensor_result2 = from_dataframe(mdf2).execute().fetch()
    np.testing.assert_equal(tensor_result2[0].dtype, np.dtype('float64'))
    tensor_expected2 = mt.tensor([[0.1, 1.0], [0.2, 2.0], [0.3, 3.0]]).execute().fetch()
    np.testing.assert_equal(tensor_result2, tensor_expected2)

    raw = [[0.1, 0.2, 0.4], [0.4, 0.7, 0.3]]
    mdf3 = md.DataFrame(raw, columns=list('abc'), chunk_size=2)
    tensor_result3 = from_dataframe(mdf3).execute().fetch()
    np.testing.assert_array_equal(tensor_result3, np.asarray(raw))
    assert tensor_result3.flags['F_CONTIGUOUS'] is True
    assert tensor_result3.flags['C_CONTIGUOUS'] is False

    # test from series
    series = md.Series([1, 2, 3])
    tensor_result = series.to_tensor().execute().fetch()
    np.testing.assert_array_equal(tensor_result, np.array([1, 2, 3]))

    series = md.Series(range(10), chunk_size=3)
    tensor_result = series.to_tensor().execute().fetch()
    np.testing.assert_array_equal(tensor_result, np.arange(10))

    # test from index
    index = md.Index(pd.MultiIndex.from_tuples([(0, 1), (2, 3), (4, 5)]))
    tensor_result = index.to_tensor(extract_multi_index=True).execute().fetch()
    np.testing.assert_array_equal(tensor_result, np.arange(6).reshape((3, 2)))

    index = md.Index(pd.MultiIndex.from_tuples([(0, 1), (2, 3), (4, 5)]))
    tensor_result = index.to_tensor(extract_multi_index=False).execute().fetch()
    np.testing.assert_array_equal(tensor_result,
                                  pd.MultiIndex.from_tuples([(0, 1), (2, 3), (4, 5)]).to_series())