python - 使用 pandas 从 CSV 中读取十进制表示形式的 float

标签 python pandas numpy csv ieee-754

我正在尝试读取 CSV 文件的内容,其中包含我认为是十进制格式的 IEEE 754 单精度 float 。

默认情况下,它们以 int64 形式读入。如果我使用 dtype = {'col1' : np.float32} 之类的内容指定数据类型,则 dtype 会正确显示为 float32,但它们与 float 而不是 int 的值相同, IE。 1079762502 变为 1.079763e+09 而不是 3.435441493988037

我已成功使用以下任一方法对单个值进行转换:

from struct import unpack

v = 1079762502

print(unpack('>f', v.to_bytes(4, byteorder="big")))
print(unpack('>f', bytes.fromhex(str(hex(v)).split('0x')[1])))

哪个产生

(3.435441493988037,)
(3.435441493988037,)

但是,我似乎无法使用 pandas 以矢量化方式实现此目的:

import pandas as pd
from struct import unpack

df = pd.read_csv('experiments/test.csv')

print(df.dtypes)
print(df)

df['col1'] = unpack('>f', df['col1'].to_bytes(4, byteorder="big"))
#df['col1'] = unpack('>f', bytes.fromhex(str(hex(df['col1'])).split('0x')[1]))

print(df)

抛出以下错误

col1    int64
dtype: object
         col1
0  1079762502
1  1079345162
2  1078565306
3  1078738012
4  1078635652

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-c06d0986cc96> in <module>
      7 print(df)
      8 
----> 9 df['col1'] = unpack('>f', df['col1'].to_bytes(4, byteorder="big"))
     10 #df['col1'] = unpack('>f', bytes.fromhex(str(hex(df['col1'])).split('0x')[1]))
     11 

~/anaconda3/envs/test/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5177             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5178                 return self[name]
-> 5179             return object.__getattribute__(self, name)
   5180 
   5181     def __setattr__(self, name, value):

AttributeError: 'Series' object has no attribute 'to_bytes'

或者,如果我尝试第二种方式,TypeError:“Series”对象无法解释为整数

我的 Python 知识有限,我想我可以迭代每一行,转换为十六进制,然后转换为字符串,然后剥离 0x,解压并存储。但这似乎非常复杂,并且在较小的样本数据集上已经花费了几秒钟的时间,更不用说数十万个条目了。我在这里错过了一些简单的事情吗?有更好的方法吗?

最佳答案

CSV 是文本格式,IEEE 754 单精度 float 是二进制数字格式。如果你有 CSV,你就有文本,但它根本不是那种格式。如果我理解正确,我认为您的意思是您有代表整数(十进制格式)的文本,这些文本对应于 32 位 float 的 32 位整数解释。

因此,对于初学者来说,当您从 csv 读取数据时,pandas 默认情况下使用 64 位整数。因此转换为 32 位整数,然后使用 .view 重新解释字节:

In [8]: df
Out[8]:
         col1
0  1079762502
1  1079345162
2  1078565306
3  1078738012
4  1078635652

In [9]: df.col1.astype(np.int32).view('f')
Out[9]:
0    3.435441
1    3.335940
2    3.150008
3    3.191184
4    3.166780
Name: col1, dtype: float32

分解为多个步骤以帮助理解:

In [10]: import numpy as np

In [11]: arr = df.col1.values

In [12]: arr
Out[12]: array([1079762502, 1079345162, 1078565306, 1078738012, 1078635652])

In [13]: arr.dtype
Out[13]: dtype('int64')

In [14]: arr_32 = arr.astype(np.int32)

In [15]: arr_32
Out[15]:
array([1079762502, 1079345162, 1078565306, 1078738012, 1078635652],
      dtype=int32)

In [16]: arr_32.view('f')
Out[16]:
array([3.4354415, 3.33594  , 3.1500077, 3.191184 , 3.1667795],
      dtype=float32)

关于python - 使用 pandas 从 CSV 中读取十进制表示形式的 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63204145/

相关文章:

python - 用于 pandas 数据帧时无法看到 drop_duplicates 的影响

python - 平滑逼近 floor 函数以用于反向传播

python - 根据值以特定顺序遍历 numpy 数组

python - 使用什么 NLP 工具来匹配具有相似含义或语义的短语

python - 您如何将 KeyManager 添加到使用 moto 模拟的 kms key 中

python - Pandas 中的多个条件

python - 使用 MultiIndex 时如何将此 Pandas 列类型保留为日期时间?

python - 修改矩阵以包含各种数据特征

python - 单击按钮时如何禁用按钮?

python - 每天重新采样到每月一次,并在 pandas 中偏移 'month-end minus t days'