英文:
Datatype cannot be written to CSV
问题
当我运行时,出现了以下错误:
无法将数据类型 216106296082069783127785472 写入 CSV
问题是什么?
英文:
I want to a write big decimal to a CSV file that looks like this:
'''
pyarrow.decimal128(int precision, int scale=0) ->
pl.Decimal(scale: int, precision: int | None = None)
'''
df = DataFrame(
data={'BIG_DECIMAL': [Decimal(216106296082069775206775124.4217024184)]},
schema={'BIG_DECIMAL': pl.Decimal(10, 38)}
)
df.write_csv('file.csv')
However, when I run it, I get an error that says:
datatype 216106296082069783127785472 cannot be written to csv
What is the problem?
答案1
得分: 1
您遇到的错误表明您的大十进制值的数据类型无法直接使用默认的CSV写入程序写入CSV文件。 pandas中的CSV写入程序没有内置支持处理像pyarrow.decimal128这样的自定义数据类型的功能。
为了解决这个问题,您可以将大十进制值转换为可以写入CSV文件的兼容数据类型。一种选择是在写入CSV文件之前将大十进制值转换为字符串。以下是您的代码的更新版本,将大十进制值转换为字符串:
import polars as pl
df = pl.DataFrame({
'BIG_DECIMAL': [pl.Decimal('216106296082069775206775124.4217024184')]
})
df = df.with_column(df['BIG_DECIMAL'].map(lambda dec: str(dec)))
df.write_csv('file.csv')
英文:
The error you're encountering indicates that the datatype of your big decimal value cannot be directly written to a CSV file using the default CSV writer. The CSV writer in pandas doesn't have built-in support for handling custom datatypes like pyarrow.decimal128.
To overcome this issue, you can convert the big decimal values to a compatible datatype that can be written to a CSV file. One option is to convert the big decimal values to strings before writing them to the CSV file. Here's an updated version of your code that converts the big decimal value to a string:
import polars as pl
df = pl.DataFrame({
'BIG_DECIMAL': [pl.Decimal('216106296082069775206775124.4217024184')]
})
df = df.with_column(df['BIG_DECIMAL'].map(lambda dec: str(dec)))
df.write_csv('file.csv')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论