Eric Jensen
Eric Jensen

Reputation: 303

Python How to convert a float as hex to decimal

I've read in some data from a csv file with pandas. The data is incomplete and therefore contains many nan values. I want to add a column to the data which converts the hex values to decimal values. Unfortunately, the column with the hex values are all read as floats, not strings because they just happen to have those values. Example data

   val
0 20.0
1  nan
2 20.0

The simple way to convert a hex to decimal in python seems to be:int('20.0',16), which should yield 32.

However, since this is pandas I cannot convert the values to int, or at least I keep getting an error stating that. My current code is:

df['valdec'] = np.where(np.isnan(df['val']), 
                    df['val'], 
                    int(df['val'].astype(int).astype(str), 16))

This fails with the error:

ValueError: Cannot convert NA to integer

without the astype(int) the value is "20.0" which cannot be converted. Is there another way to interpret a float value as a hex value and convert to decimal when working with pandas dataframe?

Upvotes: 3

Views: 2552

Answers (1)

EdChum
EdChum

Reputation: 394189

You can mask the rows of interest and double cast and call apply:

In [126]:
df['valdec'] = df['val'].dropna().astype(int).astype(str).apply(lambda x: int(x, 16))
df

Out[126]:
    val  valdec
0  20.0    32.0
1   NaN     NaN
2  20.0    32.0

So firstly we call dropna to remove the NaN, this allows us to cast to int using .astype(int) then convert to str by calling .astype(str).

We then call apply on this to convert to hex and assign the result of all this to the new column

Note that the dtype of the new column will be float as the presence of NaN enforces this, you won't be able to have a mixture of ints and floats

As pointed out by @jasonharper, casting to int here will lose any fractional part so a higher precision method would be to use float.fromhex:

In [128]:
df['valdec'] = df['val'].astype(str).dropna().apply(lambda x: float.fromhex(x))
df

Out[128]:
    val  valdec
0  20.0    32.0
1   NaN     NaN
2  20.0    32.0

Upvotes: 4

Related Questions