Implementation of the DataFrame Standard for pandas and polars.
Please read our blog post! https://data-apis.org/blog/dataframe_standard_rfc/.
Please check https://data-apis.org/dataframe-api/draft/API_specification/index.html for the methods supported by the Consortium Dataframe Standard.
Here's an example of how you can try this out:
import polars as pl
df = pl.DataFrame({'a': [1,2,3]})
df_std = df.__dataframe_consortium_standard__()
The object df_std
is a Standard-compliant DataFrame. Check the
API Specification
for the full list of methods supported on it.
Here's an example of a dataframe-agnostic function:
from typing import Any
def my_dataframe_agnostic_function(df_non_standard: Any) -> Any:
df = df_non_standard.__dataframe_consortium_standard__()
xp = df.__dataframe_namespace__()
for column_name in df.column_names:
new_column = xp.col(column_name)
new_column = (new_column - new_column.mean()) / new_column.std()
df = df.assign(new_column.rename(f'{column_name}_scaled'))
return df.dataframe
As long as you have this package installed, then either a pandas or Polars DataFrame should work with the code above, e.g.:
import pandas as pd
import polars as pl
df_pd = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
df_pl = pl.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
my_dataframe_agnostic_function(df_pd)
my_dataframe_agnostic_function(df_pl)
This implementation adds some extra syntax and constructs which are not yet part of the Standard. Follow along with the discussion at data-apis/dataframe-api#249.
pip install dataframe-api-compat