Skip to content

Commit adfce67

Browse files
committed
PEP8 whitespace fix
1 parent 7e6a59c commit adfce67

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

pandas/tests/io/test_ods.py

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
from collections import OrderedDict
2+
import os
3+
from numpy import nan
4+
import pandas
5+
import pandas.util._test_decorators as td
6+
import pandas.util.testing as tm
7+
from pandas import DataFrame, Timestamp, Timedelta
8+
from pandas.io.opendocument import ODFReader
9+
import pytest
10+
11+
12+
@td.skip_if_no('odf')
13+
class TestOpenDocument(object):
14+
@pytest.fixture(autouse=True)
15+
def setup_method(self, datapath):
16+
self.dirpath = datapath("io", "data")
17+
18+
def get_opendocument(self, filename, *args, **kwargs):
19+
"""
20+
Return ODFReader class containing tables from parsed OpenDocument file
21+
22+
Parameters
23+
----------
24+
filename : str
25+
26+
Returns
27+
-------
28+
29+
document : ODFReader object
30+
"""
31+
pth = os.path.join(self.dirpath, filename)
32+
document = ODFReader(pth)
33+
return document
34+
35+
def get_opendocumentdf(self, filename, *args, **kwargs):
36+
"""
37+
Return DataFrame from named sheet in a parsed OpenDocument file
38+
39+
Parameters
40+
----------
41+
filename : str
42+
File base name
43+
44+
Returns
45+
-------
46+
47+
df : DataFrame
48+
"""
49+
document = self.get_opendocument(filename)
50+
return document.parse(*args, **kwargs)
51+
52+
def test_read_types(self):
53+
"""Make sure we read ODF data types correctly
54+
"""
55+
book = self.get_opendocument('datatypes.ods')
56+
assert len(book.sheet_names) == 1
57+
assert book.sheet_names == ['Sheet1']
58+
sheet = book.parse('Sheet1', header=None)
59+
60+
expected = DataFrame(
61+
[[1.0],
62+
[1.25],
63+
['a'],
64+
[Timestamp(2003, 1, 2)],
65+
[False],
66+
[0.35],
67+
[Timedelta(hours=3, minutes=45),
68+
Timedelta(hours=17, minutes=53),
69+
Timedelta(hours=14, minutes=8)],
70+
# though what should the value of a hyperlink be?
71+
['UBERON:0002101']])
72+
tm.assert_equal(sheet, expected)
73+
74+
def test_read_lower_diagonal(self):
75+
"""TextParser failed when given an irregular list of lists
76+
77+
Make sure we can parse:
78+
1
79+
2 3
80+
4 5 6
81+
7 8 9 10
82+
"""
83+
sheet = self.get_opendocumentdf(
84+
'lowerdiagonal.ods', 'Sheet1',
85+
index_col=None, header=None)
86+
87+
assert sheet.shape == (4, 4)
88+
89+
def test_read_headers(self):
90+
"""Do we read headers correctly?
91+
"""
92+
sheet = self.get_opendocumentdf(
93+
'headers.ods', 'Sheet1', index_col=0)
94+
95+
expected = DataFrame.from_dict(OrderedDict([
96+
("Header", ["Row 1", "Row 2"]),
97+
("Column 1", [1.0, 2.0]),
98+
("Column 2", [3.0, 4.0]),
99+
# Empty Column
100+
("Column 4", [7.0, 8.0]),
101+
# Empty Column 2
102+
("Column 6", [11.0, 12.0])]))
103+
expected.set_index("Header", inplace=True)
104+
columns = ["Column 1", "Column 2", "Column 4", "Column 6"]
105+
tm.assert_equal(sheet[columns], expected)
106+
empties = [None, 'None.1']
107+
for name in empties:
108+
for value in sheet[name]:
109+
assert pandas.isnull(value)
110+
111+
def test_read_writer_table(self):
112+
"""ODF reuses the same table tags in Writer and Presentation files
113+
114+
Test reading a table out of a text document
115+
"""
116+
table = self.get_opendocumentdf(
117+
'writertable.odt', 'Table1', index_col=0)
118+
119+
assert table.shape == (3, 3)
120+
expected = DataFrame.from_dict(OrderedDict([
121+
("Header", ["Row 1", "Row 2", "Row 3"]),
122+
("Column 1", [1.0, 2.0, 3.0]),
123+
("Unnamed: 2", [nan, nan, nan]),
124+
("Column 3", [7.0, 8.0, 9.0])]))
125+
expected.set_index("Header", inplace=True)
126+
columns = ["Column 1", "Column 3"]
127+
tm.assert_equal(table[columns], expected[columns])
128+
129+
# make sure pandas gives a name to the unnamed column
130+
for i in range(3):
131+
assert pandas.isnull(table["Unnamed: 2"][i])
132+
133+
def test_blank_row_repeat(self):
134+
table = self.get_opendocumentdf(
135+
'blank-row-repeat.ods', 'Biosamples')
136+
137+
assert table.shape == (8, 9)
138+
assert table['biosample_accession'][7] == 9.0
139+
140+
def test_runlengthencoding(self):
141+
"""Calc will use repeat when adjacent columns have the same value.
142+
"""
143+
sheet = self.get_opendocumentdf(
144+
'runlengthencoding.ods', 'Sheet1', header=None)
145+
assert sheet.shape == (5, 3)
146+
# check by column, not by row.
147+
assert list(sheet[0]) == [1.0, 1.0, 2.0, 2.0, 2.0]
148+
assert list(sheet[1]) == [1.0, 2.0, 2.0, 2.0, 2.0]
149+
assert list(sheet[2]) == [1.0, 2.0, 2.0, 2.0, 2.0]

0 commit comments

Comments
 (0)