Puzzle 25 Hit and Run

Pandas Brain Teasers — by Miki Tebeka (33 / 34)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Late Addition | TOC | You May Be Interested In… 👉

hits.py

​ ​import​ ​sqlite3​
​ ​import​ ​pandas​ ​as​ ​pd​

​ conn = sqlite3.connect(​':memory:'​)
​ conn.executescript(​'''​
​ ​CREATE TABLE visits (day DATE, hits INTEGER);​
​ ​INSERT INTO visits VALUES​
​ ​ ('2020-07-01', 300),​
​ ​ ('2020-07-02', 500),​
​ ​ ('2020-07-03', 900);​
​ ​'''​)

​ df = pd.read_sql(​'SELECT * FROM visits'​, conn)
​ ​print​(​'time span:'​, df[​'day'​].max() - df[​'day'​].min())

Guess the Output

IMPORTANT

Try to guess what the output is before moving to the next page.

images/hline.png

This code will raise a TypeError.

images/hline.png

I love SQLite3. It’s a great single-file database that I’ve used many times to transfer data. It is widely used and heavily tested and can handle vast amounts of data (currently about 140 terabytes).

However, you need to know how to work with it.

In the teaser code, we create a hits table that has two columns:

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.