Skip to content

BUG: fixed-length string arrays are not recognised as bytes on instantiation #51994

Open
@gszep

Description

@gszep

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
x = np.array([b"123",b"e23423"], dtype="S")

df = pd.DataFrame({"a": x})
df.dtypes # sees x as object

df = pd.DataFrame({})
df["a"] = x
df.dtypes # correct behaviour

Issue Description

There is a bug in pandas that prevents fixed-length string arrays np.dtype("S") from being read properly. For some reason the dataframe does the casting properly only if the array is set after dataframe creation

Expected Behavior

It shouldn't matter whether you set the fixed-length array upon instantiation of the dataframe or afterwards

Installed Versions

INSTALLED VERSIONS ------------------ commit : f8a37a7 python : 3.10.4.final.0 python-bits : 64 OS : Linux OS-release : 6.2.0-76060200-generic Version : #202302191831~1677858327~22.04~3cea1be SMP PREEMPT_DYNAMIC Fri M machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 2.1.0.dev0+217.gf8a37a76c5
numpy : 1.23.5
pytz : 2022.6
dateutil : 2.8.2
setuptools : 65.3.0
pip : 23.0.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 : 8.7.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.3
snappy : None
sqlalchemy : None
tables : 3.7.0
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : None
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

BugCompatpandas objects compatability with Numpy or Python functionsConstructorsSeries/DataFrame/Index/pd.array ConstructorsDataFrameDataFrame data structure

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions