Skip to content

[BUG]: px.sunburst() constructor fails when using PyArrow backend with Pandas 3 #5571

@emilykl

Description

@emilykl

Description

When using Pandas 3, creating a px.sunburst() chart from a Pandas dataframe with a PyArrow backend fails with a TypeError.

Seems to be an underlying Narwhals (or possibly Pandas?) issue. See: narwhals-dev/narwhals#3546

Screenshots/Video

TypeError: operation 'add' not supported for dtype 'string[pyarrow]' with dtype 'str'

Steps to reproduce

  • Set up environment: pip install "plotly==6.7.0" "pandas==3.0.2" "pyarrow==23.0.1"
  • Run the following script:
import plotly.express as px
import pandas as pd

df = pd.DataFrame(dict(
    fruit=["apple", "apple", "banana", "banana"],
    color=["red", "green", "green", "yellow"],
    values=[21, 16, 12, 34],
    )).convert_dtypes(dtype_backend="pyarrow")
path = ["fruit", "color"]

# Raises TypeError
fig = px.sunburst(df, path=path)

Expected: Sunburst chart created successfully

Result: Fails with TypeError and error message seen above

Notes

Full stack trace:
Traceback (most recent call last):
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/pandas/core/arrays/arrow/array.py", line 986, in _evaluate_op_method
    result = pc.binary_join_element_wise(self._pa_array, other, sep)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/pyarrow/compute.py", line 271, in wrapper
    return func.call(args, options, memory_pool)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pyarrow/_compute.pyx", line 399, in pyarrow._compute.Function.call
  File "pyarrow/error.pxi", line 155, in pyarrow.lib.pyarrow_internal_check_status
  File "pyarrow/error.pxi", line 92, in pyarrow.lib.check_status
pyarrow.lib.ArrowNotImplementedError: Function 'binary_join_element_wise' has no kernel matching input types (string, large_string, string)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/ekl/code/plotly.py/scratch/minimal-repro-pandas-3-issue-sunburst.py", line 12, in <module>
    fig = px.sunburst(df, path=path)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/plotly/express/_chart_types.py", line 1781, in sunburst
    return make_figure(
           ^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/plotly/express/_core.py", line 2514, in make_figure
    args = process_dataframe_hierarchy(args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/plotly/express/_core.py", line 2099, in process_dataframe_hierarchy
    df_tree = df_tree.with_columns(
              ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/dataframe.py", line 2689, in with_columns
    return super().with_columns(*exprs, **named_exprs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/dataframe.py", line 214, in with_columns
    return self._with_compliant(self._compliant_frame.with_columns(*compliant_exprs))
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/_pandas_like/dataframe.py", line 503, in with_columns
    columns = self._evaluate_exprs(*exprs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/_compliant/dataframe.py", line 367, in _evaluate_exprs
    return tuple(chain.from_iterable(self._evaluate_expr(expr) for expr in exprs))  # pyright: ignore[reportArgumentType]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/_compliant/dataframe.py", line 367, in <genexpr>
    return tuple(chain.from_iterable(self._evaluate_expr(expr) for expr in exprs))  # pyright: ignore[reportArgumentType]
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/_compliant/dataframe.py", line 380, in _evaluate_expr
    result = expr(self)
             ^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/_compliant/expr.py", line 247, in __call__
    return self._call(df)
           ^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/_compliant/expr.py", line 651, in <lambda>
    lambda df: [series.alias(name) for series in self(df)],
                                                 ^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/_compliant/expr.py", line 247, in __call__
    return self._call(df)
           ^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/_pandas_like/namespace.py", line 333, in func
    result = reduce(lambda x, y: x + separator + y, series).zip_with(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/_pandas_like/namespace.py", line 333, in <lambda>
    result = reduce(lambda x, y: x + separator + y, series).zip_with(
                                 ~~~~~~~~~~~~~~^~~
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/_pandas_like/series.py", line 452, in __add__
    return self._with_binary(operator.add, other)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/narwhals/_pandas_like/series.py", line 415, in _with_binary
    op(ser, other_native), preserve_broadcast=preserve_broadcast
    ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/pandas/core/ops/common.py", line 85, in new_method
    return method(self, other)
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/pandas/core/arraylike.py", line 190, in __add__
    return self._arith_method(other, operator.add)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/pandas/core/series.py", line 6751, in _arith_method
    return base.IndexOpsMixin._arith_method(self, other, op)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/pandas/core/base.py", line 1644, in _arith_method
    result = ops.arithmetic_op(lvalues, rvalues, op)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/pandas/core/ops/array_ops.py", line 279, in arithmetic_op
    res_values = op(left, right)
                 ^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/pandas/core/ops/common.py", line 85, in new_method
    return method(self, other)
           ^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/pandas/core/arraylike.py", line 190, in __add__
    return self._arith_method(other, operator.add)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/pandas/core/arrays/arrow/array.py", line 1079, in _arith_method
    result = self._evaluate_op_method(other, op, ARROW_ARITHMETIC_FUNCS)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ekl/code/plotly.py/venv/fresh-env-311/lib/python3.11/site-packages/pandas/core/arrays/arrow/array.py", line 990, in _evaluate_op_method
    raise TypeError(
TypeError: operation 'add' not supported for dtype 'string[pyarrow]' with dtype 'str'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsomething broken

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions