cudf icon indicating copy to clipboard operation
cudf copied to clipboard

[BUG] Support typecasting to np.datetime64 and automatically cast to nanosecond resolution

Open xiaonans opened this issue 7 years ago • 4 comments

Describe the bug KeyError: dtype('<M8') occurred when I tried to convert a string column to datetime64.

The whole error messages: KeyError Traceback (most recent call last) in 1 gdf=cudf.DataFrame({"a":["2014-01-26"]}) ----> 2 gdf["a"]=gdf["a"].astype(np.datetime64)

/opt/conda/envs/rapids/lib/python3.6/site-packages/cudf/core/series.py in astype(self, dtype, **kwargs) 1441 return self 1442 -> 1443 return self._copy_construct(data=self._column.astype(dtype, **kwargs)) 1444 1445 def argsort(self, ascending=True, na_position="last"):

/opt/conda/envs/rapids/lib/python3.6/site-packages/cudf/core/column/column.py in astype(self, dtype, **kwargs) 929 930 elif np.issubdtype(dtype, np.datetime64): --> 931 return self.as_datetime_column(dtype, **kwargs) 932 933 else:

/opt/conda/envs/rapids/lib/python3.6/site-packages/cudf/core/column/string.py in as_datetime_column(self, dtype, **kwargs) 561 562 def as_datetime_column(self, dtype, **kwargs): --> 563 return self.as_numerical_column(dtype, **kwargs) 564 565 def as_string_column(self, dtype, **kwargs):

/opt/conda/envs/rapids/lib/python3.6/site-packages/cudf/core/column/string.py in as_numerical_column(self, dtype, **kwargs) 544 kwargs.update({"devptr": out_ptr}) 545 --> 546 _str_to_numeric_typecast_functions[str_dtype](self.str(), **kwargs) 547 548 out_col = column.as_column(out_arr)

KeyError: dtype('<M8')

Steps/Code to reproduce bug import numpy as np import cudf gdf=cudf.DataFrame({"a":["2014-01-26"]}) gdf["a"]=gdf["a"].astype(np.datetime64)

Expected behavior No error shoud occur.

Environment overview (please complete the following information)

  • Environment location: Bare-metal
  • Method of cuDF install: Docker
    • If method of install is [Docker], provide docker pull & docker run commands used cudf.version 0.10.0 docker pull rapidsai/rapidsai:cuda10.0-runtime-centos7 docker run --runtime=nvidia
      --rm -it
      -e NVIDIA_VISIBLE_DEVICES=1
      --network=host
      -v /home/$USER/projects/rapids:/rapids/mine
      rapidsai/rapidsai:cuda10.0-runtime-centos7

Environment details

