【问题标题】:TypeError: unhashable type: 'numpy.ndarray' when applying datetimeTypeError: unhashable type: 'numpy.ndarray' 应用日期时间时
【发布时间】:2021-12-03 16:47:09
【问题描述】:

尝试应用于日期时间时,我得到不可散列的类型错误:不可散列的类型:'numpy.ndarray'。问题是应用 iloc x 时不再来自类型 pd。那么这种情况我该怎么办呢?

X 的列,日期如 21/10/2020

from pandas import read_csv
from matplotlib import pyplot
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np


#import data

s= pd.read_csv('.....csv', header=0, squeeze=True)


x= s.iloc[:,0].values.reshape(-1,1)   
X=pd.to_datetime(x)# here is the problem
#np.atleast_2d(X)

提前谢谢你!

【问题讨论】:

  • 内容为日期
  • 能否请您更精确一点(尺寸、“日期”的 dtype 等)并在问题中添加一个示例?
  • 只是猜测:您是否希望将数据框s 的第一列解析为日期时间?然后就做x = pd.to_datetime(s.iloc[:,0], dayfirst=True)

标签: pandas dataframe numpy datetime hash


【解决方案1】:

根据文档https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

参数

arg:  int, float, str, datetime, list, tuple, 1-d array, Series, DataFrame/dict-like

    The object to convert to a datetime.

你检查x= s.iloc[:,0].values.reshape(-1,1)中x的形状

它不是一维数组。

只需放下重塑。

对于这样的数据:

x= df.iloc[:,0].values
pd.to_datetime(x)

【讨论】:

  • 您是否知道调用reshape 后OP 输入的形状,还是您猜测?你能重现错误TypeError: unhashable type: 'numpy.ndarray'吗?
  • 输入是来自切片的熊猫系列。
  • 那么,你能重现错误吗?
  • 为什么不简单地pd.to_datetime(df['Date'])? ;-)
  • 我不知道为什么它首先被转换为 x 。问题没有说。如果输出是一个 numpy X,那么可以只做 X = pd.to_datetime(df['Date']).to_numpy() 让 OP 试试看这是否能解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
  • 2019-07-10
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
相关资源
最近更新 更多