-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
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
pd.array([1, None], dtype=str)
# <NumpyExtensionArray>
# ['1', 'None']
# Length: 2, dtype: str128
pd.Series([1, None], dtype=str).array
# <NumpyExtensionArray>
# ['1', None]
# Length: 2, dtype: object
pd.DataFrame([1, None], dtype=str)[0].array
# <NumpyExtensionArray>
# ['1', None]
# Length: 2, dtype: objectIssue Description
While pd.Series returns object dtype when including None value in data for dtype=str, pd.array convert it to "None" string value. I am not sure this is intended or already discussed, but seems curious.
As investigating in construction process, pd.array pass dtype as is (str) but pd.Sereis call (via lib.ensure_string_array) dtype as "object"
using pd.array
# https://github.com/pandas-dev/pandas/blob/v2.2.1/pandas/core/arrays/numpy_.py#L127
result = np.asarray(scalars, dtype=dtype)using pd.Sereis
# https://github.com/pandas-dev/pandas/blob/v2.2.1/pandas/core/construction.py#L800-L802
# lib.ensure_string_array(arr, convert_na_value=False, copy=copy).reshape(shape)
# https://github.com/pandas-dev/pandas/blob/v2.2.1/pandas/_libs/lib.pyx#L766
result = np.asarray(arr, dtype="object")Expected Behavior
Although there might be historical reasons behind this divergent behavior, striving for consistency between pd.array and pd.Series for dtype=str would be more intuitive.
Installed Versions
INSTALLED VERSIONS
commit : bdc79c1
python : 3.11.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.133.1-microsoft-standard-WSL2
Version : #1 SMP Thu Oct 5 21:02:42 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.2.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 : 3.1.3
IPython : 8.22.1
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.2.0
gcsfs : 2024.2.0
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : 2.0.27
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None