Pyarrow 使用 Pandas 不断将字符串转换为二进制

Pyarrow keeps converting string to binary using Pandas

我正在尝试使用 pandas 和 python2.7 中的 pyarrow 将 csv 文件转换为镶木地板。

我在将字符串从 pa.Table.from_pandas(df) 转换为字符串时遇到问题。它一直将数据类型转换为 'binary',这让 AWS Glue 非常不高兴。

我已经尝试过自定义架构,但它不起作用。

fields = []
for name, type in dtypes.items():
        fields.append(pa.field(name, type))
my_schema = pa.schema(fields)
df = pd.read_csv(StringIO(file), delimiter="\t")
table = pa.Table.from_pandas(df)

以前在读入 csv 时指定数据类型,这也不起作用。还尝试了 replace_schema_metadata() 但这并没有做太多,因为它不是实际的模式。

Python 2的str类型其实和Parquet定义BINARY的内容是一样的,所以所有有str对象的列都会保存为binary。在 Python 3 中,它们应该被正确加载为 bytes。要在 Parquet 中将列存储为字符串 / UTF-8,您需要将列转换为 unicode 个对象。

@Maddy Schiappa,还请注意,由于您提到了 AWS Glue,目前仅支持纯 python 库。 “

You can use Python extension modules and libraries with your AWS Glue ETL scripts as long as they are written in pure Python. C libraries such as pandas are not supported at the present time, nor are extensions written in other languages.

您也可以这样做:

df = df.astype(unicode)