Skip to content

update docs on how to opt-in #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions protocol/purpose_and_scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,30 @@ aims to develop in the future. When that full API standard is implemented by
dataframe libraries, the example above can change to:

```python
def get_df_module(df):
def get_compliant_df(df):
"""Utility function to support programming against a dataframe API"""
if hasattr(df, '__dataframe_namespace__'):
# Retrieve the namespace
pdx = df.__dataframe_namespace__()
# Is already Standard-compliant DataFrame, nothing to do here.
pass
elif hasattr(df, '__dataframe_standard__'):
# Convert to Standard-compliant DataFrame.
df = df.__dataframe_standard__()
else:
# Here we can raise an exception if we only want to support compliant dataframes,
# or convert to our default choice of dataframe if we want to accept (e.g.) dicts
pdx = pd
df = pd.DataFrame(df)

return pdx, df
raise TypeError(
"Expected Standard-compliant DataFrame, or DataFrame with Standard-compliant implementation"
)
return df


def somefunc(df, ...):
"""`df` can be any dataframe conforming to the dataframe API standard"""
pdx, df = get_df_module(df)
# From now on, use `df` methods and `pdx` functions/objects
# Get Standard-compliant DataFrame.
df = get_compliant_df(df)
# Get Standard namespace (optional, only if you need methods from it).
namespace = df.__dataframe_namespace__()
# From now on, use `df` methods and `namespace` functions/objects
```


Expand Down