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

BUG: repr of Categorical does not distinguish int and str. #33676

Closed
3 tasks done
simonjayhawkins opened this issue Apr 20, 2020 · 4 comments · Fixed by #34222
Closed
3 tasks done

BUG: repr of Categorical does not distinguish int and str. #33676

simonjayhawkins opened this issue Apr 20, 2020 · 4 comments · Fixed by #34222
Labels
API Design Bug Categorical Categorical Data Type Needs Discussion Requires discussion from core team before further action Output-Formatting __repr__ of pandas objects, to_string
Milestone

Comments

@simonjayhawkins
Copy link
Member

simonjayhawkins commented Apr 20, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

>>> pd.Categorical([1, "2", 3, 4])
[1, 2, 3, 4]
Categories (4, object): [1, 3, 4, 2]

Problem description

does not show the string elements quoted. This is inconsistent with numpy.

>>> np.array([1, "2", 3, 4], dtype="object")
array([1, '2', 3, 4], dtype=object)

Expected Output

>>> pd.Categorical([1, "2", 3, 4])
[1, '2', 3, 4]
Categories (4, object): [1, 3, 4, '2']

Output of pd.show_versions()

[paste the output of pd.show_versions() here leaving a blank line after the details tag]

@simonjayhawkins simonjayhawkins added Bug Needs Triage Issue that has not been reviewed by a pandas team member Categorical Categorical Data Type Output-Formatting __repr__ of pandas objects, to_string and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 20, 2020
@MarcoGorelli
Copy link
Member

Does this mean that some of the existing tests need to change?

For example

    def test_unicode_print(self):
        c = Categorical(["aaaaa", "bb", "cccc"] * 20)
        expected = """\
[aaaaa, bb, cccc, aaaaa, bb, ..., bb, cccc, aaaaa, bb, cccc]
Length: 60
Categories (3, object): [aaaaa, bb, cccc]"""

        assert repr(c) == expected

should become

    def test_unicode_print(self):
        c = Categorical(["aaaaa", "bb", "cccc"] * 20)
        expected = """\
['aaaaa', 'bb', 'cccc', 'aaaaa', 'bb', ..., 'bb', 'cccc', 'aaaaa', 'bb', 'cccc']
Length: 60
Categories (3, object): ['aaaaa', 'bb', 'cccc']"""

        assert repr(c) == expected

@simonjayhawkins
Copy link
Member Author

Does this mean that some of the existing tests need to change?

Maybe more discussion needed, but IMO the current representation of the string categories without quotes is a bug and not pythonic. However, it is also a breaking change. need to see what others think as I don't know the history.

@simonjayhawkins simonjayhawkins added API Design Needs Discussion Requires discussion from core team before further action labels May 17, 2020
@MarcoGorelli
Copy link
Member

MarcoGorelli commented May 17, 2020

This isn't limited to Categorical though, is it? Should

>>> pd.DataFrame(['a', 'b', 2])
   0
0  a
1  b
2  2

become

>>> pd.DataFrame(['a', 'b', 2])
   0
0 'a'
1 'b'
2  2

?

@simonjayhawkins
Copy link
Member Author

In the OP, the values are shown inside square brackets, so for me, this implies a list literal which I would expect could be copy and pasted. I do find it frustrating that the reprs of many pandas objects cannot be used to create an object, but that's another issue.

For the Series case, the values are not shown inside square brackets, so the strings shown without quotes is more acceptable.

@jreback jreback added this to the 1.1 milestone Jun 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Bug Categorical Categorical Data Type Needs Discussion Requires discussion from core team before further action Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants