Skip to content

Series constructor with missing values and int dtype fails or passes depending on data type #22585

Closed
@TomAugspurger

Description

@TomAugspurger

I'm not sure what the expected behavior is, but these should match

In [16]: pd.Series([1, 2, np.nan], dtype=int)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-f0075752a7f2> in <module>()
----> 1 pd.Series([1, 2, np.nan], dtype=int)

~/sandbox/pandas/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    280             else:
    281                 data = _sanitize_array(data, index, dtype, copy,
--> 282                                        raise_cast_failure=True)
    283
    284                 data = SingleBlockManager(data, index, fastpath=True)

~/sandbox/pandas/pandas/core/series.py in _sanitize_array(data, index, dtype, copy, raise_cast_failure)
   4154         if dtype is not None:
   4155             try:
-> 4156                 subarr = _try_cast(data, False)
   4157             except Exception:
   4158                 if raise_cast_failure:  # pragma: no cover

~/sandbox/pandas/pandas/core/series.py in _try_cast(arr, take_fast_path)
   4087             # that we can convert the data to the requested dtype.
   4088             if is_float_dtype(dtype) or is_integer_dtype(dtype):
-> 4089                 subarr = maybe_cast_to_integer_array(arr, dtype)
   4090
   4091             subarr = maybe_cast_to_datetime(arr, dtype)

~/sandbox/pandas/pandas/core/dtypes/cast.py in maybe_cast_to_integer_array(arr, dtype, copy)
   1341     try:
   1342         if not hasattr(arr, "astype"):
-> 1343             casted = np.array(arr, dtype=dtype, copy=copy)
   1344         else:
   1345             casted = arr.astype(dtype, copy=copy)

ValueError: cannot convert float NaN to integer

and when data is an array, we end up with float dtype.

In [17]: pd.Series(np.array([1, 2, np.nan]), dtype=np.int64)
Out[17]:
0    1.0
1    2.0
2    NaN
dtype: float64

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugConstructorsSeries/DataFrame/Index/pd.array ConstructorsDtype ConversionsUnexpected or buggy dtype conversionsMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions