草庐IT

img_float

全部标签

python - 如何在不丢失精度的情况下将 float 存储为文本?

正如问题所说。转换为(截断的)字符串表示形式/从(截断的)字符串表示形式转换会影响它们的精度。但是以pickle等其他格式存储它们会使它们变得不可读(是的,我也想要这个)。如何在不损失精度的情况下在文本中存储float? 最佳答案 以二进制或其幂的形式存储。>>>(3.4).hex()'0x1.b333333333333p+1'>>>float.fromhex('0x1.b333333333333p+1')3.4 关于python-如何在不丢失精度的情况下将float存储为文本?,我们在

python - 使用 Matplotlib.dates.datestr2num 将 pandas DatetimeIndex 转换为 'float days format'

一些Matplotlib方法需要几天'floatdaysformat'.datestr2num是一个转换器函数,但它与相关的pandas对象有关:In[3]:type(df.index)Out[3]:pandas.tseries.index.DatetimeIndexIn[4]:type(df.index[0])Out[4]:pandas.tslib.TimestampIn[5]:mpl.dates.date2num(df.index)Out[5]:...AttributeError:'numpy.datetime64'objecthasnoattribute'toordinal'这提

python - dtype : integer, 但 loc 返回 float

我有一个奇怪的数据集:yearfirmsagesurvival019775649180NaN219785039910NaN3197841313010.731310519794978050NaN6197939035210.774522我将前三列的dtype转换为整数:>>>df.dtypesyearint64firmsint64ageint64survivalfloat64但现在我想根据这里的索引在另一个表中搜索:idx=331otherDf.loc[df.loc[idx,'age']]Traceback(mostrecentcalllast):(...)KeyError:8.0这来自d

python - 在 Python 中比较两个 float 是否相等

这个问题在这里已经有了答案:Whatisthebestwaytocomparefloatsforalmost-equalityinPython?(18个答案)关闭7年前。在Python中比较两个float时,我看到代码总是这样比较一个小值epsilon,想知道选择正确epsilon值的最佳做法是什么?而背后的场景又是怎样的呢?谢谢。epsilon=0.000001abs(a-b)

python - 具有表示错误的四舍五入 float 到最接近和正确的结果

我有一个情境当经典representationerror在Python中开始成为一个问题:我需要将它们用于Numpy中的Matrix运算,并且decimal类型尚不支持。你们都知道如果我执行111.85*111.85我会得到12510.422499999999但是如果我round(12510.422499999999,4)我可以得到正确的结果当然是12510.4225。但实际的问题是:这一轮的事情是个好主意和好做法吗?这对所有情况都有效吗?有时..999小数点的小数点位置可能更多最后,如何获得适当的小数位数以用于所有可能值的舍入? 最佳答案

python - 将数据框中某些列的 Float 转换为 Int

我正在尝试将第0列转换为第4列,并将第6列从当前的浮点类型转换为整数。我试过:df[0:4,6].astype(int)但这当然行不通... 最佳答案 考虑dfdf=pd.DataFrame(np.random.rand(10,10)*10)使用np.r_获取slcslc=np.r_[0:4,6]df[slc]=df[slc].astype(int)df或者传递一个以键作为列名的类型字典df.astype({c:intforcinslc}) 关于python-将数据框中某些列的Float

python - 为什么 NaN 被视为 float ?

在pandas中,当我们尝试将包含NaN值的系列转换为带有如下代码片段的整数时df.A=df.A.apply(int),我经常看到错误信息ValueError:cannotconvertfloatNaNtointeger我了解NaN值无法转换为整数。但我对这种情况下抛出的ValueError很好奇。它说floatNaN无法转换为整数。NaN值被视为浮点对象有什么具体原因吗?还是显示的错误消息存在某些问题? 最佳答案 简短的回答是IEEE754将NaN指定为float值。至于如何将pd.Series转换为特定的数字数据类型,我更喜欢使

python - 在 Python 中将分数列表转换为 float

我有一个分数列表,例如:data=['24/221','25/221','24/221','25/221','25/221','30/221','31/221','31/221','31/221','31/221','30/221','30/221','33/221']我将如何将它们转换为float,例如data=['0.10','0.11','0.10','0.11','0.13','0.14','0.14','0.14','0.14','0.14','0.13','0.13','0.15']Fraction模块似乎只能转换为Fractions(不是来自)并且float([x])需要

python - 在 Python 中将 float.hex() 值转换为二进制

我想知道如何将float.hex()返回的结果转换为二进制,例如,从0x1.a000000000000p+2到110.1.有人可以帮忙吗?谢谢。 最佳答案 deffloat_to_binary(num):exponent=0shifted_num=numwhileshifted_num!=int(shifted_num):shifted_num*=2exponent+=1ifexponent==0:return'{0:0b}'.format(int(shifted_num))binary='{0:0{1}b}'.format(int

python - 'numpy.float6 4' object has no attribute ' translate' 在 Python 中向 Mysql 插入值

importdatasetdb=dataset.connect(....)table=db[...]当我尝试向Mysql表中插入一些值时,发生了这个错误。我要插入到表中的示例值:print("Buy",ticker,price,date,OType,OSize)BuyAAPL93.43571428572016-05-12Market200data=dict(Order_Side='Buy',Ticker=ticker,Price=price,Order_Date=date,Order_Type=OType,Volume=OSize)table.insert(data)错误信息:Trac