熊猫:无法安全地将传递给int32的用户dtype转换为float64

时间:2018-07-06 15:59:02

标签: python pandas validation numpy dataframe

我为使用read_table()将数据加载到Pandas数据框而感到困惑。错误显示TypeError: Cannot cast array from dtype('float64') to dtype('int32') according to the rule 'safe'ValueError: cannot safely convert passed user dtype of int32 for float64 dtyped data in column 2

test.py:

import numpy as np
import os
import pandas as pd

# put test.csv in same folder as script
mydir = os.path.dirname(os.path.abspath(__file__))
csv_path = os.path.join(mydir, "test.csv")

df = pd.read_table(csv_path, sep=' ',
                   comment='#',
                   header=None,
                   skip_blank_lines=True,
                   names=["A", "B", "C", "D", "E", "F", "G"],
                   dtype={"A": np.int32,
                       "B": np.int32,
                       "C": np.float64,
                       "D": np.float64,
                       "E": np.float64,
                       "F": np.float64,
                       "G": np.int32})

test.csv:

2270433 3 21322.889 11924.667 5228.753 1.0 -1 2270432 3 21322.297 11924.667 5228.605 1.0 2270433

1 个答案:

答案 0 :(得分:0)

问题是我使用空格作为定界符,而csv具有尾随空格。删除尾随空格可以解决此问题。

要修剪目录中每个文件的每一行上的所有尾随空格,我运行了以下命令:find . -name "*.csv" | xargs sed -i 's/[ \t]*$//'