0

I have a process where I need to convert a numpy recarray to bytes, and after that reconstruct the recarray from the bytes.

However, I am not sure how to do recover the array from bytes.

Does anyone know how could I do it?

Example code:

import numpy as np
import pandas as pd

df = pd.DataFrame(np.zeros(500))
rec = df.to_records()
rec_s = rec.tostring()  # this returns a bytes object

# perform some computation

new_rec = <method to recover from bytes>(rec_s)

Note: I don't actually need to use numpy recarry, just some structure that will allow me to transform the pandas dataframe into a bytes object, and also recover it.

Daniel Lima
  • 925
  • 1
  • 8
  • 22

1 Answers1

1
In [497]: arr = np.ones(3, dtype='i,i,f')                                                      
In [498]: arr                                                                                  
Out[498]: 
array([(1, 1, 1.), (1, 1, 1.), (1, 1, 1.)],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<f4')])
In [499]: astr = arr.tostring()                                                                
In [500]: astr                                                                                 
Out[500]: b'\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x80?\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x80?\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x80?'

Recover it using the same dtype:

In [502]: np.frombuffer(astr, arr.dtype)                                                       
Out[502]: 
array([(1, 1, 1.), (1, 1, 1.), (1, 1, 1.)],
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<f4')])

If the source was 2d, you'd have to reshape as well

hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • @MatheusTorquato, a comment to an old question is not the place to ask about a new error, especially since the unstated code has no apparent relation to code in the answer (or OP question). – hpaulj Jul 20 '21 at 16:28