Skip to content

BUG: Numpy deprecation warning when constructing Series from numpy.datetime64 value #22741

Closed
@fjetter

Description

@fjetter

Code Sample, a copy-pastable example if possible

# Starting the interpreter with `python -Werror::DeprecationWarning:pandas.core.dtypes.cast`

>>> import numpy as np
>>> import pandas as pd
>>> pd.Series("2018-03-08", index=pd.RangeIndex(stop=1), dtype="datetime64[ns]")
0   2018-03-08
dtype: datetime64[ns]
>>> pd.Series(np.datetime64("2018-03-08"), index=pd.RangeIndex(stop=1), dtype="datetime64[ns]")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "$VIRTUAL_ENV/local/lib/python2.7/site-packages/pandas/core/series.py", line 275, in __init__
    raise_cast_failure=True)
  File "$VIRTUAL_ENV/local/lib/python2.7/site-packages/pandas/core/series.py", line 4132, in _sanitize_array
    subarr = _try_cast(data, False)
  File "$VIRTUAL_ENV/local/lib/python2.7/site-packages/pandas/core/series.py", line 4044, in _try_cast
    subarr = maybe_cast_to_datetime(arr, dtype)
  File "$VIRTUAL_ENV/local/lib/python2.7/site-packages/pandas/core/dtypes/cast.py", line 1021, in maybe_cast_to_datetime
    if value == iNaT or isna(value):
DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.

Problem description

The above code triggers a numpy DeprecationWarning when using a numpy scalar value as input to the series.

Expected Output

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.13.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.60-linuxkit-aufs
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.4
pytest: None
pip: 10.0.1
setuptools: 40.0.0
Cython: None
numpy: 1.14.5
scipy: None
pyarrow: 0.10.0
xarray: None
IPython: 5.7.0
sphinx: 1.6.7
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: 1.2.9
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeprecateFunctionality to remove in pandasNumeric OperationsArithmetic, Comparison, and Logical operations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions