Skip to content

BUG: Printing a DataFrame containing user-defined object causes infinite loop #2978

@cxrodgers

Description

@cxrodgers

Version: pandas 0.10.0 , via pip
OS: Ubuntu 12.04 x64

Description:
I put a user-defined object (which defines only getitem) into a dataframe. Printing the dataframe causes the system to hang. I suspect that the construction of a text representation of the object is calling my object's getitem.

A simple code snippet to fully reproduce the problem:

import pandas
class A:
    def __getitem__(self, key):
        return 3 # obviously simplified
df = pandas.DataFrame([A()])
print df

Desired behavior: pandas should not be calling the getitem method of objects in DataFrame.

Running this script causes my computer to hang. Ctrl+C dumps a long traceback. Here is the first bit of it:

Traceback (most recent call last):
File "test.py", line 6, in
print df
File "/home/jack/.local/lib/python2.7/site-packages/pandas/core/frame.py", line 636, in str
return self.bytes()
File "/home/jack/.local/lib/python2.7/site-packages/pandas/core/frame.py", line 646, in bytes
return self.unicode().encode(encoding , 'replace')
File "/home/jack/.local/lib/python2.7/site-packages/pandas/core/frame.py", line 663, in unicode
self.to_string(buf=buf, line_width=line_width)
File "/home/jack/.local/lib/python2.7/site-packages/pandas/core/frame.py", line 1503, in to_string
formatter.to_string()
File "/home/jack/.local/lib/python2.7/site-packages/pandas/core/format.py", line 297, in to_string
strcols = self._to_str_columns()
File "/home/jack/.local/lib/python2.7/site-packages/pandas/core/format.py", line 251, in _to_str_columns
fmt_values = self._format_col(i)
File "/home/jack/.local/lib/python2.7/site-packages/pandas/core/format.py", line 379, in _format_col
space=self.col_space)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions