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.

TypeError for mixed-type indices in python3

See original GitHub issue

Code Sample, a copy-pastable example if possible

import pandas as pd
import numpy as np
table = pd.DataFrame(data=np.random.randn(5,2), index=[4,1,'foo',3,'bar'])
table = table.sort_index()

Problem description

The code above works in python2. However, in python3 a TypeError is raised:

TypeError: '<' not supported between instances of 'str' and 'int'

The reason for this is described here: mixed-type sequencing cannot be sorted anymore just like this.

Expected Output

No exception when calling sort_index in python3.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None

pandas: 0.22.0 pytest: None pip: 10.0.0 setuptools: 39.0.1 Cython: None numpy: 1.14.2 scipy: 1.0.1 pyarrow: None xarray: None IPython: 6.3.1 sphinx: None patsy: None dateutil: 2.7.2 pytz: 2018.4 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: 2.2.2 openpyxl: None xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: None html5lib: None sqlalchemy: None pymysql: None psycopg2: None jinja2: None s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mroeschkecommented, Apr 4, 2020

This looks like a duplicate of https://github.com/pandas-dev/pandas/issues/17010. Closing

0reactions
jrebackcommented, May 1, 2018

this is using pandas in a very non-idiomatic way as mixed types are not easily represented, except by object type. that said, the sorting mechanisms internally for object types could use safe_sort (see pandas.core.sorting) which handles correctly these mixed types cases

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas: how to use slicing for mixed-type multi-indices in ...
The solution works because pandas naturally can deal with mixed-type indices. It appears that only the string-based subset of indices needs ...
Read more >
TypeError: list indices must be integers or slices, not str
This type error occurs when indexing a list with anything other than integers or slices, as the error mentions. Usually, the most straightforward...
Read more >
Built-in Types — Python 3.11.1 documentation
Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is ......
Read more >
type and isinstance in Python - GeeksforGeeks
If class info is not a type or tuple of types, a TypeError exception is raised. Example 1: In this example, we will...
Read more >
Common Python Data Structures (Guide)
Python lists can hold arbitrary elements—everything is an object in Python, including functions. Therefore, you can mix and match different kinds of data ......
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