You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Python] Support converting to non-nano datetime64 for pandas >= 2.0 #33321
Pandas is adding capabilities to store non-nanosecond datetime64 data. At the moment, we however always do convert to nanosecond, regardless of the timestamp resolution of the arrow table (and regardless of the pandas metadata).
This is because we have a coerce_temporal_nanoseconds conversion option which we hardcode to True (for top-level columns, we hardcode it to False for nested data).
When users have pandas >= 2, we should support converting with preserving the resolution. We should certainly do so if the pandas metadata indicates which resolution was originally used (to ensure correct roundtrip).
We could (and at some point also should) also do that by default if there is no pandas metadata (but maybe only later depending on how stable this new feature is in pandas, as it is potentially a breaking change for our users if you use eg pyarrow to read a parquet file).
When users have pandas >= 2, we should support converting with preserving the resolution. We should certainly do so if the pandas metadata indicates which resolution was originally used (to ensure correct roundtrip).
We could (and at some point also should) also do that by default if there is no pandas metadata (but maybe only later depending on how stable this new feature is in pandas, as it is potentially a breaking change for our users if you use eg pyarrow to read a parquet file).
Can you provide some more detail about this paragraph? Does pandas metadata mean the pyarrow/array.pxi::PandasOptions class? When would pandas metadata not be present and why would that break parquet reading?
So this indicates that the original data in the pandas.DataFrame had "datetime64[ns]" type. In this case that matches the arrow type, but for example after a roundtrip through Parquet, this might no longer be the case:
>>>pq.write_table(table, "test.parquet")
>>>table2=pq.read_table("test.parquet")
>>>table2.schemaOut[33]:
col: timestamp[us] # <--- now us instead of ns--schemametadata--pandas: '{"index_columns": [{"kind": "range", "name": null, "start": 0, "'+413>>>table2.schema.pandas_metadata
{ ...
'columns': [{'name': 'col',
'field_name': 'col',
'pandas_type': 'datetime',
'numpy_type': 'datetime64[ns]', # <--- but this still indicates ns'metadata': None}],
...
So the question is here what table2.to_pandas() should do? Use the microsecond resolution of the data, of the nanosecond resolution of the metadata?
(note that this is also a consequence of the default parquet version we write not yet supporting nanoseconds, while we should probably bump that default version we write, and then the nanoseconds would be preserved in the Parquet roundtrip)
Now, I am not sure if it would be easy to use the information of the pandas metadata to influence the conversion, as we typically only use the metadata after converting the actual data to finalize the resulting pandas DataFrame (eg set the index, cast the column names, ..).
And I am also not fully sure if it would actually be desirable to follow the pandas metadata, since that would involve an extra conversion step (and effectively all existing pandas metadata (eg in already written parquet files) will always say that it is nanoseconds, since until recently that was the only supported resolution by pandas).
Excellent write-up and example! I'd prefer supporting nanosecond in parquet and not converting based off of metadata since it seems like its not guaranteed to be reliable. Filed #35746 to track Parquet support for ns.
Activity
danepitkin commentedon May 17, 2023
Can you provide some more detail about this paragraph? Does pandas metadata mean the
pyarrow/array.pxi::PandasOptionsclass? When would pandas metadata not be present and why would that break parquet reading?jorisvandenbossche commentedon May 24, 2023
The "pandas metadata" is custom metadata that we store in the pyarrow schema whenever the data is created from a pandas.DataFrame:
So this indicates that the original data in the pandas.DataFrame had "datetime64[ns]" type. In this case that matches the arrow type, but for example after a roundtrip through Parquet, this might no longer be the case:
So the question is here what
table2.to_pandas()should do? Use the microsecond resolution of the data, of the nanosecond resolution of the metadata?(note that this is also a consequence of the default parquet version we write not yet supporting nanoseconds, while we should probably bump that default version we write, and then the nanoseconds would be preserved in the Parquet roundtrip)
Now, I am not sure if it would be easy to use the information of the pandas metadata to influence the conversion, as we typically only use the metadata after converting the actual data to finalize the resulting pandas DataFrame (eg set the index, cast the column names, ..).
And I am also not fully sure if it would actually be desirable to follow the pandas metadata, since that would involve an extra conversion step (and effectively all existing pandas metadata (eg in already written parquet files) will always say that it is nanoseconds, since until recently that was the only supported resolution by pandas).
danepitkin commentedon May 24, 2023
Excellent write-up and example! I'd prefer supporting nanosecond in parquet and not converting based off of metadata since it seems like its not guaranteed to be reliable. Filed #35746 to track Parquet support for ns.
Fix parquet paritioning pytest failures (#13474)
2 remaining items
GH-33321: [Python] Support converting to non-nano datetime64 for pand…
GH-36541: [Python][CI] Fixup nopandas build after merge of GH-33321 (#…
GH-36629: [CI][Python] Skip dask tests due to our non-nanosecond chan…
GH-36629: [CI][Python] Skip dask tests due to our non-nanosecond chan…
Pin pyarrow>=13 and use pytest.mark.skipif on string_view type test