Using pyarrow
to convert a pandas.DataFrame
containing Player
objects to a pyarrow.Table
with the following code
import pandas as pd
import pyarrow as pa
class Player:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
def __repr__(self):
return f'<{self.name} ({self.age})>'
data = [
Player('Jack', 21, 'm'),
Player('Ryan', 18, 'm'),
Player('Jane', 35, 'f'),
]
df = pd.DataFrame(data, columns=['player'])
print(pa.Table.from_pandas(df))
we get the error:
pyarrow.lib.ArrowInvalid: ('Could not convert <Jack (21)> with type Player: did not recognize Python value type when inferring an Arrow data type', 'Conversion failed for column 0 with type object')
Same error encountered by using
df.to_parquet('players.pq')
Is it possible for pyarrow
to fallback to serializing these Python objects using pickle
? Or is there a better solution? The pyarrow.Table
will eventually be written to disk using Parquet.write_table()
.
- Using Python 3.8.0, pandas 0.25.3, pyarrow 0.13.0.
pandas.DataFrame.to_parquet()
does not support multi index, so a solution usingpq.write_table(pa.Table.from_dataframe(pandas.DataFrame))
is preferred.
Thank you!