@@ -50,24 +50,30 @@ aims to develop in the future. When that full API standard is implemented by
50
50
dataframe libraries, the example above can change to:
51
51
52
52
``` python
53
- def get_df_module (df ):
53
+ def get_compliant_df (df ):
54
54
""" Utility function to support programming against a dataframe API"""
55
55
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__()
58
61
else :
59
62
# Here we can raise an exception if we only want to support compliant dataframes,
60
63
# 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
65
68
66
69
67
70
def somefunc (df , ...):
68
71
""" `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
71
77
```
72
78
73
79
0 commit comments