Constructs a dataframe and modifies `args` in-place. The argument values in `args` can be either strings corresponding to existing columns of a dataframe, or data arrays (lists, numpy arrays, pandas columns, series). Parameters ---------- args : OrderedDict arg
(args, constructor)
| 1478 | |
| 1479 | |
| 1480 | def build_dataframe(args, constructor): |
| 1481 | """ |
| 1482 | Constructs a dataframe and modifies `args` in-place. |
| 1483 | |
| 1484 | The argument values in `args` can be either strings corresponding to |
| 1485 | existing columns of a dataframe, or data arrays (lists, numpy arrays, |
| 1486 | pandas columns, series). |
| 1487 | |
| 1488 | Parameters |
| 1489 | ---------- |
| 1490 | args : OrderedDict |
| 1491 | arguments passed to the px function and subsequently modified |
| 1492 | constructor : graph_object trace class |
| 1493 | the trace type selected for this figure |
| 1494 | """ |
| 1495 | |
| 1496 | # make copies of all the fields via dict() and list() |
| 1497 | for field in args: |
| 1498 | if field in array_attrables and args[field] is not None: |
| 1499 | if isinstance(args[field], dict): |
| 1500 | args[field] = dict(args[field]) |
| 1501 | elif field in ["custom_data", "hover_data"] and isinstance( |
| 1502 | args[field], str |
| 1503 | ): |
| 1504 | args[field] = [args[field]] |
| 1505 | else: |
| 1506 | args[field] = list(args[field]) |
| 1507 | |
| 1508 | # Cast data_frame argument to DataFrame (it could be a numpy array, dict etc.) |
| 1509 | df_provided = args["data_frame"] is not None |
| 1510 | |
| 1511 | # Flag that indicates if the resulting data_frame after parsing is pandas-like |
| 1512 | # (in terms of resulting Narwhals DataFrame). |
| 1513 | # True if pandas, modin.pandas or cudf DataFrame/Series instance, or converted from |
| 1514 | # PySpark to pandas. |
| 1515 | is_pd_like = False |
| 1516 | |
| 1517 | # Flag that indicates if data_frame needs to be converted to PyArrow. |
| 1518 | # True if Ibis, DuckDB, Vaex, or implements __dataframe__ |
| 1519 | needs_interchanging = False |
| 1520 | |
| 1521 | # If data_frame is provided, we parse it into a narwhals DataFrame, while accounting |
| 1522 | # for compatibility with pandas specific paths (e.g. Index/MultiIndex case). |
| 1523 | if df_provided: |
| 1524 | # data_frame is pandas-like DataFrame (pandas, modin.pandas, cudf) |
| 1525 | if nw.dependencies.is_pandas_like_dataframe(args["data_frame"]): |
| 1526 | columns = args["data_frame"].columns # This can be multi index |
| 1527 | args["data_frame"] = nw.from_native(args["data_frame"], eager_only=True) |
| 1528 | is_pd_like = True |
| 1529 | |
| 1530 | # data_frame is pandas-like Series (pandas, modin.pandas, cudf) |
| 1531 | elif nw.dependencies.is_pandas_like_series(args["data_frame"]): |
| 1532 | args["data_frame"] = nw.from_native( |
| 1533 | args["data_frame"], series_only=True |
| 1534 | ).to_frame() |
| 1535 | columns = args["data_frame"].columns |
| 1536 | is_pd_like = True |
| 1537 |