Skip to content

BUG: dtype keyword in Series constructor ignored for Series input in certain cases #59060

@jonmooser

Description

@jonmooser

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

try:
    s = pd.Series([1, 2, 'x', 4, 5], dtype=int)
    print("1. No exception")
except Exception as ex:
    print("1. Exception thrown", ex)

s = pd.Series([1, 2, 'x', 4, 5])
try:
    s2 = pd.Series(s, dtype=int)
    print("2. No exception")
except Exception as ex:
    print("2. Exception thrown", ex)

print("s2 dtype", s2.dtype)

Issue Description

The behavior of the above code doesn't seem right.
If I try to create a Series with dtype=int using a list that contains a string, it throws a ValueError, as expected.

But if the input to the Series constructor is another Series that contains a string, there is no exception and it quietly returns a new Series with dtype=object.

Is this expected? If so it's very counterintuitive. Perhaps the recommended way is
s.astype(int)
but it seems like this should produce the same behavior

Expected Behavior

In both of the cases above, it should throw a ValueError. Instead, only the first case does:

1. Exception thrown invalid literal for int() with base 10: 'x'
1. No exception
s2 dtype object

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.11.6.final.0
python-bits : 64
OS : Darwin
OS-release : 23.1.0
Version : Darwin Kernel Version 23.1.0: Mon Oct 9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.2
numpy : 1.23.4
pytz : 2022.5
dateutil : 2.8.2
setuptools : 65.5.0
pip : 24.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 : 3.1.2
IPython : 8.12.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.13.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugClosing CandidateMay be closeable, needs more eyeballsNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions