[FEATURE]: Make Plotly Express dataframe implementation-agnostic
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 18.8k
- 派生
- 2.8k
- 平均合并
- 16 小时 26 分钟
- 30 天内合并 PR
- 21
描述
[FEATURE]: Make Plotly Express dataframe implementation-agnostic
Description
For a long time, there's been questions around how tightly Plotly Express depends on pandas. For example, here's a program from the Getting Started page that's supposed to show how easy it is to make a simple plot with Plotly Express:
import plotly.express as px
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2])
fig.show()
But if you run this with only plotly[express] installed, you get an error stating that pandas is required:
Traceback (most recent call last):
File "/.../plotly/express/_core.py", line 1210, in to_named_series
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
...
NotImplementedError: Pandas installation is required if no dataframe is provided.
I think I understand this error; Plotly doesn't want to require pandas, when there are other dataframe-based libraries that people are choosing such as Polars. However, this message appears even if you have Polars installed. I think we can make Plotly Express more agnostic toward dataframe providers by looking for an available provider, rather than exiting if pandas is not found.
Why should this feature be added?
Plotly claims to not require pandas, but it still requires pandas in places where other dataframe implementations seem to work.
Mocks/ designs
I made some small changes to plotly/express/_core.py, which allows the above example to work with Polars installed. First, add a new function to look for an existing library that supports dataframes through narwhals:
def get_df_implementation():
"""Get an installed dataframe implementation."""
for df_implementation in ("pandas", "polars"):
try:
return import_module(df_implementation)
except ImportError:
pass
msg = (
"A dataframe implementation such as Pandas or Polars is required if "
"no dataframe is provided."
)
raise NotImplementedError(msg)
This POC version looks for pandas first, and then Polars.
The only change in the function to_named_series() is to call get_df_implemenation() in the else block, instead of doing its own check for pandas:
def to_named_series(x, name=None, native_namespace=None):
...
if isinstance(x, nw.Series):
return x.rename(name)
elif native_namespace is not None:
return nw.new_series(name=name, values=x, native_namespace=native_namespace)
else:
df_implementation = get_df_implementation()
return nw.new_series(name=name, values=x, native_namespace=df_implementation)
The existing function process_args_into_dataframe() needs one call to get_df_implemenation() as well:
def process_args_into_dataframe(
args, wide_mode, var_name, value_name, is_pd_like, native_namespace
):
...
length = len(df_output[next(iter(df_output))]) if len(df_output) else 0
if native_namespace is None:
native_namespace = get_df_implementation()
if ranges:
import numpy as np
...
With these changes, the above example runs without errors when Polars is installed. When pandas and Polars are both unavailable, the error states that one of these libraries must be installed.
Notes
I'm not tied to this particular implementation in any way, this was just a proof of concept to see if we could make Plotly more agnostic about the dataframe provider.
Is this a direction that's worth pursuing? I'm happy to work on a PR if it is, or happy to see others more versed in Plotly's internals implement it.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 plotly/express/_core.py 开始,阅读 to_named_series() 和 process_args_into_dataframe(),然后检查 Narwhals 如何接收原生 dataframe namespace。在 pandas 不可用且已安装 Polars 的情况下运行 Getting Started 柱状图示例,并验证该示例可以正常工作,同时在两个 provider 都不可用时,缺少 provider 的错误仍然包含有用的信息。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- pandas, python
- 领域
- data-visualization
- Issue 类型
- 功能
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 冷清
- 描述清晰度
- 基本清楚
- 新手友好度
- 58/100