Status: New
Owner: ----
Labels: Type-Defect Priority-Medium
New issue 55 by
xjosi...@gmail.com: Problem handling timestamp columns
(without timezone) in R when pulling data using RPostgreSQL
http://code.google.com/p/rpostgresql/issues/detail?id=55
I'm trying to pull data from a PostgreSQL database and the results for a
timestamp field are inconsistent. I'm not sure if I'm handling POSIXct
results properly. Otherwise, I think I found a bug in the RPostgreSQL
package. Here are the steps to reproduce the problem:
1. Create testing database and table:
CREATE DATABASE mydb;
CREATE TABLE test_table
(
"DateTime" timestamp without time zone NOT NULL,
CONSTRAINT "pk_test_table" PRIMARY KEY ("DateTime")
)
WITH (
OIDS=FALSE
);
ALTER TABLE test_table
OWNER TO postgres;
2. And let’s say there are a few hundred records. I will populate them in
R. Here is the code:
library(RPostgreSQL)
# Let's feed the table with some sequence of date/time values
date_values <- as.chron(seq(10000, 10500, 1/24))
format.chron <- function(z) {
sprintf("%04.0f-%02.0f-%02.0f %02.0f:%02.0f:00",
as.numeric(as.character(years(z))),
months(z),
as.numeric(as.character(days(z))),
as.numeric(as.character(hours(z))),
as.numeric(as.character(minutes(z))))
}
.generateInsertQuery <- function(date_values, field_name, table_name) {
insert_val <- paste(paste0("(", sQuote(format(date_values)), ")"),
collapse=',')
qry <- paste("INSERT INTO", dQuote(table_name), paste0("(",
dQuote(field_name), ")"), "VALUES", insert_val)
qry
}
drv <- dbDriver('PostgreSQL')
con <- dbConnect(drv, user='postgres', dbname='mydb')
qry <- .generateInsertQuery(date_values, "DateTime", "test_table")
dbSendQuery(con, qry)
3. Trying to get the values from postgres, the time component gets stripped
out of the resulting data
res <- dbGetQuery(con, "SELECT * FROM test_table")
res[1:20,1]
The class of the result, however, is POSIXct
class(res[,1])
If the result is fetched one record at a time, the values with hour:min
equal to 00:00 loose the time component:
rs <- dbSendQuery(con, "SELECT \"DateTime\" FROM test_table")
res_list <- list()
for(i in 1:100) res_list[i] <- fetch(rs,1)
res_list
===================================================================
The expected output is a data.frame with a POSIXct column with date and
time components.
What I see instead is a POSIXct column only with date components. The time
component is stripped out of the data. If I want to see at least partially
correct values, I need to fetch results one at a time.
===================================================================
My system specs are:
"PostgreSQL 9.2.4, compiled by Visual C++ build 1600, 64-bit"
Win 7 Enterprise 64-bit
R 3.0.1 64-bit
RPostgreSQL version 0.4
DBI version 0.2-7
--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings