首页 > 解决方案 > TypeError:pivot_table 上的不可散列类型 numpy.ndarray

问题描述

希望你能在这方面提供帮助。我一直在努力解决这个问题。我有以下熊猫数据框,df.info 下面:

DatetimeIndex: 18135 entries, 2019-09-20 14:32:02 to 2019-07-29 10:13:07
Data columns (total 10 columns):
OrderNo           18135 non-null object
HasDiscrepency    18135 non-null object
CreatedDate       18135 non-null datetime64[ns]
ExportedDate      18135 non-null datetime64[ns]
ExportedBy        18135 non-null object
XDStr             18135 non-null object
XMStr             18135 non-null object
V2X               18135 non-null timedelta64[ns]
V2Xt              18135 non-null float64
AutoExported      18135 non-null bool
dtypes: bool(1), datetime64[ns](2), float64(1), object(5), timedelta64[ns](1)

样本数据:

{'OrderNo': {Timestamp('2019-09-20 14:32:02'): '225-191369-0',
  Timestamp('2019-09-20 14:20:38'): 'OrderOne',
  Timestamp('2019-09-20 14:33:39'): 'OrderTwo',
  Timestamp('2019-09-20 14:11:32'): '30462951',
  Timestamp('2019-09-20 14:11:09'): '259257',
  Timestamp('2019-09-20 13:58:50'): 'ABC12345',
  Timestamp('2019-09-20 13:44:32'): '82/02357',
  Timestamp('2019-09-20 13:43:31'): '05/077228',
  Timestamp('2019-09-20 13:31:11'): 'DEF456',
  Timestamp('2019-09-20 13:31:01'): '6425V1874720G'},
 'HasDiscrepency': {Timestamp('2019-09-20 14:32:02'): array(False),
  Timestamp('2019-09-20 14:20:38'): array(True),
  Timestamp('2019-09-20 14:33:39'): array(True),
  Timestamp('2019-09-20 14:11:32'): array(False),
  Timestamp('2019-09-20 14:11:09'): array(False),
  Timestamp('2019-09-20 13:58:50'): array(True),
  Timestamp('2019-09-20 13:44:32'): array(False),
  Timestamp('2019-09-20 13:43:31'): array(True),
  Timestamp('2019-09-20 13:31:11'): array(False),
  Timestamp('2019-09-20 13:31:01'): array(False)},
 'CreatedDate': {Timestamp('2019-09-20 14:32:02'): Timestamp('2019-09-20 14:31:29'),
  Timestamp('2019-09-20 14:20:38'): Timestamp('2019-09-20 14:17:17'),
  Timestamp('2019-09-20 14:33:39'): Timestamp('2019-09-20 14:14:58'),
  Timestamp('2019-09-20 14:11:32'): Timestamp('2019-09-20 14:10:23'),
  Timestamp('2019-09-20 14:11:09'): Timestamp('2019-09-20 14:07:16'),
  Timestamp('2019-09-20 13:58:50'): Timestamp('2019-09-20 13:57:47'),
  Timestamp('2019-09-20 13:44:32'): Timestamp('2019-09-20 13:44:20'),
  Timestamp('2019-09-20 13:43:31'): Timestamp('2019-09-20 13:36:59'),
  Timestamp('2019-09-20 13:31:11'): Timestamp('2019-09-20 13:31:03'),
  Timestamp('2019-09-20 13:31:01'): Timestamp('2019-09-20 13:26:21')},
 'ExportedDate': {Timestamp('2019-09-20 14:32:02'): Timestamp('2019-09-20 14:32:02'),
  Timestamp('2019-09-20 14:20:38'): Timestamp('2019-09-20 14:20:38'),
  Timestamp('2019-09-20 14:33:39'): Timestamp('2019-09-20 14:33:39'),
  Timestamp('2019-09-20 14:11:32'): Timestamp('2019-09-20 14:11:32'),
  Timestamp('2019-09-20 14:11:09'): Timestamp('2019-09-20 14:11:09'),
  Timestamp('2019-09-20 13:58:50'): Timestamp('2019-09-20 13:58:50'),
  Timestamp('2019-09-20 13:44:32'): Timestamp('2019-09-20 13:44:32'),
  Timestamp('2019-09-20 13:43:31'): Timestamp('2019-09-20 13:43:31'),
  Timestamp('2019-09-20 13:31:11'): Timestamp('2019-09-20 13:31:11'),
  Timestamp('2019-09-20 13:31:01'): Timestamp('2019-09-20 13:31:01')},
 'ExportedBy': {Timestamp('2019-09-20 14:32:02'): 'UserA',
  Timestamp('2019-09-20 14:20:38'): 'UserB',
  Timestamp('2019-09-20 14:33:39'): 'UserA',
  Timestamp('2019-09-20 14:11:32'): 'UserD',
  Timestamp('2019-09-20 14:11:09'): 'UserD',
  Timestamp('2019-09-20 13:58:50'): 'UserD',
  Timestamp('2019-09-20 13:44:32'): 'UserD',
  Timestamp('2019-09-20 13:43:31'): 'UserF',
  Timestamp('2019-09-20 13:31:11'): 'UserG',
  Timestamp('2019-09-20 13:31:01'): 'UserG'},
 'XDStr': {Timestamp('2019-09-20 14:32:02'): '2019-09-20',
  Timestamp('2019-09-20 14:20:38'): '2019-09-20',
  Timestamp('2019-09-20 14:33:39'): '2019-09-20',
  Timestamp('2019-09-20 14:11:32'): '2019-09-20',
  Timestamp('2019-09-20 14:11:09'): '2019-09-20',
  Timestamp('2019-09-20 13:58:50'): '2019-09-20',
  Timestamp('2019-09-20 13:44:32'): '2019-09-20',
  Timestamp('2019-09-20 13:43:31'): '2019-09-20',
  Timestamp('2019-09-20 13:31:11'): '2019-09-20',
  Timestamp('2019-09-20 13:31:01'): '2019-09-20'},
 'XMStr': {Timestamp('2019-09-20 14:32:02'): '2019 09',
  Timestamp('2019-09-20 14:20:38'): '2019 09',
  Timestamp('2019-09-20 14:33:39'): '2019 09',
  Timestamp('2019-09-20 14:11:32'): '2019 09',
  Timestamp('2019-09-20 14:11:09'): '2019 09',
  Timestamp('2019-09-20 13:58:50'): '2019 09',
  Timestamp('2019-09-20 13:44:32'): '2019 09',
  Timestamp('2019-09-20 13:43:31'): '2019 09',
  Timestamp('2019-09-20 13:31:11'): '2019 09',
  Timestamp('2019-09-20 13:31:01'): '2019 09'},
 'V2X': {Timestamp('2019-09-20 14:32:02'): Timedelta('0 days 00:00:17'),
  Timestamp('2019-09-20 14:20:38'): Timedelta('0 days 00:03:00'),
  Timestamp('2019-09-20 14:33:39'): Timedelta('0 days 00:02:43'),
  Timestamp('2019-09-20 14:11:32'): Timedelta('0 days 00:00:12'),
  Timestamp('2019-09-20 14:11:09'): Timedelta('0 days 00:00:20'),
  Timestamp('2019-09-20 13:58:50'): Timedelta('0 days 00:00:51'),
  Timestamp('2019-09-20 13:44:32'): Timedelta('0 days 00:00:08'),
  Timestamp('2019-09-20 13:43:31'): Timedelta('0 days 00:02:16'),
  Timestamp('2019-09-20 13:31:11'): Timedelta('0 days 00:00:08'),
  Timestamp('2019-09-20 13:31:01'): Timedelta('0 days 00:04:40')},
 'V2Xt': {Timestamp('2019-09-20 14:32:02'): 17.0,
  Timestamp('2019-09-20 14:20:38'): 180.0,
  Timestamp('2019-09-20 14:33:39'): 163.0,
  Timestamp('2019-09-20 14:11:32'): 12.0,
  Timestamp('2019-09-20 14:11:09'): 20.0,
  Timestamp('2019-09-20 13:58:50'): 51.0,
  Timestamp('2019-09-20 13:44:32'): 8.0,
  Timestamp('2019-09-20 13:43:31'): 136.0,
  Timestamp('2019-09-20 13:31:11'): 8.0,
  Timestamp('2019-09-20 13:31:01'): 280.0},
 'AutoExported': {Timestamp('2019-09-20 14:32:02'): False,
  Timestamp('2019-09-20 14:20:38'): False,
  Timestamp('2019-09-20 14:33:39'): False,
  Timestamp('2019-09-20 14:11:32'): False,
  Timestamp('2019-09-20 14:11:09'): False,
  Timestamp('2019-09-20 13:58:50'): False,
  Timestamp('2019-09-20 13:44:32'): False,
  Timestamp('2019-09-20 13:43:31'): False,
  Timestamp('2019-09-20 13:31:11'): True,
  Timestamp('2019-09-20 13:31:01'): True}}

XMStr并且XDStr是格式化为字符串的日期,以帮助我的报告。两者都基于 ExpDate 索引。 V2X是一个时间增量,而V2Xt格式化V2X为浮点数。 HasDiscrepency是真/假值

根据这些数据,我正在尝试运行以下内容。它应该生成每月平均 V2Xt 的报告,分为 HasDiscrepency(Y 或 N):

v2x_pivot = pd.pivot_table(df['2019-07-29':'2019-09-20'],
                           index=['XMStr'],
                           columns=['HasDiscrepency'],
                           values=['V2Xt'],
                           margins=True,
                           aggfunc=[np.mean])

但我遇到了这个:


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.factorize()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable._unique()

TypeError: unhashable type: 'numpy.ndarray'

任何想法如何解决这个问题?我需要更改列中的任何内容吗?

标签: python-3.xpandaspivot-table

解决方案


您可以将值转换为列表,然后转换为数组并重新分配:

df['HasDiscrepency'] = pd.array(df['HasDiscrepency'].tolist())

然后删除[]以避免MultiIndex列中:

v2x_pivot = pd.pivot_table(df['2019-07-29':'2019-09-20'],
                           index='XMStr',
                           columns='HasDiscrepency',
                           values='V2Xt',
                           margins=True,
                           aggfunc=np.mean)
print (v2x_pivot)
HasDiscrepency  False   True   All
XMStr                             
2019 09          57.5  132.5  87.5
All              57.5  132.5  87.5

推荐阅读