Skip to content

Dont do selection by callable if not wanted #13558

Closed
@Chaoste

Description

@Chaoste

Copy-pastable Code Sample

import pandas as pd
import pandas.util.testing as tm

class myClass1:
    def __call__(self):
        return "Result 1"
    def __str__(self):
        return "Class 1"

class myClass2:
    def __call__(self):
        return "Result 2"
    def __str__(self):
        return "Class 2"

obj1 = myClass1()
obj2 = myClass2()
df = pd.DataFrame([], index=[0], columns=[obj1, obj2])
 # Throws error because new pandas version wants to select by callable
df[obj1] = obj1()
df[obj2] = obj2()

Expected Output

expected = pd.DataFrame([["Result 1", "Result 2"]], columns=[obj1, obj2])
tm.assert_frame_equal(df, expected)

How I fixed the test for the moment

class myClass1:
    def __call__(self, *args):
        if args:
            return self
        return "Result 1"
    def __str__(self):
        return "Class 1"

class myClass2:
    def __call__(self, *args):
        if args:
            return self
        return "Result 2"
    def __str__(self):
        return "Class 2"

If pandas invokes a callable object it passes the frame itself as a parameter. Because i dont need arguments for my call function i can prevent this call by checking if a argument is passed. Is there a better way of doing this or can you add an option so i can disable the new behavior.

output of pd.show_versions()

commit: None
python: 3.4.3.final.0
python-bits: 32
OS: Windows
OS-release: 8
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.18.1
nose: None
pip: 8.1.2
setuptools: 19.6.2
Cython: None
numpy: 1.11.0
scipy: 0.16.1
statsmodels: None
xarray: None
IPython: 4.2.0
sphinx: 1.4.1
patsy: None
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions