/usr/lib/python2.7/dist-packages/rpy2/robjects/tests/testDataFrame.py is in python-rpy2 2.4.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | import unittest
import rpy2.robjects as robjects
rinterface = robjects.rinterface
import rpy2.rlike.container as rlc
import array
import csv, tempfile
class DataFrameTestCase(unittest.TestCase):
def testNewFromTaggedList(self):
letters = robjects.r.letters
numbers = robjects.r('1:26')
df = robjects.DataFrame(rlc.TaggedList((letters, numbers),
tags = ('letters', 'numbers')))
self.assertEqual("data.frame", df.rclass[0])
def testNewFromRObject(self):
numbers = robjects.r('1:5')
self.assertRaises(ValueError, robjects.DataFrame, numbers)
rfunc = robjects.r('sum')
self.assertRaises(ValueError, robjects.DataFrame, rfunc)
rdataf = robjects.r('data.frame(a=1:2, b=c("a", "b"))')
dataf = robjects.DataFrame(rdataf)
def testNewFromOrdDict(self):
od = rlc.OrdDict(c=(('a', robjects.IntVector((1,2))),
('b', robjects.StrVector(('c', 'd')))
))
dataf = robjects.DataFrame(od)
self.assertEqual(1, dataf.rx2('a')[0])
def testDim(self):
letters = robjects.r.letters
numbers = robjects.r('1:26')
df = robjects.DataFrame(rlc.TaggedList((letters, numbers),
tags = ('letters', 'numbers')))
self.assertEqual(26, df.nrow)
self.assertEqual(2, df.ncol)
def testFrom_csvfile(self):
column_names = ('letter', 'value')
data = (column_names,
('a', 1),
('b', 2),
('c', 3))
fh = tempfile.NamedTemporaryFile(mode = "w", delete = False)
csv_w = csv.writer(fh)
csv_w.writerows(data)
fh.close()
dataf = robjects.DataFrame.from_csvfile(fh.name)
self.assertEqual(column_names, tuple(dataf.names))
self.assertEqual(3, dataf.nrow)
self.assertEqual(2, dataf.ncol)
def testTo_csvfile(self):
fh = tempfile.NamedTemporaryFile(mode = "w", delete = False)
fh.close()
d = {'letter': robjects.StrVector('abc'),
'value' : robjects.IntVector((1, 2, 3))}
dataf = robjects.DataFrame(d)
dataf.to_csvfile(fh.name)
dataf = robjects.DataFrame.from_csvfile(fh.name)
self.assertEqual(3, dataf.nrow)
self.assertEqual(2, dataf.ncol)
def testIter_col(self):
dataf = robjects.r('data.frame(a=1:2, b=I(c("a", "b")))')
col_types = [x.typeof for x in dataf.iter_column()]
self.assertEqual(rinterface.INTSXP, col_types[0])
self.assertEqual(rinterface.STRSXP, col_types[1])
def testIter_row(self):
dataf = robjects.r('data.frame(a=1:2, b=I(c("a", "b")))')
rows = [x for x in dataf.iter_row()]
self.assertEqual(1, rows[0][0][0])
self.assertEqual("b", rows[1][1][0])
def testColnames(self):
dataf = robjects.r('data.frame(a=1:2, b=I(c("a", "b")))')
self.assertEqual('1', dataf.rownames[0])
self.assertEqual('2', dataf.rownames[1])
def testColnames_set(self):
dataf = robjects.r('data.frame(a=1:2, b=I(c("a", "b")))')
dataf.colnames = robjects.StrVector('de')
self.assertEqual('d', dataf.colnames[0])
self.assertEqual('e', dataf.colnames[1])
def testRownames(self):
dataf = robjects.r('data.frame(a=1:2, b=I(c("a", "b")))')
self.assertEqual('a', dataf.colnames[0])
self.assertEqual('b', dataf.colnames[1])
def testRownames_set(self):
dataf = robjects.r('data.frame(a=1:2, b=I(c("a", "b")))')
dataf.rownames = robjects.StrVector('de')
self.assertEqual('d', dataf.rownames[0])
self.assertEqual('e', dataf.rownames[1])
def testCbind(self):
dataf = robjects.r('data.frame(a=1:2, b=I(c("a", "b")))')
dataf = dataf.cbind(robjects.r('data.frame(a=1:2, b=I(c("a", "b")))'))
self.assertEqual(4, dataf.ncol)
self.assertEqual(2, len([x for x in dataf.colnames if x == 'a']))
def testCbind(self):
dataf = robjects.r('data.frame(a=1:2, b=I(c("a", "b")))')
dataf = dataf.cbind(a = robjects.StrVector(("c", "d")))
self.assertEqual(3, dataf.ncol)
self.assertEqual(2, len([x for x in dataf.colnames if x == 'a']))
def suite():
suite = unittest.TestLoader().loadTestsFromTestCase(DataFrameTestCase)
return suite
if __name__ == '__main__':
unittest.main()
|