Click here to see environment details
 **git***
 Not inside a git repository
 
 ***OS Information***
 CentOS Linux release 7.7.1908 (Core)
 NAME="CentOS Linux"
 VERSION="7 (Core)"
 ID="centos"
 ID_LIKE="rhel fedora"
 VERSION_ID="7"
 PRETTY_NAME="CentOS Linux 7 (Core)"
 ANSI_COLOR="0;31"
 CPE_NAME="cpe:/o:centos:centos:7"
 HOME_URL="https://www.centos.org/"
 BUG_REPORT_URL="https://bugs.centos.org/"
 
 CENTOS_MANTISBT_PROJECT="CentOS-7"
 CENTOS_MANTISBT_PROJECT_VERSION="7"
 REDHAT_SUPPORT_PRODUCT="centos"
 REDHAT_SUPPORT_PRODUCT_VERSION="7"
 
 CentOS Linux release 7.7.1908 (Core)
 CentOS Linux release 7.7.1908 (Core)
 Linux xiaonans-System-Product-Name 4.15.0-66-generic #75-Ubuntu SMP Tue Oct 1 05:24:09 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
 
 ***GPU Information***
 Thu Nov 14 06:55:56 2019
 +-----------------------------------------------------------------------------+
 | NVIDIA-SMI 410.48                 Driver Version: 410.48                    |
 |-------------------------------+----------------------+----------------------+
 | GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
 | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
 |===============================+======================+======================|
 |   0  Quadro RTX 6000     Off  | 00000000:02:00.0 Off |                  Off |
 | 33%   40C    P8    29W / 260W |    661MiB / 24190MiB |      0%      Default |
 +-------------------------------+----------------------+----------------------+
 
 +-----------------------------------------------------------------------------+
 | Processes:                                                       GPU Memory |
 |  GPU       PID   Type   Process name                             Usage      |
 |=============================================================================|
 +-----------------------------------------------------------------------------+
 
 ***CPU***
 Architecture:          x86_64
 CPU op-mode(s):        32-bit, 64-bit
 Byte Order:            Little Endian
 CPU(s):                12
 On-line CPU(s) list:   0-11
 Thread(s) per core:    2
 Core(s) per socket:    6
 Socket(s):             1
 NUMA node(s):          1
 Vendor ID:             GenuineIntel
 CPU family:            6
 Model:                 158
 Model name:            Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
 Stepping:              10
 CPU MHz:               4671.761
 CPU max MHz:           4700.0000
 CPU min MHz:           800.0000
 BogoMIPS:              7392.00
 Virtualization:        VT-x
 L1d cache:             32K
 L1i cache:             32K
 L2 cache:              256K
 L3 cache:              12288K
 NUMA node0 CPU(s):     0-11
 Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d
 
 ***CMake***
 
 ***g++***
 
 ***nvcc***
 
 ***Python***
 /opt/conda/envs/rapids/bin/python
 Python 3.6.7
 
 ***Environment Variables***
 PATH                            : /opt/conda/envs/rapids/bin:/opt/conda/condabin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/conda/bin:/conda/bin
 LD_LIBRARY_PATH                 : /usr/local/nvidia/lib:/usr/local/nvidia/lib64:/usr/local/cuda/lib64:/usr/local/lib
 NUMBAPRO_NVVM                   :
 NUMBAPRO_LIBDEVICE              :
 CONDA_PREFIX                    : /opt/conda/envs/rapids
 PYTHON_PATH                     :
 
 ***conda packages***
 /opt/conda/condabin/conda
 # packages in environment at /opt/conda/envs/rapids:
 #
 # Name                    Version                   Build  Channel
 _tflow_select             2.3.0                       mkl    anaconda
 absl-py                   0.8.1                    py36_0    conda-forge
 aiohttp                   3.6.1            py36h516909a_0    conda-forge
 arrow-cpp                 0.14.1           py36h5ac5442_4    conda-forge
 astor                     0.7.1                      py_0    conda-forge
 async-timeout             3.0.1                   py_1000    conda-forge
 atomicwrites              1.3.0                      py_0    conda-forge
 attrs                     19.3.0                     py_0    conda-forge
 backcall                  0.1.0                      py_0    conda-forge
 blas                      1.1                    openblas    conda-forge
 bleach                    3.1.0                      py_0    conda-forge
 bokeh                     1.3.4                    py36_0    conda-forge
 boost-cpp                 1.70.0               h8e57a91_2    conda-forge
 brotli                    1.0.7             he1b5a44_1000    conda-forge
 bzip2                     1.0.8                h516909a_1    conda-forge
 c-ares                    1.15.0            h516909a_1001    conda-forge
 ca-certificates           2019.8.28                     0
 certifi                   2019.9.11                py36_0
 chardet                   3.0.4                 py36_1003    conda-forge
 click                     7.0                        py_0    conda-forge
 cloudpickle               1.2.2                      py_0    conda-forge
 cudatoolkit               10.0.130                      0    nvidia
 cudf                      0.10.0                   py36_0    rapidsai
 cudnn                     7.6.0                cuda10.0_0    nvidia
 cugraph                   0.10.0                   py36_0    rapidsai
 cuml                      0.10.0          cuda10.0_py36_0    rapidsai
 cupy                      6.3.0            py36h88562e5_0    rapidsai
 cuspatial                 0.10.0                   py36_0    rapidsai
 cycler                    0.10.0                     py_1    conda-forge
 cython                    0.29.13          py36he1b5a44_0    conda-forge
 cytoolz                   0.10.0           py36h516909a_0    conda-forge
 dask                      2.6.0                      py_0    conda-forge
 dask-core                 2.6.0                      py_0    conda-forge
 dask-cuda                 0.10.0                   py36_0    rapidsai
 dask-cudf                 0.10.0                   py36_0    rapidsai
 dask-glm                  0.2.0                      py_1    conda-forge
 dask-labextension         1.0.3                      py_0    conda-forge
 dask-ml                   1.0.0                      py_1    conda-forge
 dask-xgboost              0.2.0.dev28      cuda10.0py36_0    rapidsai
 dbus                      1.13.6               he372182_0    conda-forge
 decorator                 4.4.0                      py_0    conda-forge
 defusedxml                0.6.0                      py_0    conda-forge
 distributed               2.6.0                      py_0    conda-forge
 dlpack                    0.2                  he1b5a44_1    conda-forge
 double-conversion         3.1.5                he1b5a44_1    conda-forge
 entrypoints               0.3                   py36_1000    conda-forge
 expat                     2.2.5             he1b5a44_1003    conda-forge
 fastavro                  0.22.5           py36h516909a_0    conda-forge
 fastrlock                 0.4             py36hf484d3e_1000    conda-forge
 fontconfig                2.13.1            h86ecdb6_1001    conda-forge
 freetype                  2.10.0               he983fc9_1    conda-forge
 fsspec                    0.5.2                      py_0    conda-forge
 gast                      0.3.2                      py_0    conda-forge
 gettext                   0.19.8.1          hc5be6a0_1002    conda-forge
 gflags                    2.2.2             he1b5a44_1001    conda-forge
 glib                      2.58.3            h6f030ca_1002    conda-forge
 glog                      0.4.0                he1b5a44_1    conda-forge
 google-pasta              0.1.7                      py_0    conda-forge
 grpc-cpp                  1.23.0               h18db393_0    conda-forge
 grpcio                    1.23.0           py36he9ae1f9_0    conda-forge
 gst-plugins-base          1.14.5               h0935bb2_0    conda-forge
 gstreamer                 1.14.5               h36ae1b5_0    conda-forge
 h5py                      2.10.0          nompi_py36h513d04c_100    conda-forge
 hdf5                      1.10.5          nompi_h3c11f04_1103    conda-forge
 heapdict                  1.0.1                      py_0    conda-forge
 icu                       64.2                 he1b5a44_1    conda-forge
 idna                      2.8                   py36_1000    conda-forge
 idna_ssl                  1.1.0                 py36_1000    conda-forge
 importlib_metadata        0.23                     py36_0    conda-forge
 ipykernel                 5.1.2            py36h5ca1d4c_0    conda-forge
 ipython                   7.3.0            py36h24bf2e0_0    conda-forge
 ipython_genutils          0.2.0                      py_1    conda-forge
 jedi                      0.15.1                   py36_0    conda-forge
 jinja2                    2.10.3                     py_0    conda-forge
 joblib                    0.14.0                     py_0    conda-forge
 jpeg                      9c                h14c3975_1001    conda-forge
 json5                     0.8.5                      py_0    conda-forge
 jsonschema                3.1.1                    py36_0    conda-forge
 jupyter-server-proxy      1.1.0                      py_0    conda-forge
 jupyter_client            5.3.3                    py36_1    conda-forge
 jupyter_core              4.5.0                      py_0    conda-forge
 jupyterlab                1.0.7                    py36_0    conda-forge
 jupyterlab-nvdashboard    0.1.11                   pypi_0    pypi
 jupyterlab_server         1.0.6                      py_0    conda-forge
 keras-applications        1.0.8                      py_1    conda-forge
 keras-preprocessing       1.1.0                      py_0    conda-forge
 kiwisolver                1.1.0            py36hc9558a2_0    conda-forge
 libblas                   3.8.0               14_openblas    conda-forge
 libcblas                  3.8.0               14_openblas    conda-forge
 libcudf                   0.10.0               cuda10.0_0    rapidsai
 libcugraph                0.10.0               cuda10.0_0    rapidsai
 libcuml                   0.10.0               cuda10.0_0    rapidsai
 libcumlprims              0.10.0a191017       cuda10.0_15    rapidsai-nightly
 libcuspatial              0.10.0               cuda10.0_0    rapidsai
 libevent                  2.1.10               h72c5cf5_0    conda-forge
 libffi                    3.2.1             he1b5a44_1006    conda-forge
 libgcc-ng                 7.3.0                hdf63c60_0
 libgfortran-ng            7.3.0                hdf63c60_0
 libiconv                  1.15              h516909a_1005    conda-forge
 liblapack                 3.8.0               14_openblas    conda-forge
 libnvstrings              0.10.0               cuda10.0_0    rapidsai
 libopenblas               0.3.7                h6e990d7_1    conda-forge
 libpng                    1.6.37               hed695b0_0    conda-forge
 libprotobuf               3.8.0                h8b12597_0    conda-forge
 librmm                    0.10.0               cuda10.0_0    rapidsai
 libsodium                 1.0.17               h516909a_0    conda-forge
 libstdcxx-ng              7.3.0                hdf63c60_0
 libtiff                   4.0.10            h57b8799_1003    conda-forge
 libuuid                   2.32.1            h14c3975_1000    conda-forge
 libuv                     1.32.0               h516909a_0    conda-forge
 libxcb                    1.13              h14c3975_1002    conda-forge
 libxgboost                1.0.0.SNAPSHOT       cuda10.0_1    rapidsai
 libxml2                   2.9.9                hee79883_5    conda-forge
 llvmlite                  0.29.0           py36hfd453ef_1    conda-forge
 locket                    0.2.0                      py_2    conda-forge
 lz4-c                     1.8.3             he1b5a44_1001    conda-forge
 markdown                  3.1.1                      py_0    conda-forge
 markupsafe                1.1.1            py36h14c3975_0    conda-forge
 matplotlib                3.1.1                    py36_1    conda-forge
 matplotlib-base           3.1.1            py36he7580a8_1    conda-forge
 mistune                   0.8.4           py36h14c3975_1000    conda-forge
 more-itertools            7.2.0                      py_0    conda-forge
 msgpack-python            0.6.2            py36hc9558a2_0    conda-forge
 multidict                 4.5.2           py36h14c3975_1000    conda-forge
 multipledispatch          0.6.0                      py_0    conda-forge
 nbconvert                 5.6.0                    py36_1    conda-forge
 nbformat                  4.4.0                      py_1    conda-forge
 nccl                      2.4.6.1              cuda10.0_0    nvidia
 ncurses                   6.1               hf484d3e_1002    conda-forge
 networkx                  2.4                        py_0    conda-forge
 nodejs                    12.11.1              h10a4023_0    conda-forge
 nomkl                     2.0                           0
 notebook                  6.0.1                    py36_0    conda-forge
 numba                     0.45.1           py36hb3f55d8_0    conda-forge
 numpy                     1.16.4           py36h95a1406_0    conda-forge
 nvstrings                 0.10.0                   py36_0    rapidsai
 olefile                   0.46                       py_0    conda-forge
 openblas                  0.3.7                h6e990d7_1    conda-forge
 openssl                   1.1.1d               h7b6447c_3
 packaging                 19.2                       py_0    conda-forge
 pandas                    0.24.2           py36hb3f55d8_0    conda-forge
 pandoc                    2.7.3                         0    conda-forge
 pandocfilters             1.4.2                      py_1    conda-forge
 parquet-cpp               1.5.1                         2    conda-forge
 parso                     0.5.1                      py_0    conda-forge
 partd                     1.0.0                      py_0    conda-forge
 patsy                     0.5.1                      py_0    conda-forge
 pcre                      8.43                 he1b5a44_0    conda-forge
 pexpect                   4.7.0                    py36_0    conda-forge
 pickleshare               0.7.5                 py36_1000    conda-forge
 pillow                    6.2.0            py36h6b7be26_0    conda-forge
 pip                       19.2.3                   py36_0    conda-forge
 pluggy                    0.12.0                     py_0    conda-forge
 prometheus_client         0.7.1                      py_0    conda-forge
 prompt_toolkit            2.0.10                     py_0    conda-forge
 protobuf                  3.8.0            py36he1b5a44_1    conda-forge
 psutil                    5.6.3            py36h516909a_0    conda-forge
 pthread-stubs             0.4               h14c3975_1001    conda-forge
 ptyprocess                0.6.0                   py_1001    conda-forge
 py                        1.8.0                      py_0    conda-forge
 py-xgboost                1.0.0.SNAPSHOT   cuda10.0py36_1    rapidsai
 pyarrow                   0.14.1           py36h8b68381_2    conda-forge
 pygments                  2.4.2                      py_0    conda-forge
 pynvml                    8.0.3                      py_0    conda-forge
 pyparsing                 2.4.2                      py_0    conda-forge
 pyqt                      5.9.2            py36hcca6a23_4    conda-forge
 pyrsistent                0.15.4           py36h516909a_0    conda-forge
 pytest                    5.2.1                    py36_0    conda-forge
 python                    3.6.7             h357f687_1005    conda-forge
 python-dateutil           2.8.0                      py_0    conda-forge
 pytz                      2019.3                     py_0    conda-forge
 pyyaml                    5.1.2            py36h516909a_0    conda-forge
 pyzmq                     18.1.0           py36h1768529_0    conda-forge
 qt                        5.9.7                h0c104cb_3    conda-forge
 rapids                    0.10.0          cuda10.0_py36_1    rapidsai
 rapids-xgboost            0.10.0          cuda10.0_py36_0    rapidsai
 re2                       2019.09.01           he1b5a44_0    conda-forge
 readline                  8.0                  hf8c457e_0    conda-forge
 rmm                       0.10.0                   py36_0    rapidsai
 scikit-learn              0.21.3           py36hcdab131_0    conda-forge
 scipy                     1.3.1            py36h921218d_2    conda-forge
 seaborn                   0.9.0                      py_1    conda-forge
 send2trash                1.5.0                      py_0    conda-forge
 setuptools                41.4.0                   py36_0    conda-forge
 simpervisor               0.3                        py_1    conda-forge
 sip                       4.19.8          py36hf484d3e_1000    conda-forge
 six                       1.12.0                py36_1000    conda-forge
 snappy                    1.1.7             he1b5a44_1002    conda-forge
 sortedcontainers          2.1.0                      py_0    conda-forge
 sqlite                    3.30.0               hcee41ef_0    conda-forge
 statsmodels               0.10.1           py36hc1659b7_0    conda-forge
 tblib                     1.4.0                      py_0    conda-forge
 tensorboard               1.14.0                   py36_0    conda-forge
 tensorflow                1.14.0          mkl_py36h2526735_0    anaconda
 tensorflow-base           1.14.0          mkl_py36h7ce6ba3_0    anaconda
 tensorflow-estimator      1.14.0           py36h5ca1d4c_0    conda-forge
 termcolor                 1.1.0                      py_2    conda-forge
 terminado                 0.8.2                    py36_0    conda-forge
 testpath                  0.4.2                   py_1001    conda-forge
 thrift-cpp                0.12.0            hf3afdfd_1004    conda-forge
 tk                        8.6.9             hed695b0_1003    conda-forge
 toolz                     0.10.0                     py_0    conda-forge
 tornado                   6.0.3            py36h516909a_0    conda-forge
 traitlets                 4.3.3                    py36_0    conda-forge
 typing_extensions         3.7.4                    py36_0    conda-forge
 umap-learn                0.3.10                   py36_0    conda-forge
 uriparser                 0.9.3                he1b5a44_1    conda-forge
 wcwidth                   0.1.7                      py_1    conda-forge
 webencodings              0.5.1                      py_1    conda-forge
 werkzeug                  0.16.0                     py_0    conda-forge
 wheel                     0.33.6                   py36_0    conda-forge
 wrapt                     1.11.2           py36h516909a_0    conda-forge
 xgboost                   1.0.0.SNAPSHOT   cuda10.0py36_1    rapidsai
 xorg-libxau               1.0.9                h14c3975_0    conda-forge
 xorg-libxdmcp             1.1.3                h516909a_0    conda-forge
 xz                        5.2.4             h14c3975_1001    conda-forge
 yaml                      0.1.7             h14c3975_1001    conda-forge
 yarl                      1.3.0           py36h14c3975_1000    conda-forge
 zeromq                    4.3.2                he1b5a44_2    conda-forge
 zict                      1.0.0                      py_0    conda-forge
 zipp                      0.6.0                      py_0    conda-forge
 zlib                      1.2.11            h516909a_1006    conda-forge
 zstd                      1.4.0                h3b9ef0a_0    conda-forge

