Skip to content

astype attempts to convert datetime64[ms] as nanoseconds when missing value in data #10746

Closed
@dschallis

Description

@dschallis

When creating a DataFrame with a millisecond timestamp, created with dtype='datetime64[ms]', this works as expected when there are no missing values in the data:

import pandas as pd
df = pd.DataFrame([1036713600000], dtype='float64')
print(df[0].astype('datetime64[ms]'))

Output:

0   2002-11-08
Name: 0, dtype: datetime64[ns]

Adding a missing value to the data causes the values to get parsed as nanoseconds rather than microseconds, which causes an exception:

df = pd.DataFrame([1036713600000, None], dtype='float64')
print(df[0].astype('datetime64[ms]'))

Output:

Traceback (most recent call last):                                          
  File "./f.py", line 6, in <module>                                        
    print(df[0].astype('datetime64[ms]'))                                   
  File "/Users/dsc/.virtualenvs/p3default/lib/python3.4/site-packages/pandas/core/generic.py", line 2411, in astype
    dtype=dtype, copy=copy, raise_on_error=raise_on_error, **kwargs)        
  File "/Users/dsc/.virtualenvs/p3default/lib/python3.4/site-packages/pandas/core/internals.py", line 2504, in astype
    return self.apply('astype', dtype=dtype, **kwargs)                      
  File "/Users/dsc/.virtualenvs/p3default/lib/python3.4/site-packages/pandas/core/internals.py", line 2459, in apply
    applied = getattr(b, f)(**kwargs)                                       
  File "/Users/dsc/.virtualenvs/p3default/lib/python3.4/site-packages/pandas/core/internals.py", line 373, in astype
    values=values, **kwargs)                                                
  File "/Users/dsc/.virtualenvs/p3default/lib/python3.4/site-packages/pandas/core/internals.py", line 407, in _astype
    fastpath=True, dtype=dtype, klass=klass)                                
  File "/Users/dsc/.virtualenvs/p3default/lib/python3.4/site-packages/pandas/core/internals.py", line 2101, in make_block
    placement=placement)                                                    
  File "/Users/dsc/.virtualenvs/p3default/lib/python3.4/site-packages/pandas/core/internals.py", line 1795, in __init__
    values = tslib.cast_to_nanoseconds(values)                              
  File "pandas/tslib.pyx", line 2622, in pandas.tslib.cast_to_nanoseconds (pandas/tslib.c:43295)
  File "pandas/tslib.pyx", line 1333, in pandas.tslib._check_dts_bounds (pandas/tslib.c:23332)
pandas.tslib.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 292278994-08-16 16:47:04

Expected output was something like:

0   2002-11-08
1   NaT
Name: 0, dtype: datetime64[ns]

This was using python 3.4.2, Pandas 0.16.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions