‘float’对象没有’rint’属性

huangapple go评论154阅读模式
英文:

'float' object has no attribute 'rint'

问题

DataFrame的所有单元格都是浮点型,但仍然无法四舍五入。

DataFrame

‘float’对象没有’rint’属性

数据类型
‘float’对象没有’rint’属性

我正在执行以下操作:

df_lmsd['LMSD']=np.around(df_lmsd['LMSD'],2)
df_lmsd['LMSD(#)']=np.around(df_lmsd['LMSD(#)'],2)
df_lmsd

输出结果:
‘float’对象没有’rint’属性

Python版本:Python 3.10.9
IDE:Jupyter Notebook

英文:

All the cells of DataFrame are of float type but still it is not able to round off.

DataFrame

‘float’对象没有’rint’属性

Dtype
‘float’对象没有’rint’属性

I am doing this:

df_lmsd['LMSD']=np.around(df_lmsd['LMSD'],2)
df_lmsd['LMSD(#)']=np.around(df_lmsd['LMSD(#)'],2)
df_lmsd

Output:-
‘float’对象没有’rint’属性

Python Version:- Python 3.10.9
Ide:- Jupyter Notebook

答案1

得分: 3

存储类型为object的LMSD列,而不是float。为了演示这一点,我已经重现了错误。


输入数据(将LMSD列存储为float类型)

import pandas as pd
import numpy as np

data = {
    "created_date": ["2023-03-01", "2023-03-03", "2023-03-07", "2023-03-08", "2023-03-09"],
    "lmsd": [99.701135, 100.0, 4.142565, 14.182277, 24.433356],
    "lmsd_hash": [99.853902, 100.0, 4.579792, 14.58302, 23.937297]
}

df = pd.DataFrame(data)

# 将日期转换为日期时间对象
df["created_date"] = pd.to_datetime(df["created_date"])

print(df)
  created_date        lmsd   lmsd_hash
0   2023-03-01   99.701135   99.853902
1   2023-03-03  100.000000  100.000000
2   2023-03-07    4.142565    4.579792
3   2023-03-08   14.182277   14.583020
4   2023-03-09   24.433356   23.937297

df.info()的输出:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
 #   Column        Non-Null Count  Dtype         
---  ------        --------------  -----         
 0   created_date  5 non-null      datetime64[ns]
 1   lmsd          5 non-null      float64       
 2   lmsd_hash     5 non-null      float64       
dtypes: datetime64[ns](1), float64(2)
memory usage: 248.0 bytes

将LMSD列存储为object类型

# 将LMSD列表示为对象
df[["lmsd", "lmsd_hash"]] = df[["lmsd", "lmsd_hash"]].astype(object)

print(df)
  created_date       lmsd  lmsd_hash
0   2023-03-01  99.701135  99.853902
1   2023-03-03      100.0      100.0
2   2023-03-07   4.142565   4.579792
3   2023-03-08  14.182277   14.58302
4   2023-03-09  24.433356  23.937297

(请注意,这与您的DataFrame匹配)

df.info()的输出:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
 #   Column        Non-Null Count  Dtype         
---  ------        --------------  -----         
 0   created_date  5 non-null      datetime64[ns]
 1   lmsd          5 non-null      object        
 2   lmsd_hash     5 non-null      object        
dtypes: datetime64[ns](1), object(2)
memory usage: 248.0+ bytes

尝试在object列中四舍五入

# 尝试在LMSD列中(表示为对象)四舍五入值
df['lmsd']  = np.around(df['lmsd'], 2)
df['lmsd_hash']  = np.around(df['lmsd_hash'], 2)

会产生以下错误,与您收到的错误相匹配:

TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method

在四舍五入之前将列表示为float

为了解决这个问题,确保在四舍五入之前将LMSD列存储为float,使用astype()方法:

# 将LMSD列转换为float值。
df[["lmsd", "lmsd_hash"]] = df[["lmsd", "lmsd_hash"]].astype(float)

# 将值四舍五入到小数点后两位
df['lmsd']  = np.around(df['lmsd'], 2)
df['lmsd_hash']  = np.around(df['lmsd_hash'], 2)

print(df)

输出:

  created_date    lmsd  lmsd_hash
0   2023-03-01   99.70      99.85
1   2023-03-03  100.00     100.00
2   2023-03-07    4.14       4.58
3   2023-03-08   14.18      14.58
4   2023-03-09   24.43      23.94
英文:

It appears that you have the LMSD columns stored as the type object rather than float. To demonstrate this, I have reproduced the error.


Input Data (with the LMSD columns stored as type float)

import pandas as pd
import numpy as np


