PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/rpy/robjects/tests/testPandasConversions.py

https://bitbucket.org/lgautier/rpy2
Python | 93 lines | 68 code | 15 blank | 10 comment | 4 complexity | 8cf202584ac46380ad712bd9941327d9 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. import unittest
  2. import rpy2.robjects as robjects
  3. from collections import OrderedDict
  4. from datetime import datetime
  5. # XXX - this is inconsistent with how test_ggplot2 is handled
  6. # See rpy2/robjects/lib/tests/__init__.py for the details
  7. try:
  8. import pandas
  9. import numpy
  10. has_pandas = True
  11. import rpy2.robjects.pandas2ri as rpyp
  12. except:
  13. has_pandas = False
  14. class MissingPandasDummyTestCase(unittest.TestCase):
  15. def testMissingPandas(self):
  16. self.assertTrue(False) # pandas is missing. No tests.
  17. class PandasConversionsTestCase(unittest.TestCase):
  18. def setUp(self):
  19. rpyp.activate()
  20. def tearDown(self):
  21. robjects.conversion.py2ri = robjects.default_py2ri
  22. robjects.conversion.ri2py = robjects.default_ri2py
  23. def testDataFrame(self):
  24. l = (('b', numpy.array([True, False, True], dtype=numpy.bool_)),
  25. ('i', numpy.array([1, 2, 3], dtype="i")),
  26. ('f', numpy.array([1, 2, 3], dtype="f")),
  27. ('s', numpy.array(["a", "b", "c"], dtype="S")),
  28. ('u', numpy.array([u"a", u"b", u"c"], dtype="U")),
  29. ('dates', [datetime(2012, 5, 2),
  30. datetime(2012, 6, 3),
  31. datetime(2012, 7, 1)]))
  32. od = OrderedDict(l)
  33. pd_df = pandas.core.frame.DataFrame(od)
  34. rp_df = robjects.conversion.py2ri(pd_df)
  35. self.assertEqual(pd_df.shape[0], rp_df.nrow)
  36. self.assertEqual(pd_df.shape[1], rp_df.ncol)
  37. def testDataFrame_PR130(self):
  38. # PR #130
  39. a = pandas.core.frame.DataFrame(dict(dates=['05-01-2001', '04-01-2013'],
  40. not_necessary=[1, 2]))
  41. a.dates = pandas.to_datetime(a.dates)
  42. r_dataf = rpyp.pandas2ri(a)
  43. self.assertIsInstance(r_dataf, robjects.DataFrame)
  44. def testSeries(self):
  45. Series = pandas.core.series.Series
  46. s = Series(numpy.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
  47. rp_s = robjects.conversion.py2ri(s)
  48. self.assertIsInstance(rp_s, robjects.Array)
  49. def testRepr(self):
  50. # this should go to testVector, with other tests for repr()
  51. l = (('b', numpy.array([True, False, True], dtype=numpy.bool_)),
  52. ('i', numpy.array([1, 2, 3], dtype="i")),
  53. ('f', numpy.array([1, 2, 3], dtype="f")),
  54. ('s', numpy.array(["a", "b", "c"], dtype="S")),
  55. ('u', numpy.array([u"a", u"b", u"c"], dtype="U")))
  56. od = OrderedDict(l)
  57. pd_df = pandas.core.frame.DataFrame(od)
  58. rp_df = robjects.conversion.py2ri(pd_df)
  59. s = repr(rp_df) # used to fail with a TypeError
  60. s = s.split('\n')
  61. self.assertEqual('[Array, Array, Array, FactorV..., FactorV...]', s[1].strip())
  62. def testPandas2ri(self):
  63. # XXX - not a full test, just tests that the function returns the right
  64. # class. This is currently also the case with some of the tests above
  65. # (e.g., testSeries)
  66. rdataf = robjects.r('data.frame(a=1:2, b=c("a", "b"))')
  67. # Note - I'm following the convention above, but this conflates
  68. # .activate() with testing the function, so it's not as "unit" as it
  69. # could be.
  70. pandas_df = robjects.conversion.ri2py(rdataf)
  71. self.assertIsInstance(pandas_df, pandas.DataFrame)
  72. def suite():
  73. if has_pandas:
  74. return unittest.TestLoader().loadTestsFromTestCase(PandasConversionsTestCase)
  75. else:
  76. return unittest.TestLoader().loadTestsFromTestCase(MissingPandasDummyTestCase)
  77. if __name__ == '__main__':
  78. unittest.main(defaultTest='suite')