Skip to content

BUG: DataFrame argument columns fails to type-check when passed a list of strings (pandas 2.2.0) #56995

@eachimei

Description

@eachimei

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

# content of example.py:
from pandas import DataFrame

DataFrame([[1,2,3],[4,5,6]], columns=["A", "B", "C"])  # fails type check

data = {'row_1': [3, 2, 1, 0], 'row_2': ['a', 'b', 'c', 'd']}
DataFrame.from_dict(data, orient='index', columns=['A', 'B', 'C', 'D'])  # also fails type check


# then run `pyright` type checker:

$ pyright example.py
example.py
  example.py:3:38 - error: Argument of type "list[str]" cannot be assigned to parameter "columns" of type "Axes | None" in function "__init__"
    Type "list[str]" cannot be assigned to type "Axes | None"
      "list[str]" is incompatible with "ExtensionArray"
      "list[str]" is incompatible with "ndarray[Unknown, Unknown]"
      "list[str]" is incompatible with "Index"
      "list[str]" is incompatible with "Series"
      "list[str]" is incompatible with protocol "SequenceNotStr[Unknown]"
        "index" is an incompatible type
          Type "(__value: str, __start: SupportsIndex = 0, __stop: SupportsIndex = sys.maxsize, /) -> int" cannot be assigned to type "(value: Any, /, start: int = 0, stop: int = ...) -> int"
    ... (reportGeneralTypeIssues)
  c:\temp\example.py:6:51 - error: Argument of type "list[str]" cannot be assigned to parameter "columns" of type "Axes | None" in function "from_dict"
    Type "list[str]" cannot be assigned to type "Axes | None"
      "list[str]" is incompatible with "ExtensionArray"
      "list[str]" is incompatible with "ndarray[Unknown, Unknown]"
      "list[str]" is incompatible with "Index"
      "list[str]" is incompatible with "Series"
      "list[str]" is incompatible with protocol "SequenceNotStr[Unknown]"
        "index" is an incompatible type
          Type "(__value: str, __start: SupportsIndex = 0, __stop: SupportsIndex = sys.maxsize, /) -> int" cannot be assigned to type "(value: Any, /, start: int = 0, stop: int = ...) -> int"
    ... (reportGeneralTypeIssues)
2 errors, 0 warnings, 0 informations

Issue Description

Although the documentation shows that columns argument accepts a list of strings, the declared type annotation fails on pyright type check. This issue showed up on pandas version 2.2.0

Examples from the docs:
image
image

Expected Behavior

Passing a list of strings to the columns argument is expected to pass type check.

Installed Versions

INSTALLED VERSIONS

commit : f538741
python : 3.10.11.final.0
python-bits : 64
OS : Windows
OS-release : 10
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.2.0
numpy : 1.26.1
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.1
Cython : None
pytest : 7.4.3
hypothesis : None
sphinx : 7.2.6
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.17.2
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

pyright version: 1.1.347

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugClosing CandidateMay be closeable, needs more eyeballsTypingtype annotations, mypy/pyright type checking

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions