Pandas:使用 astype() 将不同的数据类型转换为 pyarrow 数据类型

问题描述 投票:0回答:1

我有一个具有不同数据类型的数据框,例如 bool、int、float、datetime、category。 目前我正在转换

# Earlier to pandas 2.0

1. object -> string
2. object -> datetime[ns] # if date

使用新的 pandas 2.0 或更高版本,我尝试对所有字段使用 pyarrow 数据类型并以

parquet
格式保存。

我们可以有以下:

int8 -> int8[pyarrow] likewise for other int's type
float16 -> float16[pyarrow] likewise for other float's type
string or object -> string[pyarrow]

eg:

df['col_int'] = df['col_int'].astype('int8[pyarrow]')

我没有找到太多关于如何使用

astype()
转换日期时间和类别的信息,如下所示:

1. datetime -> timestamp # if date
2. category -> dictionary

eg:

df['col_date'] = df['col_date'].astype(???)
df['col_dictionary'] = df['col_dictionary'].astype(???)

请帮忙。

python pandas
1个回答
0
投票

试试这个:

import pyarrow as pa

df['col_date'] = df['col_date'].astype(pd.ArrowDtype(pa.string()))

更多 apache 箭头数据类型

© www.soinside.com 2019 - 2024. All rights reserved.