Skip to content

Fix object dtype to string conversion#2477

Closed
RamiNoodle733 wants to merge 1 commit intovaexio:masterfrom
RamiNoodle733:fix-object-to-string-1686
Closed

Fix object dtype to string conversion#2477
RamiNoodle733 wants to merge 1 commit intovaexio:masterfrom
RamiNoodle733:fix-object-to-string-1686

Conversation

@RamiNoodle733
Copy link

Description

This PR fixes the issue where converting object dtype columns to string fails with a TypeError.

When calling .astype('str') on an object dtype column (e.g., numpy arrays with mixed types like [123, "test", None]), the to_string() function would fail because vaex.strings.to_string() doesn't have an overload for object dtype arrays.

Fix

Modified the to_string() function in packages/vaex-core/vaex/functions.py to handle object dtype arrays by converting them to string arrays using ar.astype(str) before passing to vaex.strings.to_string().

Changes

  • Added dtype check for object arrays in to_string()
  • Convert object arrays to string arrays before processing
  • Handles both regular and masked arrays

Example

import vaex
import numpy as np

# This now works correctly
arr = np.array([123, "test", None], dtype=object)
df = vaex.from_arrays(test=arr)
df['test'] = df['test'].astype('str')  # No longer raises TypeError

Fixes #1686

Handle object dtype arrays in to_string() by converting to string
arrays before passing to vaex.strings.to_string(). This fixes the
TypeError when calling .astype('str') on object dtype columns.

Fixes #1686
else:
# Handle object dtype by converting to string array first
if ar.dtype == object:
ar = ar.astype(str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the same thing happens for both if and else branches, it can be moved out of the if else entirely

@RamiNoodle733 RamiNoodle733 closed this by deleting the head repository Feb 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG-REPORT] Issue converting object to string

2 participants