【问题标题】:TypeError: data type ' int64' not understoodTypeError:不理解数据类型“int64”
【发布时间】:2020-03-25 00:26:57
【问题描述】:

我有以下函数可以在我的 jupyter notebook 中加载数据

#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

【问题讨论】:

  • 你能把csv文件的前几行贴出来
  • Python 没有int64,它只有int。这是numpy 中的一个数据类型。 (np.int64)。
  • 您需要在Int64 中大写I,并确保没有前导空格。 TypeError 看起来你有一个额外的前导空格。

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


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2020-06-16
    • 2018-05-24
    • 2021-06-11
    • 2021-07-17
    • 2018-12-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多