Additional context Add any other context about the problem here.

xiaonans avatar Nov 14 '19 06:11 xiaonans

Thanks for filing @xiaonans . It looks like we currently can't handle trying to typecast to the base numpy datetime class np.datetime64.

In the meantime, you can achieve your goal with a small change. Pandas automatically casts to nanosecond resolution when you pass np.datetime64, so you could do the following:

import numpy as np
import cudf
gdf=cudf.DataFrame({"a":["2014-01-26"]})
gdf["a"]=gdf["a"].astype('datetime64[ns]')
gdf.dtypes
a    datetime64[ns]
dtype: object

I'll change this issue to a request to support that usage pattern of casting to nanosecond resolution by default when passed the base numpy class.

beckernick avatar Nov 14 '19 14:11 beckernick

Thanks @beckernick ! That works for me.

xiaonans avatar Nov 17 '19 08:11 xiaonans

I've tried this and the conversion works except that the fractions of second are lost. How can I include the fractions of second?

df['utc_datetime'] = df.utc.astype('datetime64[ns]')
print(df.loc[[0,100],:])
                         utc        utc_datetime
0    2016-01-01 00:00:00.000 2016-01-01 00:00:00
100  2016-01-01 00:00:03.333 2016-01-01 00:00:03

print(df.utc_datetime[100] - df.utc_datetime[0])
3000000000 nanoseconds

bpleao avatar Nov 20 '19 19:11 bpleao

Thanks for the report @bpleao . I'm going to open a separate issue for this one and will crosslink it here.

beckernick avatar Nov 25 '19 14:11 beckernick