Open jonmooser opened 1 week ago
@jonmooser Thanks for raising this issue! If I understand it correctly, you are trying to change the dtype of a Series
that is already initialized.
I agree it is counter-intuitive that s2 = pd.Series(s, dtype=int)
ignores the customized dtype silently without raising.
However, I don't think the constructor should be used for casting. As you have mentioned, a better way of doing this might be Series.astype
, in which case a ValueError
is raised as expected:
>>> s = pd.Series([1, 2, 'x', 4, 5]) # dtype of s: object
>>> s3 = s.astype('int64')
ValueError: invalid literal for int() with base 10: 'x'
Thanks for looking into this. Glad there is a work around but it would be great if future versions are more consistent and intuitive.
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
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 behaviorExpected Behavior
In both of the cases above, it should throw a ValueError. Instead, only the first case does:
Installed Versions