Description
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
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