Skip to content

Commit

Permalink
fix demo to work with modern pandas and python3, reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Dec 22, 2015
1 parent 9f50b83 commit 97ff5e5
Showing 1 changed file with 32 additions and 35 deletions.
67 changes: 32 additions & 35 deletions examples/demo.py
Expand Up @@ -4,16 +4,15 @@
from pandasql import load_meat, load_births
import re


births = load_births()
meat = load_meat()
iris = load_iris()
iris_df = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_df['species'] = pd.Categorical(iris.target, levels=iris.target_names)
iris_df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
iris_df.columns = [re.sub("[() ]", "", col) for col in iris_df.columns]

print sqldf("select * from iris_df limit 10;", locals())
print sqldf("select sepalwidthcm, species from iris_df limit 10;", locals())
print(sqldf("SELECT * FROM iris_df LIMIT 10;", locals()))
print(sqldf("SELECT sepalwidthcm, species FROM iris_df LIMIT 10;", locals()))

q = """
select
Expand All @@ -27,26 +26,26 @@
species;
"""
print "*"*80
print "aggregation"
print "-"*80
print q
print sqldf(q, locals())
print("*" * 80)
print("aggregation")
print("-" * 80)
print(q)
print(sqldf(q, locals()))


def pysqldf(q):
"add this to your script if you get tired of calling locals()"
return sqldf(q, globals())

print "*"*80
print "calling from a helper function"
print '''def pysqldf(q):
"add this to your script if you get tired of calling locals()"
return sqldf(q, globals())'''
print "-"*80
print q
print pysqldf(q)

print("*" * 80)
print("calling from a helper function")
print('''def pysqldf(q):)
"add this to your script if you get tired of calling locals()"
return sqldf(q, globals())''')
print("-" * 80)
print(q)
print(pysqldf(q))

q = """
select
Expand All @@ -59,12 +58,11 @@ def pysqldf(q):
limit 10;
"""

print "*"*80
print "joins"
print "-"*80
print q
print pysqldf(q)

print("*" * 80)
print("joins")
print("-" * 80)
print(q)
print(pysqldf(q))

q = """
select
Expand All @@ -75,11 +73,11 @@ def pysqldf(q):
species = 'virginica'
and sepallengthcm > 7.7;
"""
print "*"*80
print "where clause"
print "-"*80
print q
print pysqldf(q)
print("*" * 80)
print("where clause")
print("-" * 80)
print(q)
print(pysqldf(q))
iris_df['id'] = range(len(iris_df))
q = """
select
Expand All @@ -89,11 +87,11 @@ def pysqldf(q):
where
id in (select id from iris_df where sepalwidthcm*sepallengthcm > 25);
"""
print "*"*80
print "subqueries"
print "-"*80
print q
print pysqldf(q)
print("*" * 80)
print("subqueries")
print("-" * 80)
print(q)
print(pysqldf(q))

q = """
SELECT
Expand All @@ -108,5 +106,4 @@ def pysqldf(q):
m.date;
"""

print pysqldf(q).head()

print(pysqldf(q).head())

0 comments on commit 97ff5e5

Please sign in to comment.