Skip to content

BUG: pandas.Series.unique() does not return correct unique values on \u string #45929

Open
@cbhushan

Description

@cbhushan

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

data_list = [
    '1 \udcd6a NY',
    '2 \udcd6b NY',
    '3 \ud800c NY',
    '4 \udcd6d NY',
    '5 \udcc3e NY',]

ss = pd.Series(data_list)
print(f'len(ss.unique()): {len(ss.unique())}')
print(f'len(ss.value_counts()): {len(ss.value_counts())}')
print(f'len(np.unique(data_list): {len(np.unique(data_list))}')
print(f'len(set(data_list)): {len(set(data_list))}')
print(f'ss.is_unique: {ss.is_unique}')

Issue Description

pandas.Series.unique() does not return correct unique values when working with non-standard strings. In above example data_list clearly has 5 unique elements but ss.unique() returns an array of length-1. ss.is_unique is also wrong (it should be True). Surprising, ss.value_counts() seems to work . In contrast, np.unique and set() works as expected.

Above is a small reproducible example, I came across this issue while working with filenames with non-ascii characters on a Linux system.

See Output below:

len(ss.unique()): 1
len(ss.value_counts()): 5
len(np.unique(data_list): 5
len(set(data_list)): 5
ss.is_unique: False

Expected Behavior

All of the length should equal 5 and ss.is_unique should return True.

Installed Versions

pd.show_versions()

INSTALLED VERSIONS

commit : bb1f651
python : 3.8.6.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-154-generic
Version : #161-Ubuntu SMP Fri Jul 30 13:04:17 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.0
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 51.1.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Metadata

Metadata

Assignees

Labels

AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffBugStringsString extension data type and string data

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions