Skip to content

Instantly share code, notes, and snippets.

@tpjfern03
tpjfern03 / sql.py
Last active November 29, 2018 18:38 — forked from jorisvandenbossche/sql.py
Patched version of pandas.io.sql to support PostgreSQL. Collection of query wrappers / abstractions to both facilitate data retrieval and to reduce dependency on DB-specific API. #pgsql
"""
Patched version to support PostgreSQL
(original version: https://github.com/pydata/pandas/blob/v0.13.1/pandas/io/sql.py)
Adapted functions are:
- added _write_postgresql
- updated table_exist
- updated get_sqltype
- updated get_schema
@tpjfern03
tpjfern03 / SQLAlchemy Cheat Sheet
Last active May 31, 2021 10:26 — forked from akkefa/SQLAlchemy Cheat Sheet
SQLAlchemy is a deep and powerful thing made up of many layers. This cheat sheet sticks to parts of the ORM (Object Relational Mapper) layer,and aims to be a reference not a tutorial. That said, if you are familiar with SQL then this cheat sheet should get you well on your way to understanding SQLAlchemy. #sqlalchemy
#A SQLAlchemy Cheat Sheet
###Introduction
##Basic Models
One model is used to describe one database table. For example:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session,sessionmaker
from zope.sqlalchemy import ZopeTransactionExtension
from sqlalchemy import (
@tpjfern03
tpjfern03 / cx_oracle_to_pandas.py
Created November 29, 2018 18:33 — forked from mvaz/cx_oracle_to_pandas.py
Example of executing and reading a query into a pandas dataframe #cx_oracle
import cx_Oracle
import pandas
connection = cx_Oracle.connect('username/pwd@host:port/dbname')
def read_query(connection, query):
cursor = connection.cursor()
try:
cursor.execute( query )
names = [ x[0] for x in cursor.description]
@tpjfern03
tpjfern03 / Working_with_cx_Oracle.md
Last active November 29, 2018 18:30 — forked from jtrive84/Working_with_cx_Oracle.md
Introduction to accessing Oracle data from Python using the cx_Oracle library. #cx_oracle

Introduction to cx_Oracle

cx_Oracle is a third-party Python library that facilitates Oracle-to-Python database communication. A cursor is a control structure that enables traversal over the records in a database. This is Python's primary means of accessing database table data. The setup is:

  • Create a connection object
  • Define a cursor and call the cursor() method on the connection object
  • Construct a query string for the data of interest
  • Pass the query string to the cursor's execute method
@tpjfern03
tpjfern03 / sqlalchemy_upsert.py
Created November 29, 2018 18:25 — forked from malexer/sqlalchemy_upsert.py
Modelling UPSERT in SQLAlchemy (well actually it is not upsert but speed improvement is significant in comparison with simple session.merge) #sqlalchemy
# Note: it is a copy of great answer by "mgoldwasser" from Stackoverflow
# Check the original answer here: http://stackoverflow.com/a/26018934/1032439
# Imagine that post1, post5, and post1000 are posts objects with ids 1, 5 and 1000 respectively
# The goal is to "upsert" these posts.
# we initialize a dict which maps id to the post object
my_new_posts = {1: post1, 5: post5, 1000: post1000}
for each in posts.query.filter(posts.id.in_(my_new_posts.keys())).all():
@tpjfern03
tpjfern03 / load_data.py
Last active November 29, 2018 18:17 — forked from evz/load_data.py
Faster loader for loading data to pgsql #pgsql #sqlalchemy
import csv
import sqlalchemy as sa
import os
import pytz
from datetime import datetime
import itertools
tz = pytz.timezone('America/Chicago')
# DB_CONN = os.environ['DATABASE_URL']
@tpjfern03
tpjfern03 / df2json.py
Last active May 20, 2016 14:46 — forked from mikedewar/df2json.py
A little script to convert a pandas data frame to a JSON object. Is there a better way?
"""
tiny script to convert a pandas data frame into a JSON object
"""
import ujson as json
import pandas
import numpy as np
df = pandas.DataFrame({
"time" : [1,2,3,4,5],
#List unique values in a DataFrame column
pd.unique(df.column_name.ravel())
#Convert Series datatype to numeric, getting rid of any non-numeric values
df['col'] = df['col'].astype(str).convert_objects(convert_numeric=True)
#Grab DataFrame rows where column has certain values
valuelist = ['value1', 'value2', 'value3']
df = df[df.column.isin(value_list)]
@tpjfern03
tpjfern03 / pandas_dbms.py
Created May 20, 2016 14:29 — forked from catawbasam/pandas_dbms.py
Python PANDAS : load and save Dataframes to sqlite, MySQL, Oracle, Postgres
# -*- coding: utf-8 -*-
"""
LICENSE: BSD (same as pandas)
example use of pandas with oracle mysql postgresql sqlite
- updated 9/18/2012 with better column name handling; couple of bug fixes.
- used ~20 times for various ETL jobs. Mostly MySQL, but some Oracle.
to do:
save/restore index (how to check table existence? just do select count(*)?),
finish odbc,