Skip to content

Instantly share code, notes, and snippets.

@jmsdnns
Created September 17, 2024 06:13
Show Gist options
  • Save jmsdnns/86b9a61c31cc3761e0cd261778ee174c to your computer and use it in GitHub Desktop.
Save jmsdnns/86b9a61c31cc3761e0cd261778ee174c to your computer and use it in GitHub Desktop.
Fluent interfaces technically return self or this from each chained function. Pandas returns new instances of the same type. Technically, that isn't fluent. I suspect it is meant to be indistinguishable for optimization reasons.
#!/usr/bin/env python
import pandas as pd
def getDataframe(url_table, ind):
df = pd.read_html(url_table)[ind]
return df
# Chaining functions
df = (
getDataframe("https://en.wikipedia.org/wiki/Historical_population_of_Ireland", 1)
.drop(columns=["Rank"])
.rename(columns=lambda x: x.lower())
.query("province == 'Leinster'")
.sort_values("density (/ km2)", ascending=True)
)
print(df)
# But, is this a fluent interface? It creates new objects each time
df = getDataframe("https://en.wikipedia.org/wiki/Historical_population_of_Ireland", 1)
print(id(df))
df = df.drop(columns=["Rank"])
print(id(df))
df = df.rename(columns=lambda x: x.lower())
print(id(df))
df = df.query("province == 'Leinster'")
print(id(df))
df = df.sort_values("density (/ km2)", ascending=True)
print(id(df))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment