Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save blakeNaccarato/bf8d3dac3ae5e9240238fe165331cf53 to your computer and use it in GitHub Desktop.

Select an option

Save blakeNaccarato/bf8d3dac3ae5e9240238fe165331cf53 to your computer and use it in GitHub Desktop.
Insert sparkline row at top of DataFrames to preview time series in Data Wrangler

The Data Wrangler VSCode extension column summaries can show a histogram of the distribution of data in a column, but this summary is not particularly useful for time series data like sensor signals.

  • If not installed, install the Data Wrangler VSCode extension
  • If sparklines is not installed in the environment to be used by Data Wrangler, use e.g. uv to add the sparklines package to the project's dev dependency group
uv add --dev sparklines
  • Open a data file, e.g. a CSV in VSCode and click the "Open in Data Wrangler" button in the tab ribbon

image

  • If prompted to select a runtime/kernel to run Data Wrangler with, select the one that you installed sparklines to, probably the .venv in the current directory
  • Paste the following Python code into the active Data Wrangler view of e.g. a CSV data file containing time series data in the columns. This inserts a row at the top of the dataframe with a little ASCII "plot" of chronological bars, where no bar indicates the minimum value and a full bar indicates the maximum. Note that this step should be applied only temporarily to preview data as it inserts the ASCII strings into the actual columns of numerical data, but is useful to preview time series data nonetheless.
from pandas import Series, concat, read_csv
from sparklines import sparklines

WIDTH = 20

df = df.apply(
    lambda ser: concat([
        Series(
            [
                "\n".join(
                    sparklines(
                        numbers=ser[:: len(ser) // WIDTH].dropna() - ser[:: len(ser) // WIDTH].min(),
                        num_lines=1,
                    )
                )
            ]
        ),
        ser,
    ])
)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment