Debian Codegraph @

petl/test/io/test_pandas.py

raw
1 # -*- coding: utf-8 -*-
2 from __future__ import division, print_function, absolute_import
3
4
5 import pytest
6
7 import petl as etl
8 from petl.test.helpers import ieq
9 from petl.io.pandas import todataframe, fromdataframe
10
11
12 try:
13 import pandas as pd # noqa: F401
14 except ImportError as e:
15 pytest.skip('SKIP pandas tests: %s' % e, allow_module_level=True)
16 else:
17
18 def test_todataframe():
19 tbl = [('foo', 'bar', 'baz'),
20 ('apples', 1, 2.5),
21 ('oranges', 3, 4.4),
22 ('pears', 7, .1)]
23
24 expect = pd.DataFrame.from_records(tbl[1:], columns=tbl[0])
25 actual = todataframe(tbl)
26 assert expect.equals(actual)
27
28 def test_headerless():
29 tbl = []
30 expect = pd.DataFrame()
31 actual = todataframe(tbl)
32 assert expect.equals(actual)
33
34 def test_fromdataframe():
35 tbl = [('foo', 'bar', 'baz'),
36 ('apples', 1, 2.5),
37 ('oranges', 3, 4.4),
38 ('pears', 7, .1)]
39 df = pd.DataFrame.from_records(tbl[1:], columns=tbl[0])
40 ieq(tbl, fromdataframe(df))
41 ieq(tbl, fromdataframe(df))
42
43 def test_integration():
44 tbl = [('foo', 'bar', 'baz'),
45 ('apples', 1, 2.5),
46 ('oranges', 3, 4.4),
47 ('pears', 7, .1)]
48 df = etl.wrap(tbl).todataframe()
49 tbl2 = etl.fromdataframe(df)
50 ieq(tbl, tbl2)
51 ieq(tbl, tbl2)