data = {
    &quot;created_date&quot;: [&quot;2023-03-01&quot;, &quot;2023-03-03&quot;, &quot;2023-03-07&quot;, &quot;2023-03-08&quot;, &quot;2023-03-09&quot;],
    &quot;lmsd&quot;: [99.701135, 100.0, 4.142565, 14.182277, 24.433356],
    &quot;lmsd_hash&quot;: [99.853902, 100.0, 4.579792, 14.58302, 23.937297]
}

df = pd.DataFrame(data)

# Convert the dates to datetime objects
df[&quot;created_date&quot;] = pd.to_datetime(df[&quot;created_date&quot;])

print(df)
  created_date        lmsd   lmsd_hash
0   2023-03-01   99.701135   99.853902
1   2023-03-03  100.000000  100.000000
2   2023-03-07    4.142565    4.579792
3   2023-03-08   14.182277   14.583020
4   2023-03-09   24.433356   23.937297

Output of df.info():

&lt;class &#39;pandas.core.frame.DataFrame&#39;&gt;
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
 #   Column        Non-Null Count  Dtype         
---  ------        --------------  -----         
 0   created_date  5 non-null      datetime64[ns]
 1   lmsd          5 non-null      float64       
 2   lmsd_hash     5 non-null      float64       
dtypes: datetime64[ns](1), float64(2)
memory usage: 248.0 bytes

Storing the LMSD columns as type object

# Represent the LMSD columns as objects
df[[&quot;lmsd&quot;, &quot;lmsd_hash&quot;]] = df[[&quot;lmsd&quot;, &quot;lmsd_hash&quot;]].astype(object)

print(df)
  created_date       lmsd  lmsd_hash
0   2023-03-01  99.701135  99.853902
1   2023-03-03      100.0      100.0
2   2023-03-07   4.142565   4.579792
3   2023-03-08  14.182277   14.58302
4   2023-03-09  24.433356  23.937297

(Notice how this matches your DataFrame)

Output of df.info():

&lt;class &#39;pandas.core.frame.DataFrame&#39;&gt;
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
 #   Column        Non-Null Count  Dtype         
---  ------        --------------  -----         
 0   created_date  5 non-null      datetime64[ns]
 1   lmsd          5 non-null      object        
 2   lmsd_hash     5 non-null      object        
dtypes: datetime64[ns](1), object(2)
memory usage: 248.0+ bytes

Attempting to round object columns:

# Try to round the values in the LMSD columns (represented as objects)
df[&#39;lmsd&#39;]  = np.around(df[&#39;lmsd&#39;], 2)
df[&#39;lmsd_hash&#39;]  = np.around(df[&#39;lmsd_hash&#39;], 2)

The following error is produced, matching the one you were given:

<pre>
TypeError: loop of ufunc does not support argument 0 of type float which has no callable rint method
</pre>


Representing the columns as float before rounding

To fix this, ensure that the LMSD columns are stored as floats before rounding, using the .astype() method:

# Convert the LMSD columns to float values.
df[[&quot;lmsd&quot;, &quot;lmsd_hash&quot;]] = df[[&quot;lmsd&quot;, &quot;lmsd_hash&quot;]].astype(float)

# Round the values to two decimal places
df[&#39;lmsd&#39;]  = np.around(df[&#39;lmsd&#39;], 2)
df[&#39;lmsd_hash&#39;]  = np.around(df[&#39;lmsd_hash&#39;], 2)

print(df)

Output:

  created_date    lmsd  lmsd_hash
0   2023-03-01   99.70      99.85
1   2023-03-03  100.00     100.00
2   2023-03-07    4.14       4.58
3   2023-03-08   14.18      14.58
4   2023-03-09   24.43      23.94

答案2

得分: 0

你或许可以这样做:

import numpy as np
import pandas as pd

df_lmsd = pd.DataFrame({'LMSD': [1.23456789], 'LMSD(#)': [2.34567890]})

df_lmsd['LMSD'] = np.around(np.array(df_lmsd['LMSD']), 2)
df_lmsd['LMSD(#)'] = np.around(np.array(df_lmsd['LMSD(#)']), 2)

print(df_lmsd)

这将会得到:

   LMSD  LMSD(#)
0  1.23     2.35
英文:

You should maybe do this:

import numpy as np
import pandas as pd

df_lmsd = pd.DataFrame({&#39;LMSD&#39;: [1.23456789], &#39;LMSD(#)&#39;: [2.34567890]})

df_lmsd[&#39;LMSD&#39;] = np.around(np.array(df_lmsd[&#39;LMSD&#39;]), 2)
df_lmsd[&#39;LMSD(#)&#39;] = np.around(np.array(df_lmsd[&#39;LMSD(#)&#39;]), 2)

print(df_lmsd)

which gives

   LMSD  LMSD(#)
0  1.23     2.35


</details>



huangapple
  • 本文由 发表于 2023年3月9日 19:23:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定