Beispiel #1
0
def for_date(date_string=None):
    """Return all data for a specific date."""
    date_string: Optional[str]

    date = (dt.datetime.fromisoformat(date_string).date()
            if date_string is not None else
            (dt.datetime.utcnow() - dt.timedelta(days=1)).date())

    result = df[df["Last Update"].map(lambda d: d.date()) == date]

    return to_records(result)
Beispiel #2
0
def get_all(
    date=None,
    country=None,
    state=None,
    min_date=None,
    max_date=None,
    countries=None,
    states=None,
    limit=None,
):
    """Fetch all data from John Hopkins."""

    result = df

    if date is not None:

        result = df[df["Last Update"].map(lambda d: d.date()) ==
                    dt.datetime.fromisoformat(date).date()]

    if country is not None:
        result = result[result["Country/Region"] == country]

    if state is not None:
        result = result[result["Province/State"] == state]

    if min_date is not None:
        result = result[result["Last Update"].map(lambda d: d.date()) >=
                        dt.datetime.fromisoformat(min_date).date()]

    if max_date is not None:
        result = result[result["Last Update"].map(lambda d: d.date()) <=
                        dt.datetime.fromisoformat(max_date).date()]

    if countries is not None:
        countries = (json.loads(countries)
                     if not isinstance(countries, list) else countries)
        result = result[result["Countries/Regions"].map(
            lambda s: s in countries)]

    if states is not None:
        states = json.loads(states) if not isinstance(states, list) else states
        result = result[result["Province/State"].map(lambda s: s in states)]

    records = to_records(result)

    if limit is not None:
        records = records[:int(limit)]

    return records
Beispiel #3
0
def states():
    """Return all states and provinces in the dataset."""
    return to_records(df[["Province/State",
                          "Country/Region"]].dropna(how="any"))
Beispiel #4
0
def get_all(
    date=None,
    country=None,
    state=None,
    min_date=None,
    max_date=None,
    countries=None,
    states=None,
    county=None,
    counties=None,
    limit=None,
):
    """Fetch all data from John Hopkins."""

    result = df

    if date is not None:

        result = df[
            df.datetime.map(lambda d: d.date())
            == dt.datetime.fromisoformat(date).date()
        ]

    if min_date is not None:
        result = result[
            result["datetime"].map(lambda d: d.date())
            >= dt.datetime.fromisoformat(min_date).date()
        ]

    if max_date is not None:
        result = result[
            result["datetime"].map(lambda d: d.date())
            <= dt.datetime.fromisoformat(max_date).date()
        ]

    assert not (
        country and countries
    ), "country and countries filters are mutually exclusive"

    if country or countries:
        countries = (
            [country]
            if country
            else (
                json.loads(countries)
                if not isinstance(countries, list)
                else countries
            )
        )

        result = result[result.country.map(lambda c: c in countries)]

    assert not (
        state and states
    ), "state and states filters are mutually exclusive"

    if state or states:
        states = (
            [state]
            if state
            else json.loads(states)
            if not isinstance(states, list)
            else states
        )

        result = result[result.state.map(lambda s: s in states)]

    assert not (
        county and counties
    ), "county and counties filters are mutually exclusive"

    if county or counties:
        counties = (
            [county]
            if county
            else json.loads(counties)
            if not isinstance(counties, list)
            else counties
        )

        result = result[result.county.map(lambda c: c in counties)]

    records = to_records(result)

    if limit is not None:
        records = records[: int(limit)]

    return records
Beispiel #5
0
def states():
    """Return all states and provinces in the dataset."""
    return to_records(df[["state", "country"]].dropna())