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

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()]

This is by design for now, I fear, but if you have to, you can silence mypy by slicing with a callable, like this:

import pandas as pd

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

print(df)
# Ouput
     col1
3.3   1.1

And so mypy reports no issues found on this code.