TypeError(“参数必须是字符串或数字”)

时间:2019-06-21 17:14:12

标签: python pandas seaborn

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

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')

我在做什么错?这是完整的错误。请注意,我正在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)

1 个答案:

答案 0 :(得分:1)

我知道了。这是在主窗格中选择的变量中的Power BI设置,该设置与代码中的设置不一致。跨度必须相同。