【问题标题】:"TypeError: data type not understood" while reading csv file读取 csv 文件时出现“TypeError:数据类型不理解”
【发布时间】:2018-11-13 22:39:24
【问题描述】:

我正在尝试读取 .csv 文件并将特定范围的值分配给它自己的列表以用于索引目的:

import numpy as np
import scipy as sp
import matplotlib as plot
import pandas as pd

# read in the data held in the csv file using "read_csv", a function built into the "pandas" module
Ne = pd.read_table('Ionosphere_data.csv', sep='\s+', dtype=np.float64);

print(Ne.shape)
print(np.dtype)

# store each dimension from the csv file into its own array for plotting use
Altitude = Ne[:,1];
Time = Ne[0,:];

# loop through each electron density value with respect to

Ne_max = []

for i in range(0,len(Time)):
  for j in range(0,len(Altitude)):
    Ne_max[j] = np.max(Ne[:,i]);

print Ne_max

#plot(Time,Altitude,Ne_max);
#xaxislabel('Time (hours)')
#yaxislabel('Altitude (km)')

但是,当我运行此代码时,ipython 会显示错误消息: 第 10 行上下文中的“TypeError:数据类型不理解”。(另一个注意事项,当不包含“print(np.dtype)”时,第 13 行会给出单独的错误消息:“TypeError:unhashable type”)。 有谁知道我是在文件中读错了还是有其他问题?

【问题讨论】:

  • 应该是print(Ne.dtype)
  • 其实应该是 print(np.dtype(Ne))
  • 不,这行不通。当您调用 np.dtype(obj) 时,它会尝试将 obj convert 转换为 numpy dtype。如果要检查 Pandas DataFrame 的数据类型,请使用Ne.dtypes(它是 DataFrame 对象的属性)。
  • 有趣的笔记!感谢那;看来我尝试这个的困惑来自尝试命令“print(Ne.dtype)”而不是“print(Ne.dtypes)”

标签: python arrays pandas csv numpy


【解决方案1】:

在评论中,你说那行

print(np.dtype)

应该是

print(np.dtype(Ne))

这给出了错误TypeError: data type not understoodnumpy.dtype 尝试将其参数 convert 转换为 numpy 数据类型对象。它不用于检查参数的数据类型。

对于 Pandas DataFrame,使用 dtypes 属性:

print(Ne.dtypes)

【讨论】:

  • 谢谢,这解决了数据类型问题。我似乎仍然无法解决我的另一个问题,即运行该程序会在第 13 行发出一条错误消息,上面写着“TypeError:Unhashable Type”
  • 一次问一个问题会更简单。就该问题提出一个新问题。
猜你喜欢
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 2020-06-16
  • 2018-02-17
  • 2018-05-24
  • 2021-06-11
  • 2020-03-25
  • 1970-01-01
相关资源
最近更新 更多