TypeError for mixed-type indices in python3
See original GitHub issueCode 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:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This looks like a duplicate of https://github.com/pandas-dev/pandas/issues/17010. Closing
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
(seepandas.core.sorting
) which handles correctly these mixed types cases