Beispiel #1
0
def test_index_inferred_type():
    assert Index(pd.Index([1, 2, 3, 4])).inferred_type == 'integer'
    assert Index(pd.Index([1, 2, 3,
                           4]).astype('uint32')).inferred_type == 'integer'
    assert Index(pd.Index([1.2, 2.3, 4.5])).inferred_type == 'floating'
    assert Index(pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)
                                               ])).inferred_type == 'interval'
    assert Index(pd.MultiIndex.from_tuples([('a', 1), ('b', 2)
                                            ])).inferred_type == 'mixed'
Beispiel #2
0
 def testIndexInferredType(self):
     self.assertEqual(Index(pd.Index([1, 2, 3, 4])).inferred_type, 'integer')
     self.assertEqual(Index(pd.Index([1, 2, 3, 4]).astype('uint32')).inferred_type, 'integer')
     self.assertEqual(Index(pd.Index([1.2, 2.3, 4.5])).inferred_type, 'floating')
     self.assertEqual(
         Index(pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)])).inferred_type,
         'interval'
     )
     self.assertEqual(
         Index(pd.MultiIndex.from_tuples([('a', 1), ('b', 2)])).inferred_type,
         'mixed'
     )
Beispiel #3
0
    def testKeyValue(self):
        raw = pd.DataFrame(np.random.rand(4, 3), columns=list('ABC'))
        df = DataFrame(raw)

        result = self.executor.execute_dataframe(df.values, concat=True)[0]
        np.testing.assert_array_equal(result, raw.values)

        result = self.executor.execute_dataframe(df.keys(), concat=True)[0]
        pd.testing.assert_index_equal(result, raw.keys())

        raw = pd.Series(np.random.rand(10))
        s = Series(raw)

        result = self.executor.execute_dataframe(s.values, concat=True)[0]
        np.testing.assert_array_equal(result, raw.values)

        result = self.executor.execute_dataframe(s.keys(), concat=True)[0]
        pd.testing.assert_index_equal(result, raw.keys())

        raw = pd.Index(np.random.rand(10))
        idx = Index(raw)

        result = self.executor.execute_dataframe(idx.values, concat=True)[0]
        np.testing.assert_array_equal(result, raw.values)
Beispiel #4
0
def test_key_value(setup):
    raw = pd.DataFrame(np.random.rand(4, 3), columns=list('ABC'))
    df = DataFrame(raw)

    result = df.values.execute().fetch()
    np.testing.assert_array_equal(result, raw.values)

    result = df.keys().execute().fetch()
    pd.testing.assert_index_equal(result, raw.keys())

    raw = pd.Series(np.random.rand(10))
    s = Series(raw)

    result = s.values.execute().fetch()
    np.testing.assert_array_equal(result, raw.values)

    result = s.keys().execute().fetch()
    pd.testing.assert_index_equal(result, raw.keys())

    raw = pd.Index(np.random.rand(10))
    idx = Index(raw)

    result = idx.values.execute().fetch()
    np.testing.assert_array_equal(result, raw.values)
Beispiel #5
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)

        series = series[series > 0.1]
        r = series.to_frame(name='new_name')
        result = self.executor.execute_dataframes([r])[0]
        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 = 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)
Beispiel #6
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)