Skip to content

BUG: AttributeError: 'StringArray' object has no attribute 'tolist' #41134

@aiqc

Description

@aiqc
  • 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.

Code Sample, a copy-pastable example

$ pip show pandas

Name: pandas
Version: 1.2.4
import pandas as pd
import numpy as np

lzt = ["a","b","c"]
arr = np.array(lzt)
df = pd.DataFrame(arr).astype('string')

srs = df[0]
u = srs.unique()


type(u)
<class 'pandas.core.arrays.string_.StringArray'>


u.dtype == 'string'
True


u.tolist()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'StringArray' object has no attribute 'tolist'

Problem description

So I tried changing my code to use list(srs.unique()), but then all of my numeric dtypes become unserializable for JSON.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : 2cb9652 python : 3.7.6.final.0 python-bits : 64 OS : Darwin OS-release : 20.3.0 Version : Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64 machine : x86_64 processor : i386 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.2.4
numpy : 1.18.5
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 41.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.17.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 2.0.0
pyxlsb : None
s3fs : None
scipy : 1.5.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions