ValueError:新名称的长度必须为 1,得到 2
Posted
技术标签:
【中文标题】ValueError:新名称的长度必须为 1,得到 2【英文标题】:ValueError: Length of new names must be 1, got 2 【发布时间】:2022-01-01 14:51:28 【问题描述】:我正在尝试重新采样时间序列,参考另一个表中的开始和结束数据,如下所示。数据如下:
df1:
Index Timestamp Data ID
2 1 2 2014-03-04 13:16:44.310 30.0 2
6 8 2014-03-04 13:17:01.777 30.0 2
37 47 2014-04-17 11:59:57.470 25.0 2
df2:
ID Start Date End Date comment Name
8 10 2014-04-20 2014-04-30 TBA NN95
9 2 2014-03-04 2014-03-14 TBA AA01
116 120 2014-04-17 2014-04-27 TBA BB10
可重现的例子:
df1:
from pandas import Timestamp
df1 = pd.DataFrame('Index': (2, 1): 2,
(2, 6): 8,
(2, 37): 47,
(2, 81): 92,
(2, 88): 101,
(2, 132): 146,
(2, 139): 155,
(2, 436): 453,
(2, 545): 564,
(2, 816): 835,
(10, 172): 188,
(10, 450): 469,
(10, 565): 584,
(10, 830): 849,
(10, 1000): 1019,
(10, 271312): 271331,
(10, 271313): 271332,
(10, 271314): 271333,
(10, 271315): 271334,
(10, 271316): 271335,
(120, 1614): 1633,
(120, 1665): 1684,
(120, 1666): 1685,
(120, 1733): 1752,
(120, 1734): 1753,
(120, 1835): 1854,
(120, 1836): 1855,
(120, 1957): 1976,
(120, 1958): 1977,
(120, 2091): 2110,
'Timestamp': (2, 1): Timestamp('2014-03-04 13:16:44.310000'),
(2, 6): Timestamp('2014-03-04 13:17:01.777000'),
(2, 37): Timestamp('2014-04-17 11:59:57.470000'),
(2, 81): Timestamp('2014-04-17 12:01:08.973000'),
(2, 88): Timestamp('2014-04-17 12:05:55.153000'),
(2, 132): Timestamp('2014-04-17 12:08:58.933000'),
(2, 139): Timestamp('2014-04-17 12:35:58.290000'),
(2, 436): Timestamp('2014-04-17 12:41:42.147000'),
(2, 545): Timestamp('2014-04-17 12:46:14.450000'),
(2, 816): Timestamp('2014-04-17 13:05:53.077000'),
(10, 172): Timestamp('2014-04-17 12:35:58.633000'),
(10, 450): Timestamp('2014-04-17 12:41:42.067000'),
(10, 565): Timestamp('2014-04-17 12:46:14.747000'),
(10, 830): Timestamp('2014-04-17 13:05:53.153000'),
(10, 1000): Timestamp('2014-04-17 13:10:20.127000'),
(10, 271312): Timestamp('2014-05-13 14:59:44.627000'),
(10, 271313): Timestamp('2014-05-13 14:59:44.780000'),
(10, 271314): Timestamp('2014-05-13 14:59:45.600000'),
(10, 271315): Timestamp('2014-05-13 14:59:45.757000'),
(10, 271316): Timestamp('2014-05-13 14:59:46.687000'),
(120, 1614): Timestamp('2014-04-17 15:39:52.673000'),
(120, 1665): Timestamp('2014-04-17 15:46:41.260000'),
(120, 1666): Timestamp('2014-04-17 15:46:41.417000'),
(120, 1733): Timestamp('2014-04-17 16:07:54.657000'),
(120, 1734): Timestamp('2014-04-17 16:07:54.817000'),
(120, 1835): Timestamp('2014-04-17 16:23:59.943000'),
(120, 1836): Timestamp('2014-04-17 16:24:00.103000'),
(120, 1957): Timestamp('2014-04-17 16:53:00.543000'),
(120, 1958): Timestamp('2014-04-17 16:53:00.703000'),
(120, 2091): Timestamp('2014-04-17 17:29:21.163000'),
'Data': (2, 1): 30.0,
(2, 6): 30.0,
(2, 37): 25.0,
(2, 81): 25.0,
(2, 88): 25.0,
(2, 132): 25.0,
(2, 139): 25.0,
(2, 436): 25.0,
(2, 545): 25.0,
(2, 816): 25.0,
(10, 172): 25.0,
(10, 450): 25.0,
(10, 565): 25.0,
(10, 830): 25.0,
(10, 1000): 25.0,
(10, 271312): 25.0,
(10, 271313): 27.5,
(10, 271314): 27.5,
(10, 271315): 30.5,
(10, 271316): 30.5,
(120, 1614): 31.0,
(120, 1665): 30.5,
(120, 1666): 30.0,
(120, 1733): 29.5,
(120, 1734): 29.0,
(120, 1835): 28.5,
(120, 1836): 28.0,
(120, 1957): 27.5,
(120, 1958): 27.0,
(120, 2091): 26.5,
'ID': (2, 1): 2,
(2, 6): 2,
(2, 37): 2,
(2, 81): 2,
(2, 88): 2,
(2, 132): 2,
(2, 139): 2,
(2, 436): 2,
(2, 545): 2,
(2, 816): 2,
(10, 172): 10,
(10, 450): 10,
(10, 565): 10,
(10, 830): 10,
(10, 1000): 10,
(10, 271312): 10,
(10, 271313): 10,
(10, 271314): 10,
(10, 271315): 10,
(10, 271316): 10,
(120, 1614): 120,
(120, 1665): 120,
(120, 1666): 120,
(120, 1733): 120,
(120, 1734): 120,
(120, 1835): 120,
(120, 1836): 120,
(120, 1957): 120,
(120, 1958): 120,
(120, 2091): 120
)
df2:
df2 = pd.DataFrame('ID': 8: 10, 9: 2, 116: 120,
'Start Date': 8: Timestamp('2014-04-20 00:00:00'),
9: Timestamp('2014-03-04 00:00:00'),
116: Timestamp('2014-04-17 00:00:00'),
'End Date': 8: Timestamp('2014-04-30 00:00:00'),
9: Timestamp('2014-03-14 00:00:00'),
116: Timestamp('2014-04-27 00:00:00'),
'comment': 8: 'TBA', 9: 'TBA', 116: 'TBA',
'Name': 8: 'NN95', 9: 'AA01', 116: 'BB10')
我需要通过映射ID
,根据df2
中的开始和结束日期过滤df1
中的时间序列数据。然后我想在开始日期和结束日期之间以 1 分钟的频率重新采样数据(因此每个 ID
最终将具有相同数量的数据点)。最后,我想用pivot_table
转置数据。
我试过了:
for i, j in df2.iterrows():
current_id = df2.at[i, 'ID']
start_date = df2.at[i, 'Start Date']
end_date = df2.at[i, 'End Date']
sub1 = df1[(df1.Timestamp >= start_date) & (df1.Timestamp <= end_date) & (df1.ID == current_id )]
def f(x):
r = pd.date_range(start=start_date, end = end_date, freq='1min')
return x.reindex(r, method='ffill').bfill()
sub2 = (sub1.set_index('Timestamp').groupby('ID', sort=False)['Data'].apply(f).rename_axis(['ID','Timestamp']).reset_index())
df_sub1 = sub2.pivot_table('Data', 'ID', sub2.groupby('ID').cumcount()).add_prefix('x')
print(df_sub1)
它发现了错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-20-27c1ac59f909> in <module>
10 return x.reindex(r, method='ffill').bfill()
11
---> 12 sub2 = (sub1.set_index('Timestamp').groupby('ID', sort=False)['Data'].apply(f).rename_axis(['ID','Timestamp']).reset_index())
13
14 df_sub1 = sub2.pivot_table('Data', 'ID', df_sub.groupby('ID').cumcount()).add_prefix('x')
~\AppData\Roaming\Python\Python38\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
307 @wraps(func)
308 def wrapper(*args, **kwargs) -> Callable[..., Any]:
--> 309 return func(*args, **kwargs)
310
311 kind = inspect.Parameter.POSITIONAL_OR_KEYWORD
~\AppData\Roaming\Python\Python38\site-packages\pandas\core\generic.py in rename_axis(self, mapper, **kwargs)
1106 )
1107 if non_mapper:
-> 1108 return self._set_axis_name(mapper, axis=axis, inplace=inplace)
1109 else:
1110 raise ValueError("Use `.rename` to alter labels with a mapper.")
~\AppData\Roaming\Python\Python38\site-packages\pandas\core\generic.py in _set_axis_name(self, name, axis, inplace)
1180 """
1181 axis = self._get_axis_number(axis)
-> 1182 idx = self._get_axis(axis).set_names(name)
1183
1184 inplace = validate_bool_kwarg(inplace, "inplace")
~\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexes\base.py in set_names(self, names, level, inplace)
1312 else:
1313 idx = self._shallow_copy()
-> 1314 idx._set_names(names, level=level)
1315 if not inplace:
1316 return idx
~\AppData\Roaming\Python\Python38\site-packages\pandas\core\indexes\base.py in _set_names(self, values, level)
1227 raise ValueError("Names must be a list-like")
1228 if len(values) != 1:
-> 1229 raise ValueError(f"Length of new names must be 1, got len(values)")
1230
1231 # GH 20527
ValueError: Length of new names must be 1, got 2
错误是什么意思?是说我使用了重复的变量名吗?任何帮助表示赞赏。
【问题讨论】:
.rename_axis(['ID','Timestamp'])
你检查了吗?
最后第二行循环中的df_sub
是什么?
您是否尝试将ID
索引重命名为Timestamp
?
@AnuragDhadse 抱歉打错了,应该是sub2
【参考方案1】:
轴名称只能是一个,不能是两个。所以当你调用这个函数时,名字应该只有一个而不是两个:
# this will give error
.rename_axis(['ID','Timestamp'])
问题是您正在使用需要重命名的多索引。 这是可能的解决方案:
.rename_axis(index='ID': 'Timestamp')
查看此 Pandas 文档https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rename_axis.html
【讨论】:
嗨 Anurag,感谢您的解决方案,但似乎并没有解决问题,它返回了ValueError: Length of names must match number of levels in MultiIndex.
让我检查一下是否可以提供更好的答案。在此之前检查上面的链接以获取文档。
@nilsinelabore 检查新答案是否有帮助。并用此 df_sub1 = sub2.pivot_table('Data', 'Timestamp', sub2.groupby('Timestamp').cumcount()).add_prefix('x')
更改下一行。如果这是你打算做的?
不幸的是它发现了错误KeyError: 'ID'
用Timestamp
更新循环的最后一行,ID
以上是关于ValueError:新名称的长度必须为 1,得到 2的主要内容,如果未能解决你的问题,请参考以下文章
ValueError:无法强制转换为 Series,长度必须为 1:给定 n
ValueError:在昏暗 2 处预期长度为 0 的序列(得到 1)
我试图在seaborn中旋转标签45°,但得到ValueError: "Grouper和轴的长度必须相同"
AES 解密抛出 ValueError:输入字符串的长度必须是 16 的倍数
Python:ValueError:类的数量必须大于一;得到 1
ValueError: 标签形状必须为 [batch_size, labels_dimension],得到 (128, 2)