File: test_pandas.py

package info (click to toggle)
python-petl 1.7.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,224 kB
  • sloc: python: 22,617; makefile: 109; xml: 9
file content (51 lines) | stat: -rw-r--r-- 1,421 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import


import pytest

import petl as etl
from petl.test.helpers import ieq
from petl.io.pandas import todataframe, fromdataframe


try:
    import pandas as pd # noqa: F401
except ImportError as e:
    pytest.skip('SKIP pandas tests: %s' % e, allow_module_level=True)
else:

    def test_todataframe():
        tbl = [('foo', 'bar', 'baz'),
               ('apples', 1, 2.5),
               ('oranges', 3, 4.4),
               ('pears', 7, .1)]

        expect = pd.DataFrame.from_records(tbl[1:], columns=tbl[0])
        actual = todataframe(tbl)
        assert expect.equals(actual)

    def test_headerless():
        tbl = []
        expect = pd.DataFrame()
        actual = todataframe(tbl)
        assert expect.equals(actual)

    def test_fromdataframe():
        tbl = [('foo', 'bar', 'baz'),
               ('apples', 1, 2.5),
               ('oranges', 3, 4.4),
               ('pears', 7, .1)]
        df = pd.DataFrame.from_records(tbl[1:], columns=tbl[0])
        ieq(tbl, fromdataframe(df))
        ieq(tbl, fromdataframe(df))

    def test_integration():
        tbl = [('foo', 'bar', 'baz'),
               ('apples', 1, 2.5),
               ('oranges', 3, 4.4),
               ('pears', 7, .1)]
        df = etl.wrap(tbl).todataframe()
        tbl2 = etl.fromdataframe(df)
        ieq(tbl, tbl2)
        ieq(tbl, tbl2)