polars dataframe TypeError: must be real number, not str

2024/11/16 21:53:32

so bascially i changed panda.frame to polars.frame for better speed in yolov5 but when i run the code, it works fine till some point (i dont exactly know when error occurs) and it gives me TypeError: must be real number, not str. running it with panda works great without any errors but only with polars. i know it must be using wrong type of data type but i dont really know where i should look for since i've just started python. so i would really appreciate it if someone could help me with this! thanx for reading and have a nice day!

Traceback (most recent call last):File "C:\yolov5\test.py", line 61, in <module>boxes = results.polars().xywh[0]File "c:\yolov5\.\models\common.py", line 684, in polarssetattr(new, k, [pl.DataFrame(x, columns=c) for x in a])File "c:\yolov5\.\models\common.py", line 684, in <listcomp>setattr(new, k, [pl.DataFrame(x, columns=c) for x in a])File "C:\Users\jojow\AppData\Local\Programs\Python\Python39\lib\site- packages\polars\internals\frame.py", line 311, in __init__
self._df = sequence_to_pydf(data, columns=columns, orient=orient)File 
"C:\Users\jojow\AppData\Local\Programs\Python\Python39\lib\site- 
packages\polars\internals\construction.py", line 495, in 
sequence_to_pydf
data_series = [
File 
"C:\Users\jojow\AppData\Local\Programs\Python\Python39\lib\site- packages\polars\internals\construction.py", line 496, in <listcomp>
pli.Series(columns[i], data[i], dtypes.get(columns[i])).inner()File 
"C:\Users\jojow\AppData\Local\Programs\Python\Python39\lib\site- packages\polars\internals\series.py", line 227, in __init__
self._s = sequence_to_pyseries(name, values, dtype=dtype, 
strict=strict)File "C:\Users\jojow\AppData\Local\Programs\Python\Python39\lib\site- 
packages\polars\internals\construction.py", line 239, in 
sequence_to_pyseries
return constructor(name, values, strict)
TypeError: must be real number, not str

heres my code (edited)

import polars as pl 
import pandas as pdclass new:xyxy = 0a = [[[370.01605224609375, 346.4305114746094, 398.3968811035156, 
384.5684814453125, 0.9011853933334351, 0, 'corn'], 
[415.436767578125, 279.4227294921875, 433.930419921875, 
305.5151672363281, 0.8829901814460754, 0, 'corn'], 
[383.8118896484375, 268.781494140625, 402.35479736328125, 
292.4585266113281, 0.8579609394073486, 0, 'corn'], 
[431.42791748046875, 570.9154663085938, 476.672119140625, 600.0, 
0.810459554195404, 0, 'corn'], [414.912841796875, 
257.7676086425781, 427.7708740234375, 274.69635009765625,
0.7384995818138123, 0, 'corn'], [391.22821044921875, 
250.48876953125, 403.9199523925781, 268.1374816894531, 
0.6828912496566772, 0, 'corn'], [414.2362060546875, 
250.18174743652344, 423.82537841796875, 264.02667236328125, 
0.517136812210083, 0, 'corn']]]ca = 'xmin', 'ymin', 'xmax', 'ymax', 'confidence', 'class', 'name'  # xyxy columns
cb = 'xcenter', 'ycenter', 'width', 'height', 'confidence', 'class', 'name'  # xywh columnsfor k, c in zip(['xyxy', 'xyxyn', 'xywh', 'xywhn'], [ca, ca, cb, 
cb]):setattr(new, k, [pl.DataFrame(x, columns=c) for x in a])print (new.xyxy[0])
Answer

From the information you provided, I can only provide a hint as to where to look.

Near the end of your code, you are creating a list of new DataFrame

setattr(new, k, [polars.DataFrame(x, columns=c) for x in a])

And the error is caused by this call:

polars.DataFrame(x, columns=c)

What is occurring is that one of your lists that is being passed in (x) to one of these DataFrames has a mix of numbers and strings. More specifically, one of those lists starts with one or more numbers, but contains a string somewhere after that. And this is causing an error as Polars tries to make a column of numbers from that list.

An Example

Let's take a closer look. Here is an example of creating a DataFrame:

import polars as pl
pl.DataFrame([["one", "two", "three"], [1.0, 2.0, 3.0]],columns=["col1", "col2"])

Notice that ["one", "two", "three"] are all strings. And [1.0, 2.0, 3.0] are all numbers. So, in each column, we have data of only one type. And we get no errors...

