pandas TypeError('参数必须是字符串或数字')

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56707877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:23:44  来源:igfitidea点击:

TypeError('argument must be a string or number')

pythonpandasseaborn

提问by LaLaTi

I am trying to visualize a correlation matrix for categorical variables which are first Hot-Encoded. After that, the code throws me still an error and I am not sure why.

我正在尝试将首先进行热编码的分类变量的相关矩阵可视化。在那之后,代码仍然给我一个错误,我不知道为什么。

X= pandas.DataFrame(A, B, C, D, E)
le = preprocessing.LabelEncoder()
X= X.apply(le.fit_transform)

plt.figure(figsize=(15,10))
sns.heatmap(X.corr(),vmin=-1,cmap='YlGnBu',annot=True);
plt.title("Correlation Matrix", fontsize=24)
plt.show()

TypeError('argument must be a string or number')

What am I doing wrong? Here is the full error. Please note I am using the Python script editor within Power BI to visualize.

我究竟做错了什么?这是完整的错误。请注意,我在 Power BI 中使用 Python 脚本编辑器进行可视化。

Error Message:
Python script error.
Traceback (most recent call last):
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 105, in _encode
    res = _encode_python(values, uniques, encode)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 59, in _encode_python
    uniques = sorted(set(values))
TypeError: '<' not supported between instances of 'str' and 'float'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "PythonScriptWrapper.PY", line 30, in <module>
    dataset = dataset.apply(le.fit_transform)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 6487, in apply
    return op.get_result()
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\apply.py", line 151, in get_result
    return self.apply_standard()
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\apply.py", line 257, in apply_standard
    self.apply_series_generator()
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\apply.py", line 286, in apply_series_generator
    results[i] = self.f(v)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 236, in fit_transform
    self.classes_, y = _encode(y, encode=True)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 107, in _encode
    raise TypeError("argument must be a string or number")
TypeError: ('argument must be a string or number', 'occurred at index connection')


Stack Trace:
Microsoft.PowerBI.ExploreServiceCommon.ScriptHandlerException: Python script error.
Traceback (most recent call last):
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 105, in _encode
    res = _encode_python(values, uniques, encode)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 59, in _encode_python
    uniques = sorted(set(values))
TypeError: '<' not supported between instances of 'str' and 'float'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "PythonScriptWrapper.PY", line 30, in <module>
    dataset = dataset.apply(le.fit_transform)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 6487, in apply
    return op.get_result()
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\apply.py", line 151, in get_result
    return self.apply_standard()
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\apply.py", line 257, in apply_standard
    self.apply_series_generator()
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\apply.py", line 286, in apply_series_generator
    results[i] = self.f(v)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 236, in fit_transform
    self.classes_, y = _encode(y, encode=True)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 107, in _encode
    raise TypeError("argument must be a string or number")
TypeError: ('argument must be a string or number', 'occurred at index connection')
 ---> Microsoft.PowerBI.Scripting.Python.Exceptions.PythonScriptRuntimeException: Python script error.
Traceback (most recent call last):
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 105, in _encode
    res = _encode_python(values, uniques, encode)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 59, in _encode_python
    uniques = sorted(set(values))
TypeError: '<' not supported between instances of 'str' and 'float'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "PythonScriptWrapper.PY", line 30, in <module>
    dataset = dataset.apply(le.fit_transform)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 6487, in apply
    return op.get_result()
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\apply.py", line 151, in get_result
    return self.apply_standard()
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\apply.py", line 257, in apply_standard
    self.apply_series_generator()
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\apply.py", line 286, in apply_series_generator
    results[i] = self.f(v)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 236, in fit_transform
    self.classes_, y = _encode(y, encode=True)
  File "C:\Users\abc\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\preprocessing\label.py", line 107, in _encode
    raise TypeError("argument must be a string or number")
