0

I'm building a data pipeline using Python and I'm running into an issue when trying to execute a certain function. The error message I'm receiving is: ValueError: Could not convert string to float: 'N/A'

Here is the function in question:

def process_data(data):
    for item in data:
        # Do some processing...
        value = float(item[1])
        if value > 0:
            processed_item = process_item(item)
            yield processed_item

I'm calling the function like this:

data = [('A', '1.5'), ('B', '2.7'), ('C', 'N/A'), ('D', '4.1'), ('E', '5.9')]
processed_data = process_data(data)

Code:

def process_data(data):
    for item in data:
        # Do some processing...
        value = float(item[1])
        if value > 0:
            processed_item = process_item(item)
            yield processed_item

data = [('A', '1.5'), ('B', '2.7'), ('C', 'N/A'), ('D', '4.1'), ('E', '5.9')]
processed_data = process_data(data)

Error message:

ValueError: Could not convert string to float: 'N/A'

The expected outcome was to process the items in the data list and yield the processed items if the value of the item was greater than 0.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • What do you expect to happen in this case? A string like this does not represent a number. You need to manually handle this special case in a way that is suitable for your task. For some general information how to catch them you can lookup try..catch and type conversions. – Daraan Jan 01 '23 at 15:42
  • Can you please specify what your question is? – Alex Bochkarev Jan 01 '23 at 15:47

1 Answers1

0

The parameter value of float(parameter) must be a number or a string that can be converted into a floating point number.

The value 'N/A' cannot be converted because it is not a number.

You could try:

try:
    value = float(item[1])
except ValueError:
    value = 0

Assuming you want anything that is not a number to become zero, which will then be filtered out by your if value > 0: statement.

Stamper
  • 20
  • 5