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

@pa.parser does not work for Index fields #1684

Open
2 of 3 tasks
Girmii opened this issue Jun 13, 2024 · 0 comments
Open
2 of 3 tasks

@pa.parser does not work for Index fields #1684

Girmii opened this issue Jun 13, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@Girmii
Copy link

Girmii commented Jun 13, 2024

Describe the bug
A clear and concise description of what the bug is.

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandera.
  • (optional) I have confirmed this bug exists on the main branch of pandera.

Parser functions do not get called for fields typed as Index, although the documentation states: "Defines DataFrameModel parse methods for columns/indexes.".

Incidentally, I would not expect the parser for field B to be called twice, although the end result is correct.

Code Sample, a copy-pastable example

import pandas as pd
import pandera as pa
from pandera.typing import Index, Series


class TestModel(pa.DataFrameModel):
    A: Index[int]
    B: Series[int]
    
    class Config:
        coerce = True

    @pa.parser("A")
    def double(cls, series: pd.Series) -> pd.Series:
        print("parsing A")
        return series * 2

    @pa.parser("B")
    def triple(cls, series: pd.Series) -> pd.Series:
        print("parsing B")
        return series * 3


df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]})
df = DataFrame[TestModel](df)
print(df)

df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]})
df = df.set_index("A")  # Does not change the result
df = DataFrame[TestModel](df)
print(df)

Output

parsing B
parsing B

   A  B
0  1  3
1  2  6
2  3  9


parsing B
parsing B

   B
A   
1  3
2  6
3  9

Expected behavior

I would expect the parser function for 'A' to be applied to its respective Series.

Versions:

  • Python 3.11.0
  • pandas==2.2.0
  • pandas-stubs==2.1.4.231227
  • pandera==0.19.3
  • mypy 1.10.0 (compiled: yes)
@Girmii Girmii added the bug Something isn't working label Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant