Question

I'm a newbie coder so please be gentle. I am trying to put a column of dates of a timestamp type from a Postgres 9 database (date looks like this in the database: 2012-03-02 14:25:49) into an rpy2 vector (I'm using rpy2 version 2.3.9) that can then be used to create a DataFrame for a chart using ggplot2. It seems that POSIXlt is the appropriate rpy2 data type to use, but perhaps I'm wrong. I don't need the time, just the date for the chart. I'm doing this in Python 2.7, using SqlAlchemy 8.3 to access the date column of the database, which SQLAlchemy converts to a Python datetime.datetime format. The date data is in the 'publish_date' column/code below. Connecting to and accessing data from the db seems to be working fine, so I think this is primarily an rpy2 issue. Here’s the code:

# PART 0 - Initialize the packages
import sqlalchemy 
from sqlalchemy import *
import rpy2.robjects as robjects
from rpy2.robjects.vectors import DataFrame
from rpy2.robjects.packages import importr
import rpy2.robjects.lib.ggplot2 as ggplot2


# PART 1 - connect to the database
engine = create_engine('postgresql://postgres:pword@localhost/mydb')
metadata = MetaData()
conn = engine.connect()


# PART 2 - ask for stories table
stories = Table('stories', metadata, autoload=True, autoload_with=engine)


# PART 3 - read from that table and populate a new dictionary 
select = sqlalchemy.sql.select

# create dictionary with column names as keys and empty lists of vectors for data
newdict = {
    "stories_id":robjects.IntVector([]),
    "publish_date":robjects.POSIXlt([])
}

# connect to the database, execute a SELECT statement that iterates through table            
# populate the cursor 'row' with data from the table

for row in conn.execute(select([Table('stories', metadata, autoload=True autoload_with=engine)])): 
    newdict["stories_id"] += row.stories_id
    newdict["publish_date"] += row.publish_date

This is the error I get when I run the code up to this point.

ValueError                                Traceback (most recent call last)
<ipython-input-377-4483b6760461> in <module>()
     50 for row in conn.execute(select([Table('stories', metadata, autoload=True,     autoload_with=engine)])):
     51     newdict["stories_id"] += row.stories_id
 ---> 52     newdict["publish_date"] += row.publish_date
     53 
     54 

/Users/user/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/rpy2-2.3.9-     py2.7-macosx-10.6-x86_64.egg/rpy2/robjects/vectors.pyc in __add__(self, x)
    226 
    227     def __add__(self, x):
--> 228         res = baseenv_ri.get("c")(self, conversion.py2ri(x))
    229         res = conversion.ri2py(res)
    230         return res

/Users/user/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/rpy2-2.3.9-    py2.7-macosx-10.6-x86_64.egg/rpy2/robjects/numpy2ri.pyc in numpy2ri(o)
      59             raise(ValueError("Unknown numpy array type."))
      60     else:
 ---> 61         res = ro.default_py2ri(o)
      62     return res
      63 

 /Users/user/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/rpy2-2.3.9-   py2.7-macosx-10.6-x86_64.egg/rpy2/robjects/__init__.pyc in default_py2ri(o)
    146         res = rinterface.SexpVector([o, ], rinterface.CPLXSXP)
    147     else:
--> 148         raise(ValueError("Nothing can be done for the type %s at the moment." %   (type(o))))
    149     return res
    150 

 ValueError: Nothing can be done for the type <type 'datetime.datetime'> at the moment.

THanks for any help you can provide. I'm at a total loss.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top