0

I am reading data from Excel columns and saving it in a numpy array with a self written function. I Have a problem with only one of them.

Here is the function:

import numpy as np
import xlrd
import os

# Data Path
input_data_path = os.path.join(os.path.dirname(__file__), "..", r"Input.xlsx")


def read_excel_file_column(filepath, sheetpage, column=0):
    """Gives back a Excel-column as array"""

    # file
    book = xlrd.open_workbook(filepath, encoding_override="utf-8")

    # sheet
    sheet = book.sheet_by_index(sheetpage)

    # numpy-Array
    data = np.array([sheet.cell(i, column).value for i in range(sheet.nrows)])
    return data

Then I am calling the function and reading a column from my excel file which has a string in its first line and after that 49 float values and print it out

The actual excel column

time_robot = read_excel_file_column(input_data_path, 2, column=5)
print(time_robot)

Here is the output:

['CTRobot' '3.9000000000000004' '0.8999999999999999' '4.199999999999999'
 '6.75' '5.25' '4.35' '6.8999999999999995' '10.35' '3.3000000000000003'
 '3.3000000000000003' '38.25' '6.1499999999999995' '7.199999999999999'
 '4.050000000000001' '2.4000000000000004' '2.4000000000000004' '1.0'
 ...]

I don't understand why there are so many post decimal positions.

I checked my Excel sheet and removed all foormatting, to make sure it not just shows me the rounded values or something

Whats also very interesting is, that it works perfectly fine with column "E" which is basically the same: Column E

Function Call:

time_human = read_excel_file_column(input_data_path, 2, column=4)

Output:

['CTHuman' '2.6' '0.6' '2.8' '4.5' '3.5' '2.9' '4.6' '6.9' '2.2' '2.2'
 '25.5' '4.1' '4.8' '2.7' '1.6' '1.6' '9.2' ...]

I am working on Windows 10 and with python 3.7.5.

sshashank124
  • 31,495
  • 9
  • 67
  • 76
ndndinger
  • 73
  • 5
  • 1
    https://stackoverflow.com/questions/588004/is-floating-point-math-broken – sshashank124 Jan 06 '20 at 13:19
  • if you want to remove the decimals, you could use a try except statement on each thing in the list, turning them into floats, and and rounding them if it can. – Frasher Gray Jan 06 '20 at 13:20
  • Have you checked to see what the difference is between the raw values you get from `sheet.cell(i, column).value` for the two columns? That seems key. – martineau Jan 06 '20 at 13:35
  • 2
    There is something going on beyond normal floating point behavior. For example, the closest IEEE 754 64-bit binary float to 3.9 is 3.899999999999999911182158029987476766109466552734375. The next number up is 3.9000000000000003552713678800500929355621337890625, which is less close to 3.9. Is there a difference in how the two columns were created? – Patricia Shanahan Jan 06 '20 at 14:09
  • all: thank you very much for the very quick answer @martineau: no i haven't, you mean just printing them out and see if there are any differences? patricis: no i already re-did it and payed attention that they are created equally – ndndinger Jan 06 '20 at 14:31
  • Yes, I meant just print them out and see how their values are different — because that likely will be a clue to the cause of the problem. – martineau Jan 06 '20 at 14:33

0 Answers0