我有一个具有不同数据类型的数据框,例如 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(???)
请帮忙。
试试这个:
import pyarrow as pa
df['col_date'] = df['col_date'].astype(pd.ArrowDtype(pa.string()))