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

ENH?: __repr__ method for Index objects #5120

Closed
dengemann opened this issue Oct 5, 2013 · 13 comments
Closed

ENH?: __repr__ method for Index objects #5120

dengemann opened this issue Oct 5, 2013 · 13 comments
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Output-Formatting __repr__ of pandas objects, to_string
Milestone

Comments

@dengemann
Copy link
Contributor

It often happens that I print a MultiIndex object (whether on purpose or not) which leads to flooding my interpreter and makes it unresponsive for a couple of seconds since often the length of my indexers exceeds 500-1000k. I was thinking about adding a neat __repr__ for Indexers that summarizes levels + length or just omits what's in the middle once a certain length is exceeded. It seems this has not been tackled so far when looking at the current master, but please feel free to close this issue if this is a dupe (checked but did not find anything related). Any pointers are welcome.

@jtratner
Copy link
Contributor

jtratner commented Oct 5, 2013

I think you need to fix the Index representation of itself instead, because levels and labels are just Index objects that are getting pprinted in an extremely long way, here's the relevant code to intercept:

    def __repr__(self):
        encoding = get_option('display.encoding')
        attrs = [('levels', default_pprint(self.levels)),
                 ('labels', default_pprint(self.labels))]
        if not all(name is None for name in self.names):
            attrs.append(('names', default_pprint(self.names)))
        if self.sortorder is not None:
            attrs.append(('sortorder', default_pprint(self.sortorder)))

If you want to put something together that shortens extremely long indexes, you just need to alter FrozenNDArray.__str__ (and/or __repr__) in pandas/core/base.py. (because MI repr just pprints levels, labels, etc). There's been previous discussion about maximum sequence length that would go into this too.

@dengemann
Copy link
Contributor Author

Thanks @jtratner I spotted this in index.py --- actually my idea was to modify this one. Thanks for the FrozenNDArray hint. I'm happy to look into this if the idea is appreciated ;-)

@jtratner
Copy link
Contributor

jtratner commented Oct 5, 2013

I think the correct config option is display.max_seq_items. You can probably just pprint slices to make it match the length correctly.

@cpcloud
Copy link
Member

cpcloud commented Oct 5, 2013

fwiw you'll also be able adjust the "edge" items once i push that PR

@jreback
Copy link
Contributor

jreback commented Oct 8, 2013

@jtratner @cpcloud close this?

@jtratner
Copy link
Contributor

jtratner commented Oct 8, 2013

I think it's reasonable. It just needs to be changed to what it actually is
"Make Index repr respect max_seq_len" or whatever the appropriate
option is.

@dengemann
Copy link
Contributor Author

If we're not in a hurry I'm happy to look into this. Won't be before the weekend though, no time ....

@jtratner
Copy link
Contributor

jtratner commented Oct 8, 2013

Push the milestone to 0.14. If you finish earlier we can incorporate it.

@ghost
Copy link

ghost commented Dec 19, 2013

xref #3391 , #3347

@cancan101
Copy link
Contributor

Also see #5734 I think

@jreback
Copy link
Contributor

jreback commented Dec 20, 2013

@y-p I belive this is should be closed?
@jtratner (because of fix to max_seq_items)

@ghost
Copy link

ghost commented Dec 20, 2013

yep, I just tested post-#5753 and it limits the multiindex output (to something large) which
doesn't hang the terminal.
Also see that #4935 handled the issues I raised in #3347 a while back. great.

@ghost ghost closed this as completed Dec 20, 2013
@jreback
Copy link
Contributor

jreback commented Dec 20, 2013

@y-p gr8 thanks!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

No branches or pull requests

5 participants