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

Wrapping a check_input causes index out of range #79

Closed
mastersplinter opened this issue Jul 11, 2019 · 3 comments
Closed

Wrapping a check_input causes index out of range #79

mastersplinter opened this issue Jul 11, 2019 · 3 comments

Comments

@mastersplinter
Copy link
Collaborator

mastersplinter commented Jul 11, 2019

The following example:

from pandera import DataFrameSchema, Int, DateTime, String, Check, Column, Float, Bool, check_output, check_input
import pandas as pd
import numpy as np
from gsktools import load_data

df = pd.DataFrame({
        "column1": [10.0, 20.0, 30.0],
        "column2": [1, 2, 3],
    })
​
schema = DataFrameSchema({
        "column1": Column(Float),
        "column2": Column(Int),
    })

@check_input(schema,1)
def original_function(some_arg, df):
    return df

def wrapper(function_as_parameter,positional_arguments):
    df = function_as_parameter(positional_arguments)
    return df

def function_that_calls_original_function_via_wrapper(df):
    new_df = wrapper(
            function_as_parameter=original_function,
            positional_arguments=df)

function_that_calls_original_function_via_wrapper(df)

Results in this list index out of range error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-100-da3315e2c30b> in <module>
----> 1 function_that_calls_original_function_via_wrapper(df)

<ipython-input-99-0075cf7550ed> in function_that_calls_original_function_via_wrapper(df)
      2     new_df = wrapper(
      3             function_as_parameter=original_function,
----> 4             positional_arguments=df)

<ipython-input-98-c6fbf50f8087> in wrapper(function_as_parameter, positional_arguments)
      1 def wrapper(function_as_parameter,positional_arguments):
----> 2     df = function_as_parameter(positional_arguments)
      3     return df

/pandera_dev/pandera/pandera.py in _wrapper(fn, instance, args, kwargs)
    766                     zip(arg_spec_args, args))
    767                 args_dict[obj_getter] = schema.validate(args_dict[obj_getter])
--> 768                 args = list(args_dict.values())
    769         elif obj_getter is None:
    770             try:

IndexError: list index out of range
@mastersplinter mastersplinter changed the title Wrapping a check output causes a Wrapping a check_input causes index out of range Jul 11, 2019
@cosmicBboy
Copy link
Collaborator

cosmicBboy commented Jul 11, 2019

the check_input call should be @check_input(schema), or @check_input(schema, 0) since the second argument is 0-indexed (if obj_getter is an integer)

@cosmicBboy
Copy link
Collaborator

not sure if this is important, but positional args should also be a list/tuple and unpacked on function call

def wrapper(function_as_parameter, positional_arguments):
    df = function_as_parameter(*positional_arguments)
    return df

def function_that_calls_original_function_via_wrapper(df):
    new_df = wrapper(
            function_as_parameter=original_function,
            positional_arguments=(df, ))

@mastersplinter
Copy link
Collaborator Author

I need to investigate more, after playing with my toy example it's now fixed. I need to investigate my original code (which is much more complicated) and figure out if this is actually a pandera issue or not. Closing for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants