Skip to content

Commit 04650ba

Browse files
authored
update docs on how to opt-in (#172)
1 parent 5df2ede commit 04650ba

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

protocol/purpose_and_scope.md

+15-9
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,30 @@ aims to develop in the future. When that full API standard is implemented by
5050
dataframe libraries, the example above can change to:
5151

5252
```python
53-
def get_df_module(df):
53+
def get_compliant_df(df):
5454
"""Utility function to support programming against a dataframe API"""
5555
if hasattr(df, '__dataframe_namespace__'):
56-
# Retrieve the namespace
57-
pdx = df.__dataframe_namespace__()
56+
# Is already Standard-compliant DataFrame, nothing to do here.
57+
pass
58+
elif hasattr(df, '__dataframe_standard__'):
59+
# Convert to Standard-compliant DataFrame.
60+
df = df.__dataframe_standard__()
5861
else:
5962
# Here we can raise an exception if we only want to support compliant dataframes,
6063
# or convert to our default choice of dataframe if we want to accept (e.g.) dicts
61-
pdx = pd
62-
df = pd.DataFrame(df)
63-
64-
return pdx, df
64+
raise TypeError(
65+
"Expected Standard-compliant DataFrame, or DataFrame with Standard-compliant implementation"
66+
)
67+
return df
6568

6669

6770
def somefunc(df, ...):
6871
"""`df` can be any dataframe conforming to the dataframe API standard"""
69-
pdx, df = get_df_module(df)
70-
# From now on, use `df` methods and `pdx` functions/objects
72+
# Get Standard-compliant DataFrame.
73+
df = get_compliant_df(df)
74+
# Get Standard namespace (optional, only if you need methods from it).
75+
namespace = df.__dataframe_namespace__()
76+
# From now on, use `df` methods and `namespace` functions/objects
7177
```
7278

7379

0 commit comments

Comments
 (0)