예제 #1
0
    def testToFrameOrSeries(self):
        raw = pd.Series(np.random.rand(10), name='col')
        series = Series(raw)

        r = series.to_frame()
        result = self.executor.execute_dataframe(r, concat=True)[0]
        pd.testing.assert_frame_equal(raw.to_frame(), result)

        r = series.to_frame(name='new_name')
        result = self.executor.execute_dataframe(r, concat=True)[0]
        pd.testing.assert_frame_equal(raw.to_frame(name='new_name'), result)

        raw = pd.Index(np.random.rand(10), name='col')
        index = Index(raw)

        r = index.to_frame()
        result = self.executor.execute_dataframe(r, concat=True)[0]
        pd.testing.assert_frame_equal(raw.to_frame(), result)

        r = index.to_frame(index=False)
        result = self.executor.execute_dataframe(r, concat=True)[0]
        pd.testing.assert_frame_equal(raw.to_frame(index=False), result)

        r = index.to_frame(name='new_name')
        result = self.executor.execute_dataframe(r, concat=True)[0]
        pd.testing.assert_frame_equal(raw.to_frame(name='new_name'), result)

        r = index.to_series()
        result = self.executor.execute_dataframe(r, concat=True)[0]
        pd.testing.assert_series_equal(raw.to_series(), result)

        r = index.to_series(index=pd.RangeIndex(0, 10))
        result = self.executor.execute_dataframe(r, concat=True)[0]
        pd.testing.assert_series_equal(
            raw.to_series(index=pd.RangeIndex(0, 10)), result)

        r = index.to_series(name='new_name')
        result = self.executor.execute_dataframe(r, concat=True)[0]
        pd.testing.assert_series_equal(raw.to_series(name='new_name'), result)

        raw = pd.MultiIndex.from_tuples([('A', 'E'), ('B', 'F'), ('C', 'G')])
        index = Index(raw, tupleize_cols=True)

        r = index.to_frame()
        result = self.executor.execute_dataframe(r, concat=True)[0]
        pd.testing.assert_frame_equal(raw.to_frame(), result)

        with self.assertRaises(TypeError):
            index.to_frame(name='XY')

        with self.assertRaises(ValueError):
            index.to_frame(name=['X', 'Y', 'Z'])

        r = index.to_frame(name=['X', 'Y'])
        result = self.executor.execute_dataframe(r, concat=True)[0]
        pd.testing.assert_frame_equal(raw.to_frame(name=['X', 'Y']), result)

        r = index.to_series(name='new_name')
        result = self.executor.execute_dataframe(r, concat=True)[0]
        pd.testing.assert_series_equal(raw.to_series(name='new_name'), result)
예제 #2
0
def test_to_frame_or_series(setup):
    raw = pd.Series(np.random.rand(10), name='col')
    series = Series(raw)

    r = series.to_frame()
    result = r.execute().fetch()
    pd.testing.assert_frame_equal(raw.to_frame(), result)

    r = series.to_frame(name='new_name')
    result = r.execute().fetch()
    pd.testing.assert_frame_equal(raw.to_frame(name='new_name'), result)

    series = series[series > 0.1]
    r = series.to_frame(name='new_name')
    result = r.execute().fetch()
    pd.testing.assert_frame_equal(raw[raw > 0.1].to_frame(name='new_name'),
                                  result)

    raw = pd.Index(np.random.rand(10), name='col')
    index = Index(raw)

    r = index.to_frame()
    result = r.execute().fetch()
    pd.testing.assert_frame_equal(raw.to_frame(), result)

    r = index.to_frame(index=False)
    result = r.execute().fetch()
    pd.testing.assert_frame_equal(raw.to_frame(index=False), result)

    r = index.to_frame(name='new_name')
    result = r.execute().fetch()
    pd.testing.assert_frame_equal(raw.to_frame(name='new_name'), result)

    r = index.to_series()
    result = r.execute().fetch()
    pd.testing.assert_series_equal(raw.to_series(), result)

    r = index.to_series(index=pd.RangeIndex(0, 10))
    result = r.execute().fetch()
    pd.testing.assert_series_equal(raw.to_series(index=pd.RangeIndex(0, 10)),
                                   result)

    r = index.to_series(name='new_name')
    result = r.execute().fetch()
    pd.testing.assert_series_equal(raw.to_series(name='new_name'), result)

    raw = pd.MultiIndex.from_tuples([('A', 'E'), ('B', 'F'), ('C', 'G')])
    index = Index(raw, tupleize_cols=True)

    r = index.to_frame()
    result = r.execute().fetch()
    pd.testing.assert_frame_equal(raw.to_frame(), result)

    with pytest.raises(TypeError):
        index.to_frame(name='XY')

    with pytest.raises(ValueError):
        index.to_frame(name=['X', 'Y', 'Z'])

    r = index.to_frame(name=['X', 'Y'])
    result = r.execute().fetch()
    pd.testing.assert_frame_equal(raw.to_frame(name=['X', 'Y']), result)

    r = index.to_series(name='new_name')
    result = r.execute().fetch()
    pd.testing.assert_series_equal(raw.to_series(name='new_name'), result)