I have a table in a SQL Server which contains, among other things, a DateTime
column From_UTC
and a DateTime NULL
column To_UTC
. I try to read it into a pandas DataFrame
.
The non-NULL column comes in fine as a datetime64
, but the other gets datatype Object
.
import pandas.io.sql as psql
import adodbapi
conn = adodbapi.connect(...)
df = psql.read_frame("SELECT Unit, To_UTC, From_UTC, CategoryID FROM T_Alarm WHERE ...", con=conn)
df.dtypes
Unit object
To_UTC object
From_UTC datetime64[ns]
CategoryID int64
dtype: object
df.To_UTC[0]
==> pywintypes.datetime(2011, 9, 6, 13, 51, 56)
If I include a WHERE To_UTC IS NOT NULL
in my query, everything works fine.
I guess that a combination of adodbapi, the pywintypes.datetime objects, and the NULLs in the result prevents pandas from coercing into the right type?
I tried df.convert_objects()
, but it didn't help.
UPDATE:
If I do df.To_UTC = pandas.Series(df.To_UTC, dtype='datetime64[ns]')
then I get the DataFrame I want.
But I would still like a more general solution so that I don't have to make this hack everywhere I do frame queries with nullable datetime results.