Skip to content

[Python] ArrowInvalid error raised when deserialising pandas with pd.NaT values in object column #25312

Closed

Description

@asfimport

In pyarrow 0.17.x when deserialising a pandas dataframe which has pd.NaT values in an object column, an ArrowInvalid error is raised:

pyarrow.lib.ArrowInvalid: Casting from timestamp[us] to timestamp[ns] would result in out of bounds timestamp: -62135596800000000

Reproducible code (using pyarrow==0.17.1 and pandas==1.0.3):

import pandas as pd
import pyarrow.ipc as ipc
import pyarrow as pa
v = pd.DataFrame({
    "bar": [1592808896000000000, pd.NaT]
})
# works fine as datetime64[ns] but not as object type
v = v.astype({"bar": "datetime64[ns]"}).astype({"bar": "object"})
bs = ipc.serialize_pandas(v).to_pybytes()
df = ipc.deserialize_pandas(bs)  # error

 In pyarrow 0.16.0 no error occurs and df is returned as:

                            bar
0 2020-06-22 06:54:56.000000000
1 1754-08-30 22:43:41.128654848

Was the change in 0.17.x to raise an error an intentional behaviour change? Given the previous behaviour in 0.16.0 seemed a bit like undefined behaviour already, where it converted NaT to 1754-08-30 (which seems due to the -62135596800000000 timestamp mentioned in the error above?).

Also note that when serialized as datetime64[ns] rather than object, the code works fine in both 0.17.x and 0.16.0, returning:

                  bar
0 2020-06-22 06:54:56
1                 NaT

 

Reporter: Lawrence Ling
Assignee: Wes McKinney / @wesm

Related issues:

Note: This issue was originally created as ARROW-9211. Please see the migration documentation for further details.

Activity

  1. asfimport commented on Jun 24, 2020

    @asfimport
    Author

    Wes McKinney / @wesm:
    Well, raising an exception is strictly better than returning garbage to you. The problem is in the conversion of NaT to null, which is not handled correctly:

    In [29]: v                                                                                                        
    Out[29]: 
                       bar
    0  2020-06-22 06:54:56
    1                  NaT
    
    In [30]: pa.table(v)[0].chunk(0)                                                                                  
    Out[30]: 
    <pyarrow.lib.TimestampArray object at 0x7f4b4070e528>
    [
      2020-06-22 06:54:56.000000,
      0001-01-01 00:00:00.000000
    ]

    pandas's NaT value is a bit weird:

    In [31]: type(pd.NaT).mro()                                                                                       
    Out[31]: 
    [pandas._libs.tslibs.nattype.NaTType,
     pandas._libs.tslibs.nattype._NaT,
     datetime.datetime,
     datetime.date,
     object]

    I'm gonna quickly see if I can get pyarrow to recognize pandas.NaT, so I'm closing this as a duplicate of ARROW-842

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions