Conan Moon
Conan Moon

Reputation: 1

How can I convert Vaex dataframe to numpy array?

I am trying to convert my Vaex dataframe into Numpy arrays so that I can use them for my own purpose. However, it keeps throwing the following error message:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "autotrans_vaex_loading.py", line 348, in <lambda>
    output_file_button = create_button(root, "Start Transformation", 30, 10, command=lambda: apply_transformation(e_input_file, e_output_file), fg="white", bg="blue")
  File "autotrans_vaex_loading.py", line 255, in apply_transformation
    points = read_pts_file(e_input_file.get())
  File "autotrans_vaex_loading.py", line 163, in read_pts_file
    x = df['x'].values  # Extract x column
  File "c:\Users\MoonSeungtae\OneDrive - 아라엔지니어링(주)\문서\2. Work\python projects\test_python_alignment\.venv\lib\site-packages\vaex\expression.py", line 621, in __getitem__
    raise NotImplementedError
NotImplementedError

My entire code is about 400 lines long so I will only post the part that seems problematic only. It is the read_pts_file() method, which takes the input of filename from the user.

# Function to read pts file using Vaex
def read_pts_file(filename):

    # Create a temporary file
    with tempfile.NamedTemporaryFile(delete=False, mode='w', newline='') as temp_file:
        # Read the original .pts file and write to the temporary file while skipping the first row
        with open(filename, 'r') as original_file:
            original_file.readline()  # Skip the first line
            for line in original_file:
                temp_file.write(line)  # Write the rest of the lines

        temp_filename = temp_file.name  # Save the name of the temporary file
        print("Input file path:", filename)
        print("Temporary file path:", temp_filename)

    # Use Vaex to read from the temporary file
    df = vaex.from_csv(temp_filename, sep=' ', convert=True, header=None)

    # Only take the first 3 columns of the data
    df = df[[0, 1, 2]]

    # Rename columns for clarity (optional)
    df.columns = ['x', 'y', 'z']
    
    # Extract each column as a NumPy array
    x = df['x'].values  # Extract x column
    y = df['y'].values  # Extract y column
    z = df['z'].values  # Extract z column

    # Stack x, y, z arrays into a single NumPy array with shape (n, 3)
    points = np.column_stack((x, y, z))

    # Clean up the temporary file
    os.remove(temp_filename)

    # Return points and color data
    return points

It doesn't seem exactly the same as how Pandas dataframe works. I have read the API docs for Vaex but I am still not certain whether it supports all the methods that Pandas does.

If anyone knows how to convert Vaex dataframe into numpy array, I would appreciate your help.

Upvotes: 0

Views: 33

Answers (0)

Related Questions