shape: (3, 2)
┌───────┬──────┐
│ col1  ┆ col2 │
│ ---   ┆ ---  │
│ str   ┆ f64  │
╞═══════╪══════╡
│ one   ┆ 1.0  │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ two   ┆ 2.0  │
├╌╌╌╌╌╌╌┼╌╌╌╌╌╌┤
│ three ┆ 3.0  │
└───────┴──────┘

Now let's see what happens when we accidentally mix a string in with the column of numbers:

pl.DataFrame([["one", "two", "three"], [1.0, 2.0, "Oops, this is a string mixed in with numbers"]],columns=["col1", "col2"])

We get an error...

Traceback (most recent call last):File "<stdin>", line 1, in <module>File "/home/xxxx/.virtualenvs/PolarsTesting3.10/lib/python3.10/site-packages/polars/internals/frame.py", line 311, in __init__self._df = sequence_to_pydf(data, columns=columns, orient=orient)File "/home/xxxx/.virtualenvs/PolarsTesting3.10/lib/python3.10/site-packages/polars/internals/construction.py", line 495, in sequence_to_pydfdata_series = [File "/home/xxxx/.virtualenvs/PolarsTesting3.10/lib/python3.10/site-packages/polars/internals/construction.py", line 496, in <listcomp>pli.Series(columns[i], data[i], dtypes.get(columns[i])).inner()File "/home/xxxx/.virtualenvs/PolarsTesting3.10/lib/python3.10/site-packages/polars/internals/series.py", line 227, in __init__self._s = sequence_to_pyseries(name, values, dtype=dtype, strict=strict)File "/home/xxxx/.virtualenvs/PolarsTesting3.10/lib/python3.10/site-packages/polars/internals/construction.py", line 239, in sequence_to_pyseriesreturn constructor(name, values, strict)
TypeError: must be real number, not str

Compare this error message with the one you received. They match closely (except for the directories, which are specific to each computer).

So, you need to look for a list that starts with one or more numbers, but contains a string. Polars tries to make a column of numbers with this list, and throws an error.

Perhaps one or more elements in a list that is meant to be numbers contain a string such as "Error" or "NULL" or "#N/A" or something similar.

You'll have to debug this to find out.

https://en.xdnf.cn/q/119149.html

Related Q&A

Get a string in Shell/Python using sys.argv

Im beginning with bash and Im executing a script :$ ./readtext.sh ./InputFiles/applications.txt Here is my readtext.sh code :#!/bin/bash filename="$1" counter=1 while IFS=: true; doline=read …

Need to combine two functions into one (Python)

Here is my code-def Max(lst):if len(lst) == 1:return lst[0]else:m = Max(lst[1:])if m > lst[0]: return melse:return lst[0] def Min(lst):if len(lst) == 1:return lst[0]else:m = Min(lst[1:])if m < ls…

Error: descriptor blit requires a pygame.Surface object but received a NoneType [duplicate]

This question already has answers here:How can I draw images and sprites in Pygame?(4 answers)Closed 2 years ago.I am creating a game. I wrote this code to create a sprite and its hitbox:hg = pygame.i…

How can I deal with overlapping rectangles? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

panda df not showing all rows after loading from MS SQL

Im using Pandas with latest sqlalchemy (1.4.36) to query a MS SQL DB, using the following Python 3.10.3 [Win] snippet: import pandas as pd # from sqlalchemy…

How to extract multiple grandchildren/children from XML where one child is a specific value?

Im working with an XML file that stores all "versions" of the chatbot we create. Currently we have 18 versions and I only care about the most recent one. Im trying to find a way to extract a…

Is there are a way to replace python import with actual sources?

I am having python files with the import statements which I would like to replace into the actual code placed in the foo.py.For instance, with the in file:from foo import Barbar = Bar() print barI woul…

How to use supported numpy and math functions with CUDA in Python?

According to numba 0.51.2 documentation, CUDA Python supports several math functions. However, it doesnt work in the following kernel function: @cuda.jit def find_angle(angles):i, j = cuda.grid(2)if i …

PYTHON - Remove tuple from list if contained in another list

I have a list of tuples:list_of_tuples = [(4, 35.26), (1, 48.19), (5, 90.0), (3, 90.0)]tuple[0] is an item_IDtuple[1] is an angleI have a list of item_IDs I want to remove/ignore from the list:ignore_I…

How to run a ij loop in Python, and not repeat (j,i) if (i,j) has already been done?

I am trying to implement an "i not equal to j" (i<j) loop, which skips cases where i = j, but I would further like to make the additional requirement that the loop does not repeat the perm…