TypeError:数据类型为'int64'

时间:2019-11-29 11:50:07

标签: python python-3.x pandas jupyter-notebook

我具有以下功能,可在jupyter笔记本中加载数据

#function to load data
def load_dataset(x_path, y_path):
    x = pd.read_csv(os.sep.join([DATA_DIR, x_path]),
                    dtype=DTYPES,
                    index_col="ID")
    y = pd.read_csv(os.sep.join([DATA_DIR, y_path]))

    return x, y

并且数据定义了以下类型

DTYPES = {
    'ID':'int64',
    'columnA':'str',
    'columnB':'float32',
    'columnC':'float64',
    'columnD':'datetime64[ns]'}

上述csv的标头如下

ID          columnA   columnB   columnC         columnD
941215   SALE      15000       56           10/1/2018

当我在笔记本中调用该方法时

from model import load_dataset
X_train, y_train = load_dataset("X_train.zip", "y_train.zip")

我收到以下错误

2055 raise TypeError("data type '{}' not understood".format(dtype))
2057     # Any invalid dtype (such as pd.Timestamp) should raise an error.
TypeError: data type ' int64' not understood

1 个答案:

答案 0 :(得分:0)

我认为您需要在numpy中指定dtypes

DTYPES = {
    'ID':np.int64,
    'columnA':'str',
    'columnB':np.float32,
    'columnC':np.float64}

对于日期时间,需要使用不同的方法-parse_dates中的参数read_csv

def load_dataset(x_path, y_path):
    x = pd.read_csv(os.sep.join([DATA_DIR, x_path]),
                    dtype=DTYPES,
                    index_col="ID"
                    parse_dates='columnD')
    y = pd.read_csv(os.sep.join([DATA_DIR, y_path]))

    return x, y