1

I'm using Pandas to handle my data. The first step of my script is to convert a column of strings of bytes to a list of floats. My script is working but its taking too long. Any suggestions on how to speed it up??

def byte_to_hex(byte_str):
    array = ["%02X" % ord(chr(x)) for x in byte_str]
    return array

for index, row in enumerate(data[column].values):
    row_new = []
    row = byte_to_hex(row)
    stop = len(row)
    for i in range(4, stop, 4):
        sample = "".join(row[i:i + 4])
        row_new.append(struct.unpack('!f', bytes.fromhex(sample))[0])

Example:
b'\x00\x00\x00\x1cB\x80\x1f\xfcB\x80\x1f\xfcB\x80w\xc8Bz\xa1\x97B\x80\x1f\xfcB}LZB\x80\x1f\xfcBz\xa1\x97Bz\xcaoB\x803\xf5B}\xc5\x84B\x80w\xc8B}\xed\xdbB\x80\x1f\xfcB}\xc5\x84B}LZB\x80\x1f\xfcB}#\xe9B}\xed\xdbB}\xc5\x84B\x803\xf5B\x80\x1f\xfcB}\xc5\x84B\x803\xf5B\x803\xf5Bx\xef\tB\x81\xc4\xdf\x7f\xff\xff\xff'

[64.06246948242188, 64.06246948242188, 64.23394775390625, 62.65780258178711, 64.06246948242188, 63.324562072753906, 64.06246948242188, 62.65780258178711, 62.697689056396484, 64.10147857666016, 63.44288635253906, 64.23394775390625, 63.48228073120117, 64.06246948242188, 63.44288635253906, 63.324562072753906, 64.06246948242188, 63.28506851196289, 63.48228073120117, 63.44288635253906, 64.10147857666016, 64.06246948242188, 63.44288635253906, 64.10147857666016, 64.10147857666016]

Very thankful for any help :)

2
  • The strings are already in a Pandas DataFrame? Commented Sep 9, 2016 at 15:32
  • Please consider accepting if it worked so that people looking cm find its resolved. If not let me know so I can look further into it Commented Sep 9, 2016 at 20:32

1 Answer 1

2

I think you are looking for the Struct package

import struct

struct.pack('f', 3.14)
OUT: b'\xdb\x0'

struct.unpack('f', b'\xdb\x0fI@')
OUT: (3.1415927410125732,)

struct.pack('4f', 1.0, 2.0, 3.0, 4.0)
OUT: '\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.