Currently, the timestamp_parsers option of the CSV reader only works for timestamp datatypes.
If one wants to immediately read dates as date32 objects (in my use case csv data is read and stored as parquet files with correct types), one has to cast the table to a schema with date32 types after the fact.
This snipped shows that loading the data fails when specifying the date type:
importpyarrowaspafrompyarrowimportcsvdefopen_bytes(b, **kwargs):
returncsv.open_csv(pa.py_buffer(b), **kwargs)
defread_bytes(b, **kwargs):
returnopen_bytes(b, **kwargs).read_all()
rows = b"a,b\n1970/01/01,1980-01-01 00\n1970/01/02,1980-01-02 00\n"schema = pa.schema([("a", pa.timestamp("ms")), ("b", pa.string())])
opts = csv.ConvertOptions(column_types=schema, timestamp_parsers=["%Y/%m/%d"])
table = read_bytes(rows, convert_options=opts)
asserttable.schema == schema # worksschema = pa.schema([("a", pa.date32()), ("b", pa.string())])
opts = csv.ConvertOptions(column_types=schema, timestamp_parsers=["%Y/%m/%d"])
table = read_bytes(rows, convert_options=opts) # errorhereasserttable.schema == schema
---------------------------------------------------------------------------
ArrowInvalidTraceback (mostrecentcalllast)
InputIn [134], in <cellline: 22>()
20schema = pa.schema([("a", pa.date32()), ("b", pa.string())])
21opts = csv.ConvertOptions(column_types=schema, timestamp_parsers=["%Y/%m/%d"])
---> 22table = read_bytes(rows, convert_options=opts)
23asserttable.schema == schemaInputIn [134], inread_bytes(b, **kwargs)
9defread_bytes(b, **kwargs):
---> 10returnopen_bytes(b, **kwargs).read_all()InputIn [134], inopen_bytes(b, **kwargs)
5defopen_bytes(b, **kwargs):
----> 6returncsv.open_csv(pa.py_buffer(b), **kwargs)File ~/.virtualenvs/ogi/lib/python3.9/site-packages/pyarrow/_csv.pyx:1273, inpyarrow._csv.open_csv()File ~/.virtualenvs/ogi/lib/python3.9/site-packages/pyarrow/_csv.pyx:1137, inpyarrow._csv.CSVStreamingReader._open()File ~/.virtualenvs/ogi/lib/python3.9/site-packages/pyarrow/error.pxi:144, inpyarrow.lib.pyarrow_internal_check_status()File ~/.virtualenvs/ogi/lib/python3.9/site-packages/pyarrow/error.pxi:100, inpyarrow.lib.check_status()ArrowInvalid: InCSVcolumn #0: CSVconversionerrortodate32[day]: invalidvalue'1970/01/01'It would be useful to allow the timestamp_parsers for date types as well (or add an analogous argument for dates), such that such errors don't occur and the resulting table has the required datatypes without a casting step.
A little bit more context is in the comments of https://issues.apache.org/jira/browse/ARROW-10848 (26/Oct/22).
Reporter: Tim Loderhose
Watchers: Rok Mihevc / @rok
Related issues:
Note: This issue was originally created as ARROW-18166. Please see the migration documentation for further details.
Currently, the timestamp_parsers option of the CSV reader only works for timestamp datatypes.
If one wants to immediately read dates as date32 objects (in my use case csv data is read and stored as parquet files with correct types), one has to cast the table to a schema with date32 types after the fact.
This snipped shows that loading the data fails when specifying the date type:
It would be useful to allow the timestamp_parsers for date types as well (or add an analogous argument for dates), such that such errors don't occur and the resulting table has the required datatypes without a casting step.
A little bit more context is in the comments of https://issues.apache.org/jira/browse/ARROW-10848 (26/Oct/22).
Reporter: Tim Loderhose
Watchers: Rok Mihevc / @rok
Related issues:
Note: This issue was originally created as ARROW-18166. Please see the migration documentation for further details.