Skip to content

Instantly share code, notes, and snippets.

@danbirken
Last active August 29, 2015 14:01
Show Gist options
  • Save danbirken/cfd489282f54bc94d3ec to your computer and use it in GitHub Desktop.
Save danbirken/cfd489282f54bc94d3ec to your computer and use it in GitHub Desktop.
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import datetime
Base = declarative_base()
engine = create_engine('postgresql://postgres:@localhost/testing')
session = sessionmaker(bind=engine)()
class Test(Base):
__tablename__ = 'testing'
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
dt = sqlalchemy.Column(sqlalchemy.DateTime(timezone=True))
dt2 = sqlalchemy.Column(sqlalchemy.DateTime())
Base.metadata.create_all(engine)
session.add(Test(dt=datetime.datetime.now(), dt2=datetime.datetime.utcnow()))
session.commit()
session.flush()
import pandas as pd
df = pd.read_sql_table('testing', engine)
IPython 1.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: df
Out[1]:
id dt dt2
0 1 2014-05-23 15:43:12.731037-07:00 2014-05-23 22:43:12.731046
In [2]: df['dt']
Out[2]:
0 2014-05-23 15:43:12.731037-07:00
Name: dt, dtype: object
In [3]: df['dt'][0]
Out[3]: Timestamp('2014-05-23 15:43:12.731037-0700', tz='psycopg2.tz.FixedOffsetTimezone(offset=-420, name=None)')
# Datetime with time zone field remains a tz-aware timestamp
In [4]: df['dt2']
Out[5]:
0 2014-05-23 22:43:12.731046
Name: dt2, dtype: datetime64[ns]
# Datetime without time zone is parsed into datetime64[ns]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment