Skip to content

When iteratively 'add_rows' to a chart, the index of the drawing chart is not right. #748

@lifan-ake

Description

@lifan-ake

Summary

When iteratively 'add_rows' to a chart, the index of the drawing chart is not right.

Steps to reproduce

When I draw the chart in the following way, the index of the chart will be wrong.

import streamlit as st
import pandas as pd

chart = st.line_chart()

for i in range(10):
    chart.add_rows(pd.DataFrame([[i+1]], columns=['a']))

Expected behavior:

The index in the chart should be 0 to 9 with step 1.

Actual behavior:

But the actual index is like the following chart.

Screen Shot 2019-11-26 at 11 22 50 AM

Is this a regression?

no

Debug info

  • Streamlit version: 0.50.2
  • Python version: 3.6.9
  • Using Conda
  • OS version: macOS Catalina 10.15
  • Browser version: Chrome 78.0.3904.108

Additional information

My fixing suggestion.

def _maybe_melt_data_for_add_rows(data, delta_type, last_index):
    import pandas as pd
    import streamlit.elements.data_frame_proto as data_frame_proto

    # For some delta types we have to reshape the data structure
    # otherwise the input data and the actual data used
    # by vega_lite will be different and it will throw an error.
    if delta_type in DELTAS_TYPES_THAT_MELT_DATAFRAMES:
        if not isinstance(data, pd.DataFrame):
            data = data_frame_proto.convert_anything_to_df(data)

        if type(data.index) is pd.RangeIndex:
            old_step = _get_pandas_index_attr(data, "step")

            # We have to drop the predefined index
            data = data.reset_index(drop=True)

            old_stop = _get_pandas_index_attr(data, "stop")

            if old_step is None or old_stop is None:
                raise StreamlitAPIException(
                    "'RangeIndex' object has no attribute 'step'"
                )

            start = last_index + old_step
            stop = last_index + old_step + old_stop

            data.index = pd.RangeIndex(start=start, stop=stop, step=old_step)
            last_index = stop  # I think this line should be last_index = stop - 1

        data = pd.melt(data.reset_index(), id_vars=["index"])

    return data, last_index

Metadata

Metadata

Assignees

Labels

type:bugSomething isn't working as expected

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions