pandas-dev / pandas-stubs

Public type stubs for pandas
BSD 3-Clause "New" or "Revised" License
232 stars 123 forks source link

Using Timestamp as label when indexing with [] gives typing error. #620

Open soylander opened 1 year ago

soylander commented 1 year ago

Describe the bug Pylance shows error when using a timestamp as label for selecting data from a pandas series.

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
    
    import pandas as pd

dt1 = pd.to_datetime('2023-05-01') dt2 = pd.to_datetime('2023-05-02') s = pd.Series([1, 2], index=[dt1, dt2]) s[dt1] # pylance error here s.loc[[dt1]] # pylance error here also



2. Indicate which type checker you are using (`mypy` or  `pyright`).
pyright

3. Show the error message received from that type checker while checking your example.

Argument of type "Timestamp" cannot be assigned to parameter "idx" of type "int | _str" in function "__getitem__"
  Type "Timestamp" cannot be assigned to type "int | _str"
    "Timestamp" is incompatible with "int"
    "Timestamp" is incompatible with "_str"Pylance[reportGeneralTypeIssues](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportGeneralTypeIssues)

**Please complete the following information:**
 - OS: MacOS Monterey 
 - OS Version: 12.3.1
 - python version: 3.10.8
 - version of type checker: pylance v2023.3.40
 - version of installed `pandas-stubs`: 1.5.3.230321

**Additional context**
Same issue arises when using timedelta instead of timestamp.
Dr-Irv commented 1 year ago

Thanks for the report.

We need to create some consistency between the indexing used in DataFrame._LocIndexerFrame.__getitem__(), Series.__getitem__() and Series._LocIndexerSeries.__getitem__() . If you use:

df = pd.DataFrame(s)
df.loc[dt1, :]
df.loc[[dt1], :]

that is accepted. If you use

df = pd.DataFrame({"x": s})
df.loc[dt1, "x"]
df.loc[[dt1], "x"]

you get typing errors.