Моя цель - записать файлы json на паркет.Чтобы добраться до таблицы pyarrow, я использую:
import pandas as pd
import pyarrow as pa
# for the workaround
from pandas.api.types import is_datetime64_any_dtype as is_dt
from collections import OrderedDict
# Json schema in pandas form
schema = {'col1': 'object',
#
#
'col17': 'datetime64[ms]',
#
#
}
# Load json into pandas dataframe
json_df = pd.read_json('/path/to/json_file', dtype = schema, lines = True, date_unit = 'ms')
# Convert to table
table = pa.Table.from_pandas(json_df)
# This doesn't work
converted_col = pa.column(pa.array(json_df['col17']).cast('timestamp[ms]'))
table.set_column(16, converted_col) # still timestamp[ns]
В итоге я распаковал dtaframe в массивы и перестроил в pyarrow таблицу:
# Workaround
cols = OrderedDict([(col_name, pa.array(json_df[col_name]).cast('timestamp[ms]') if 'dt' in dir(json_df[col_name]) else pa.array(json_df[col_name])) for col_name in json_df])
table = pa.Table.from_arrays(cols.values(), cols.keys()) # datetime column is in fact in ms this way
Я не получилвремя различия в производительности, но я предполагаю, что первый способ может быть быстрее.Что я делаю не так в первом случае?