Skip to content
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

ENH: DataFrame argument columns should accept dict/iterable type #57798

Closed
1 of 3 tasks
5j9 opened this issue Mar 10, 2024 · 8 comments
Closed
1 of 3 tasks

ENH: DataFrame argument columns should accept dict/iterable type #57798

5j9 opened this issue Mar 10, 2024 · 8 comments
Labels
Enhancement Typing type annotations, mypy/pyright type checking

Comments

@5j9
Copy link
Contributor

5j9 commented Mar 10, 2024

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

It is convenient to pass a dict to columns argument of DataFrame and use the same dict for astype conversions later. The following sample code works fine, but it fails type checking:

from pandas import DataFrame

columns = {"A": 'int64', "B": 'int32', "C": 'int16'}
df = DataFrame([[1,2,3],[4,5,6]], columns=columns)  # fails type check
df = df.astype(columns)

print(df.dtypes)

pyright output:

$ pyright 'filename.py'
filename.py
  filename.py:4:43 - error: Argument of type "dict[str, str]" cannot be assigned to parameter "columns" of type "Axes | None" in function "__init__"
    Type "dict[str, str]" cannot be assigned to type "Axes | None"
      "dict[str, str]" is incompatible with "ExtensionArray"
      "dict[str, str]" is incompatible with "ndarray[Unknown, Unknown]"
      "dict[str, str]" is incompatible with "Index"
      "dict[str, str]" is incompatible with "Series"
      "dict[str, str]" is incompatible with protocol "SequenceNotStr[Unknown]"
        "index" is not present
        "count" is not present
    ... (reportArgumentType)
1 error, 0 warnings, 0 informations

Feature Description

Change type annotations for columns to accept dict/iterable type.

Alternative Solutions

Convert columns value to a Series or numpy array or some other compatible type.

Additional Context

No response

@5j9 5j9 added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 10, 2024
@asishm
Copy link
Contributor

asishm commented Mar 10, 2024

do you have pandas-stubs installed?

this only reproduces without pandas-stubs for me

@5j9
Copy link
Contributor Author

5j9 commented Mar 10, 2024

do you have pandas-stubs installed?

this only reproduces without pandas-stubs for me

I don't have pandas-stubs. You are right, works fine with stubs. I still think this should not raise error even without stubs given that pandas-stubs describes itself as "narrower than what is possibly allowed by pandas".

@twoertwein
Copy link
Member

Would need to adjust

columns: Axes | None = None,

to include Mapping.

@twoertwein twoertwein added Typing type annotations, mypy/pyright type checking and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 12, 2024
@phofl
Copy link
Member

phofl commented Mar 18, 2024

It is completely conicidential that this works and nobody should rely on it. Closing

@phofl phofl closed this as completed Mar 18, 2024
@twoertwein
Copy link
Member

It is completely conicidential that this works and nobody should rely on it. Closing

@Dr-Irv pandas-stubs currently declares that any dict would be okay

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Mar 18, 2024

It is completely conicidential that this works and nobody should rely on it. Closing

@phofl So should we test for a dict and then raise an Exception?

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Mar 18, 2024

It is completely conicidential that this works and nobody should rely on it. Closing

@Dr-Irv pandas-stubs currently declares that any dict would be okay

I checked via Blame and I put that in there at some point without a test. I just tested removing dict from Axes in pandas-stubs and it works fine, so maybe we should remove it??

@Dr-Irv
Copy link
Contributor

Dr-Irv commented Mar 18, 2024

I don't have pandas-stubs. You are right, works fine with stubs. I still think this should not raise error even without stubs given that pandas-stubs describes itself as "narrower than what is possibly allowed by pandas".

Yes, but the typing in the pandas source is much wider than what is actually allowed by pandas. Also, type checking is faster using the stubs than using the pandas source.

kkirss added a commit to kkirss/traveller-book-parser that referenced this issue May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

No branches or pull requests

5 participants