TypeError: ('argument must be a string or number', 'occurred at index connection')

   at Microsoft.PowerBI.Scripting.Python.PythonScriptWrapper.RunScript(String originalScript, Int32 timeoutMs)
   at Microsoft.PowerBI.Client.Windows.Python.PythonScriptHandler.GenerateVisual(ScriptHandlerOptions options)
   --- End of inner exception stack trace ---
   at Microsoft.PowerBI.Client.Windows.Python.PythonScriptHandler.GenerateVisual(ScriptHandlerOptions options)
   at Microsoft.PowerBI.ExploreServiceCommon.ScriptVisualCommandFlow.RunInternal(Stream dataShapeResultStream, QueryBindingDescriptor& bindingDescriptor)
   at Microsoft.PowerBI.ExploreServiceCommon.ScriptVisualCommandFlow.Run(Stream dataShapeResultStream, QueryBindingDescriptor& bindingDescriptor)
   at Microsoft.PowerBI.ExploreHost.SemanticQuery.ExecuteSemanticQueryFlow.TransformDataShapeResult(QueryCommand transformCommand, SemanticQueryDataShapeCommand command, Stream dataShapeResultStream, QueryBindingDescriptor& bindingDescriptor)
   at Microsoft.PowerBI.ExploreHost.SemanticQuery.ExecuteSemanticQueryFlow.ExecuteDataQuery(IQueryResultDataWriter queryResultDataWriter, EngineDataModel engineDataModel, DataQuery query, Int32 queryId, ServiceErrorStatusCode& serviceErrorStatusCode, CancellationToken cancelToken)
   at Microsoft.PowerBI.ExploreHost.SemanticQuery.ExecuteSemanticQueryFlow.ProcessAndWriteSemanticQueryCommands(IQueryResultsWriter queryResultsWriter, IList`1 queries, HashSet`1 pendingQueriesToCancel, EngineDataModel engineDataModel)

Invocation Stack Trace:
   at Microsoft.Mashup.Host.Document.ExceptionExtensions.GetCurrentInvocationStackTrace()
   at Microsoft.Mashup.Client.UI.Shared.StackTraceInfo..ctor(String exceptionStackTrace, String invocationStackTrace, String exceptionMessage)
   at Microsoft.PowerBI.Client.Windows.ErrorHostService.GetErrorDetails(ShowErrorDialogArgs args)
   at Microsoft.PowerBI.Client.Windows.ErrorHostService.<>c__DisplayClass2_0.<<ShowErrorDialog>b__0>d.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)
   at Microsoft.PowerBI.Client.Windows.ErrorHostService.<>c__DisplayClass2_0.<ShowErrorDialog>b__0()
   at Microsoft.Mashup.Host.Document.SynchronizationContextExtensions.<>c__DisplayClass1_0`1.<SendAndMarshalExceptions>b__0()
   at Microsoft.Mashup.Host.Document.SynchronizationContextExtensions.<>c__DisplayClass0_1.<SendAndMarshalExceptions>b__0(Object null)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at Microsoft.Mashup.Client.UI.Shared.WebDialogs.WebDialog.<>n__0(IWindowHandle owner)
   at Microsoft.Mashup.Client.UI.Shared.WindowManager.ShowModal[T](T dialog, Func`1 showModalFunction)
   at Microsoft.PowerBI.Client.Program.<>c__DisplayClass4_0.<Main>b__1()
   at Microsoft.PowerBI.Client.Windows.IExceptionHandlerExtensions.<>c__DisplayClass3_0.<HandleExceptionsWithNestedTasks>b__0()
   at Microsoft.Mashup.Host.Document.ExceptionHandlerExtensions.HandleExceptions(IExceptionHandler exceptionHandler, Action action)
   at Microsoft.PowerBI.Client.Program.Main(String[] args)

回答by LaLaTi

I figured it out. It was a Power BI setting in the variables that were selected in the main pane which were inconsistent with the ones in the code. It needs to be the same across.

我想到了。这是在主窗格中选择的变量中的 Power BI 设置,与代码中的不一致。它必须是相同的。