question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Series not honoring class __repr__ or __str__

See original GitHub issue

Code Sample, a copy-pastable example if possible

class foo(dict):
    def __init__(self, iterable=None, **kwargs):
        if iterable is None:
            iterable = ()
        super(foo, self).__init__(iterable)
        self.update(kwargs)
    def __repr__(self):
        return ",".join(self.keys())
    def __str__(self):
        return ",".join(self.keys())

f = foo({'alpha' : 'b',
    'beta' : 'c'})

import pandas as pd
pd.DataFrame(data=[['A', 1, f]], columns=['D', 'F', 'G'])

Problem description

For a given series with a custom object, I want to control the content when displayed via print or displaying on ipython notebooks. The object foo is a simple class that have the __str__ and __repr__ overwritten, but still displays the object’s dictionary content, not the view I want to show the end users. How do I control that?

Expected Output

alpha,beta

what I get is:

{'alpha': 'b', 'beta': 'c'}

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line] INSTALLED VERSIONS

commit: None python: 3.5.4.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.21.1 pytest: 3.3.1 pip: 9.0.1 setuptools: 38.2.4 Cython: None numpy: 1.11.2 scipy: 0.18.1 pyarrow: None xarray: None IPython: 5.3.0 sphinx: 1.6.3 patsy: 0.4.1 dateutil: 2.6.1 pytz: 2017.3 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: 1.5.3 openpyxl: None xlrd: 1.1.0 xlwt: 1.3.0 xlsxwriter: None lxml: None bs4: 4.6.0 html5lib: 0.999 sqlalchemy: None pymysql: None psycopg2: None jinja2: 2.9.6 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lgharibashvilicommented, Apr 15, 2021

Quick dirty patch for those who cannot wait for the fix:

from pandas.io.formats import printing as pd_printing
pd_printing.is_sequence = lambda obj: False
1reaction
achapkowskicommented, Dec 19, 2017

@TomAugspurger not subclassing from dict is not an option since the other classes are established. Is there a way to override the print function (not optimal) or set something on the class to say hey use the __repr__

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why should we use repr() or str() to print object attributes ...
I just came across the dunnder repr() and str() methods. The instructor told me they are generally used to print object attributes. But...
Read more >
str() vs repr() in Python - GeeksforGeeks
str () is used for creating output for end user while repr() is mainly used for debugging and development. repr's goal is to...
Read more >
repr() vs str() in Python - TutorialsTeacher
The object class is the base class for all classes, has a number of dunder (short for double underscore) methods.
Read more >
Python __str__() and __repr__() functions | DigitalOcean
You should always use str() and repr() functions, which will call the underlying __str__ and __repr__ functions. It's not a good idea to...
Read more >
Solved Write a method __repr__(self) that returns a | Chegg.com
Remember that the __repr__ method should create a single string and return it. You should not use the print function in this method....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found