Skip to content

ENH: assert_* has very superficial description if CategorialIndex are different #18056

Closed
@topper-123

Description

@topper-123

Code Sample, a copy-pastable example if possible

>>> c1 = pd.CategoricalIndex(['a', 'b'])
>>> c2 = pd.CategoricalIndex(['c', 'd'])
>>> s1 = pd.Series([1,2], index=c1)
>>> s2 = pd.Series([1,2], index=c2)
>>> pd.testing.assert_series_equal(s1, s2)
AssertionError: Series.index are different

Attribute "dtype" are different
[left]:  category
[right]: category

Note [left] and [right] output the same, so this help message isn't helpful.

Problem description

You can't see from the description where the differences in the CategoricalIndexes are. The reason is that Categorical return "category" for str(c1) and to see details, you need repr(c1).

Expected Output

The solution could be in the function util.testing.py::raise_assert_detail to replace

msg = """{obj} are different

{message}
[left]:  {left}
[right]: {right}""".format(obj=obj, message=message, left=left, right=right)

with the repr-formattes strings:

msg = """{obj} are different

{message}
[left]:  {left!r}
[right]: {right!r}""".format(obj=obj, message=message, left=left, right=right)

Note the (!r). This will give the full repr output which you'll almost always want anyway.

Alternatively, the decision to let str(c1) output "category" could be changed. That would be a breaking change, though, and would require proper deprecation warning.

I could submit a PR if the solution with !r format option is acceptable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Testingpandas testing functions or related to the test suite

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions