Pyarrow.lib.arrowtypeerror: expected a string or bytes dtype, got int64

pyarrow.lib.arrow.TypeError: expected a string or bytes dtype, got int64

This error occurs when working with PyArrow library in Python and it expects a variable of type string or bytes, but you provided it with a variable of type int64 (integer).

Example:

    import pyarrow as pa

    # Correct usage of PyArrow
    string_data = "Hello, PyArrow"
    pyarrow_array = pa.array([string_data])
  
    # Incorrect usage of PyArrow
    int_data = 123
    pyarrow_array = pa.array([int_data])  # Throws an arrow.TypeError
  

Solution:

To resolve this error, ensure that the data you are passing to PyArrow is of the correct dtype, which is either string or bytes. If you have an integer or any other datatype, you need to convert it into a string or bytes before passing it to PyArrow.

    import pyarrow as pa

    # Correct usage with integer converted to string
    int_data = 123
    string_data = str(int_data)
    pyarrow_array = pa.array([string_data])  # No error thrown
    
    # Correct usage with integer converted to bytes
    int_data = 123
    bytes_data = bytes(str(int_data), 'utf-8')
    pyarrow_array = pa.array([bytes_data])  # No error thrown
  

Leave a comment