benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 30f0531d-f3cd-5706-9ed5-c4fc55bfbee9

Problem

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

complete prompt

import requests
import pandas as pd


def task_func(url: str) -> pd.DataFrame:
    """
    This function fetches JSON data from a specified URL and converts it into a Pandas DataFrame.
    It expects the JSON to be in a format that is directly convertible to a DataFrame, typically
    a list of dictionaries. The function handles various scenarios including successful data
    retrieval and conversion, network issues, and invalid JSON format.

    Parameters:
    - url (str): The URL where the JSON file is located.

    Returns:
    - pd.DataFrame: A DataFrame constructed from the JSON data fetched from the URL.

    Raises:
    - SystemError: If there is a network-related issue such as a connection error, timeout,
      or if the server responded with an unsuccessful status code (like 404 or 500). This is a
      re-raised exception from requests.RequestException to provide a more specific error message.
    - ValueError: If the fetched data is not in a valid JSON format that can be converted into
      a DataFrame. This could occur if the data structure does not match the expected format (e.g.,
      not a list of dictionaries).

    Requirements:
    - requests
    - pandas

    Example:
    >>> task_func('https://example.com/data.json')
    DataFrame:
       A  B

    Notes:
    - The function uses a timeout of 5 seconds for the network request to avoid hanging indefinitely.
    - It checks the HTTP response status and raises an HTTPError for unsuccessful status codes.
    - Directly converts the HTTP response to JSON and then to a DataFrame, without intermediate processing.
    """

instruct prompt

This function fetches JSON data from a specified URL and converts it into a Pandas DataFrame. It expects the JSON to be in a format that is directly convertible to a DataFrame, typically a list of dictionaries. The function handles various scenarios including successful data retrieval and conversion, network issues, and invalid JSON format.
Note that: Notes: The function uses a timeout of 5 seconds for the network request to avoid hanging indefinitely. It checks the HTTP response status and raises an HTTPError for unsuccessful status codes. Directly converts the HTTP response to JSON and then to a DataFrame, without intermediate processing.
The function should raise the exception for: SystemError: If there is a network-related issue such as a connection error, timeout, or if the server responded with an unsuccessful status code (like 404 or 500). This is a re-raised exception from requests.RequestException to provide a more specific error message. ValueError: If the fetched data is not in a valid JSON format that can be converted into a DataFrame. This could occur if the data structure does not match the expected format (e.g., not a list of dictionaries).
The function should output with:
    pd.DataFrame: A DataFrame constructed from the JSON data fetched from the URL.
You should write self-contained code starting with:

Code

import requests
import pandas as pd
def task_func(url: str) -> pd.DataFrame:

code prompt

Code

import requests
import pandas as pd
def task_func(url: str) -> pd.DataFrame:

entry point

task_func

libs

  • pandas
  • requests

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