Skip to content

BUG: columns on existing DataFrame does not accept "List[str]" #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bashtage opened this issue Jul 2, 2022 · 3 comments
Closed

BUG: columns on existing DataFrame does not accept "List[str]" #73

bashtage opened this issue Jul 2, 2022 · 3 comments
Labels
mypy bug Requires mypy to fix a bug

Comments

@bashtage
Copy link
Contributor

bashtage commented Jul 2, 2022

import pandas as pd
df = pd.DataFrame({"a":[1,2,3],"b":[0.0,1,1]})

df.columns= ["c", "d"]  # mypy error, columns is `Index` so cannot take List[str}
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jul 4, 2022

This is a bug in mypy: python/mypy#3004

Here's a simple example:

from typing import Union


class Foobar:
    def __init__(self, x: Union[int, str]):
        self._x = self.makestr(x)

    def makestr(self, x: Union[int, str]) -> str:
        if isinstance(x, int):
            return str(x)
        elif isinstance(x, str):
            return x
        else:
            raise Exception("invalid type")

    @property
    def x(self) -> str:
        return self._x

    @x.setter
    def x(self, x: Union[int, str]) -> None:
        self._x = self.makestr(x)


fb = Foobar("abc")
fb.x = 3

mypy will then report:

mypyasym.py:26: error: Incompatible types in assignment (expression has type "int", variable has type "str")

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jan 27, 2025

mypy has fixed this bug in python/mypy#18510, so once that is released we can remove the # type: ignore comments in test_frame.py:test_set_columns(). Seems to be OK in this version /mypy-1.16.0%2Bdev.42e005c999d8341c0da6d7b93b10d05f2db2099 so we'll have to keep an eye out for the version 1.16 release.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Jun 3, 2025

closed via #1236

@Dr-Irv Dr-Irv closed this as completed Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mypy bug Requires mypy to fix a bug
Projects
None yet
Development

No branches or pull requests

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy