benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf ee5a7f1c-ac27-59c9-8b79-6f23f2912340

Problem

Answer published by the source. Consult the official source to check your work against its answer.

complete prompt

import pandas as pd
import matplotlib.pyplot as plt


def task_func(df: pd.DataFrame) -> pd.DataFrame:
    """
    Calculate the cumulative sum for each column in a given DataFrame and plot
    the results in a bar chart.

    Parameters:
    df (pd.DataFrame): The input DataFrame with numerical values.
                       Must not be empty and must contain numeric data to plot.
    Returns:
    - tuple: A tuple containing:
             (1) A DataFrame with cumulative sums for each column.
             (2) A matplotlib bar chart Figure of these cumulative sums.

    Raises:
    - ValueError: If the DataFrame is empty or contains non-numeric data.

    Requirements:
    - pandas
    - matplotlib

    Note:
    - NaN values are ignored in the cumulative sum calculation, i.e. treated as
      zero for the purpose of the sum without changing existing values to NaN.
    - The plot title is set to 'Cumulative Sum per Column'.
    - X-axis label is 'Index' and Y-axis label is 'Cumulative Sum'.
    - A legend is included in the plot.

    Example:
    >>> input_df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
    >>> output_df, fig = task_func(input_df)
    >>> output_df
       A   B
    0  1   4
    1  3   9
    2  6  15
    >>> fig
    <Figure size 640x480 with 1 Axes>
    """

instruct prompt

Calculate the cumulative sum for each column in a given DataFrame and plot the results in a bar chart.
Note that: NaN values are ignored in the cumulative sum calculation, i.e. treated as zero for the purpose of the sum without changing existing values to NaN. The plot title is set to 'Cumulative Sum per Column'. X-axis label is 'Index' and Y-axis label is 'Cumulative Sum'. A legend is included in the plot.
The function should raise the exception for: ValueError: If the DataFrame is empty or contains non-numeric data.
The function should output with:
    tuple: A tuple containing:
    (1) A DataFrame with cumulative sums for each column.
    (2) A matplotlib bar chart Figure of these cumulative sums.
You should write self-contained code starting with:

Code

import pandas as pd
import matplotlib.pyplot as plt
def task_func(df: pd.DataFrame) -> pd.DataFrame:

code prompt

Code

import pandas as pd
import matplotlib.pyplot as plt
def task_func(df: pd.DataFrame) -> pd.DataFrame:

entry point

task_func

libs

  • pandas
  • matplotlib

Discussion

Discussion

No discussion posts on this page yet. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

Source and history

Official source

initial import