Arthur Camberlein >> SEO & Data articles >> Type hints in Python function

Type hints in Python function

Type hints in Python function

While reviewing code from a colleague, I came across a -> pd.DataFrame in a Python function. I discovered that this indicates that the function is expected to return a pandas DataFrame.

Learning something new everyday

I like learning something new everyday and today it was these type of hints for Python function. I am sure I saw them before, but might have been "too focus" on something else to even wonder what they are.

So, how does it work?

  1. Create a function, let's call it create_dataframe
  2. Before the : on the first row, -> pd.DataFrame
  3. Voila, you got it

In other words it would look like this:

import pandas as pd
import pandas_gbq as pbq
def create_dataframe() -> pd.DataFrame:
query = "SELECT * FROM your_bq_table"
return pbq.read_gbq(query)

In this example I used the pandas_gbq to query a Big Query data base in SQL

Retour au blog

Blog post taggued in:Learning, Python, Python SEO, SQL

Related blog posts: