Skip to content

[Python] Pandas - Parquet - Pandas roundtrip causes out of bounds timestamp #25071

Closed

Description

@asfimport

The following pandas -> parquet -> pandas roudtrip raises an out of bounds timestamp error with pyarrow 0.17.0 and 0.17.1:

import pandas

target = 'ts_roundtrip.parquet'

dataframe = pandas.DataFrame({'id':[1,2,3],'timestamp':['', '', '']})
dataframe['timestamp'] = pandas.to_datetime(dataframe['timestamp'],errors='raise')

dataframe2 = pandas.DataFrame({'id':[4,5,6,7],'timestamp':['', '2020-03-02T03:03:17.791062Z','','']})
dataframe2['timestamp'] = pandas.to_datetime(dataframe2['timestamp'],errors='raise')
dataframe = dataframe.append(dataframe2)

print(dataframe.head(10))

dataframe.to_parquet(target, coerce_timestamps=None, index=False, version='2.0')

dataframe_new = pandas.read_parquet(target)
print(dataframe_new.head())

Output:


   id                         timestamp
0   1                               NaT
1   2                               NaT
2   3                               NaT
0   4                               NaT
1   5  2020-03-02 03:03:17.791062+00:00
2   6                               NaT
3   7                               NaT
Traceback (most recent call last):
  File "c:\some\path\pyarrow_ts_test.py", line 16, in <module>
    dataframe_new = pandas.read_parquet(target)
  File "c:\some\path\venv\lib\site-packages\pandas\io\parquet.py", line 310, in read_parquet
    return impl.read(path, columns=columns, **kwargs)
  File "c:\some\path\venv\lib\site-packages\pandas\io\parquet.py", line 125, in read
    path, columns=columns, **kwargs
  File "pyarrow\array.pxi", line 587, in pyarrow.lib._PandasConvertible.to_pandas
  File "pyarrow\table.pxi", line 1640, in pyarrow.lib.Table._to_pandas
  File "c:\some\path\venv\lib\site-packages\pyarrow\pandas_compat.py", line 766, in table_to_blockmanager
    blocks = _table_to_blocks(options, table, categories, ext_columns_dtypes)
  File "c:\some\path\venv\lib\site-packages\pyarrow\pandas_compat.py", line 1102, in _table_to_blocks
    list(extension_columns.keys()))
  File "pyarrow\table.pxi", line 1107, in pyarrow.lib.table_to_blocks
  File "pyarrow\error.pxi", line 85, in pyarrow.lib.check_status
pyarrow.lib.ArrowInvalid: Casting from timestamp[us] to timestamp[ns] would result in out of bounds timestamp: -62135596800000000

Background:
We have a dataset with a timestamp column that is sparsely populated and originates from many json files. So it is very likely that in some of those json files there is no timestamp (as string in ISO format) and instead just an empty string. Each JSON file was read into a pandas dataframe, the timestamp column casted to datetime and all dataframes appended. That was done with pyarrow<0.17.0 and those parquet files cannot be read any longer and result in the above mentioned error message as well.

A closer look at our old parquets show that the NaTs are converted to "1754-08-30 22:43:41.128654848" when reading back to a pandas dataframe :(. You get the same result when you run the above code and pyarrow==0.16.0. 

Environment: pandas==1.0.3
pyarrow==0.17.1
Python==3,7.6 @ Windows 10 64Bit
Reporter: Daniel Figus

Related issues:

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

Activity

  1. asfimport commented on May 26, 2020

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:
    The problem here is that the NaT values are converted to pyarrow as "0001-01-01". Using a simplified example:

    In [22]: pa.array(pd.Series([pd.NaT, pd.Timestamp("2012-01-01")], dtype=object)) 
    Out[22]: 
    <pyarrow.lib.TimestampArray object at 0x7f954b176fa8>
    [
     0001-01-01 00:00:00.000000,
     2012-01-01 00:00:00.000000
    ]

    This in itself is a bug, which is covered by  ARROW-842 (and ARROW-8115).
    When converting back to pandas, it tries to convert this to nanosecond resolution, because this is the only resolution that pandas supports. However, "0001-01-01" doesn't fit into the nanosecond range, and therefore you get this error. There is work underway to make this conversion back to pandas more flexible, so you can opt for datetime objects (see eg ARROW-5359).

    The above clarifies the behaviour you see. But in your case, the actual problem is that you are having object dtype data:

    In [23]: dataframe.dtypes                                                                                                                                                                                          
    Out[23]: 
    id            int64
    timestamp    object
    dtype: object
    
    In [24]: dataframe['timestamp'].values                                                                                                                                                                             
    Out[24]: 
    array([NaT, NaT, NaT, NaT,
           Timestamp('2020-03-02 03:03:17.791062+0000', tz='UTC'), NaT, NaT],
          dtype=object)

    And it is therefore you run into this NaT conversion bug.
    Now the reason you have object dtype data is because of appending a dataframe with tz-aware data to tz-naive data:

    # dataframe before appending dataframe2
    In [27]: dataframe.dtypes                                                                                                                                                                                          
    Out[27]: 
    id                    int64
    timestamp    datetime64[ns]
    dtype: object
    
    In [28]: dataframe2.dtypes                                                                                                                                                                                         
    Out[28]: 
    id                         int64
    timestamp    datetime64[ns, UTC]
    dtype: object
    
    In [29]: dataframe.append(dataframe2).dtypes                                                                                                                                                                       
    Out[29]: 
    id            int64
    timestamp    object
    dtype: object
  2. asfimport commented on May 27, 2020

    @asfimport
    Author

    Daniel Figus:
    Thanks Joris, I'll watch ARROW-842 (and ARROW-8115). If my understanding is correct, I would need to do the following to correct the existing parquets and use pyarrow>=0.17.0:

    1. Read the parquets into an arrow table
    2. Convert arrow table to pandas with safe=False to avoid the out-of-bound error
    3. Replace the 1754-08-30 22:43:41.128654848 with NaT values
    4. Cast column to datetime64[ns, UTC] dtype (to avoid issue described in ARROW-842 & your comment in ARROW-8115)
    5. Write back parquet
  3. asfimport commented on May 28, 2020

    @asfimport
    Author

    Joris Van den Bossche / @jorisvandenbossche:
    Yes, for existing parquet files written with the "0001-01-01" dates in it, that seems like a decent solution.
    And so when writing new parquet files, best ensure you don't have "object" dtype columns (at least for columns with datetimes in it, for string columns that is normal), to avoid the whole issue.

  4. asfimport commented on Sep 10, 2020

    @asfimport
    Author

    Daniel Figus:
    @jorisvandenbossche I think this can be closed as it was resolved with ARROW-842. Just double checked it and my example from above works.

  5. asfimport commented on Oct 27, 2020

    @asfimport
    Author

    Daniel Figus:
    Closing as this is fixed by ARROW-842

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions