[Python] Pandas DataFrame mypy error: slice index must be an integer or None

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    The following line

    pd.DataFrame({"col1": [1.1, 2.2]}, index=[3.3, 4.4])[2.5:3.5]


    raises a mypy linting error of on the [2.5


    Slice index must be an integer or None
    ​

    This is valid syntax and correctly returns

    col1
    3.3 1.1


    Without # type: ignore, how can I resolve this linting error?

    versions:

    • pandas 1.3.0
    • mypy 0.931

    The code in question:

    def get_dataframe(
    ts_data: GroupTs,
    ts_group_name: str,
    start_time: Optional[float] = None,
    end_time: Optional[float] = None,
    ) -> pd.DataFrame:
    df = pd.DataFrame(ts_data.group[ts_group_name].ts_dict)[
    start_time:end_time
    ].interpolate(
    method="index", limit_area="inside"
    ) # type: pd.DataFrame
    return df[~df.index.duplicated()]

    Continue reading...

Compartilhe esta Página