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

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

'float' object has no attribute 'rint'

问题

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

DataFrame

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

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

我正在执行以下操作:

  1. df_lmsd['LMSD']=np.around(df_lmsd['LMSD'],2)
  2. df_lmsd['LMSD(#)']=np.around(df_lmsd['LMSD(#)'],2)
  3. 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:

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

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

Python Version:- Python 3.10.9
Ide:- Jupyter Notebook

答案1

得分: 3

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


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

  1. import pandas as pd
  2. import numpy as np
  3. data = {
  4. "created_date": ["2023-03-01", "2023-03-03", "2023-03-07", "2023-03-08", "2023-03-09"],
  5. "lmsd": [99.701135, 100.0, 4.142565, 14.182277, 24.433356],
  6. "lmsd_hash": [99.853902, 100.0, 4.579792, 14.58302, 23.937297]
  7. }
  8. df = pd.DataFrame(data)
  9. # 将日期转换为日期时间对象
  10. df["created_date"] = pd.to_datetime(df["created_date"])
  11. print(df)
  1. created_date lmsd lmsd_hash
  2. 0 2023-03-01 99.701135 99.853902
  3. 1 2023-03-03 100.000000 100.000000
  4. 2 2023-03-07 4.142565 4.579792
  5. 3 2023-03-08 14.182277 14.583020
  6. 4 2023-03-09 24.433356 23.937297

df.info()的输出:

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

将LMSD列存储为object类型

  1. # 将LMSD列表示为对象
  2. df[["lmsd", "lmsd_hash"]] = df[["lmsd", "lmsd_hash"]].astype(object)
  3. print(df)
  1. created_date lmsd lmsd_hash
  2. 0 2023-03-01 99.701135 99.853902
  3. 1 2023-03-03 100.0 100.0
  4. 2 2023-03-07 4.142565 4.579792
  5. 3 2023-03-08 14.182277 14.58302
  6. 4 2023-03-09 24.433356 23.937297

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

df.info()的输出:

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

尝试在object列中四舍五入

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

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

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

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

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

  1. # 将LMSD列转换为float值。
  2. df[["lmsd", "lmsd_hash"]] = df[["lmsd", "lmsd_hash"]].astype(float)
  3. # 将值四舍五入到小数点后两位
  4. df['lmsd'] = np.around(df['lmsd'], 2)
  5. df['lmsd_hash'] = np.around(df['lmsd_hash'], 2)
  6. print(df)

输出:

  1. created_date lmsd lmsd_hash
  2. 0 2023-03-01 99.70 99.85
  3. 1 2023-03-03 100.00 100.00
  4. 2 2023-03-07 4.14 4.58
  5. 3 2023-03-08 14.18 14.58
  6. 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)

  1. import pandas as pd
  2. import numpy as np
  3. data = {
  4. &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;],
  5. &quot;lmsd&quot;: [99.701135, 100.0, 4.142565, 14.182277, 24.433356],
  6. &quot;lmsd_hash&quot;: [99.853902, 100.0, 4.579792, 14.58302, 23.937297]
  7. }
  8. df = pd.DataFrame(data)
  9. # Convert the dates to datetime objects
  10. df[&quot;created_date&quot;] = pd.to_datetime(df[&quot;created_date&quot;])
  11. print(df)
  1. created_date lmsd lmsd_hash
  2. 0 2023-03-01 99.701135 99.853902
  3. 1 2023-03-03 100.000000 100.000000
  4. 2 2023-03-07 4.142565 4.579792
  5. 3 2023-03-08 14.182277 14.583020
  6. 4 2023-03-09 24.433356 23.937297

Output of df.info():

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

Storing the LMSD columns as type object

  1. # Represent the LMSD columns as objects
  2. df[[&quot;lmsd&quot;, &quot;lmsd_hash&quot;]] = df[[&quot;lmsd&quot;, &quot;lmsd_hash&quot;]].astype(object)
  3. print(df)
  1. created_date lmsd lmsd_hash
  2. 0 2023-03-01 99.701135 99.853902
  3. 1 2023-03-03 100.0 100.0
  4. 2 2023-03-07 4.142565 4.579792
  5. 3 2023-03-08 14.182277 14.58302
  6. 4 2023-03-09 24.433356 23.937297

(Notice how this matches your DataFrame)

Output of df.info():

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

Attempting to round object columns:

  1. # Try to round the values in the LMSD columns (represented as objects)
  2. df[&#39;lmsd&#39;] = np.around(df[&#39;lmsd&#39;], 2)
  3. 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:

  1. # Convert the LMSD columns to float values.
  2. df[[&quot;lmsd&quot;, &quot;lmsd_hash&quot;]] = df[[&quot;lmsd&quot;, &quot;lmsd_hash&quot;]].astype(float)
  3. # Round the values to two decimal places
  4. df[&#39;lmsd&#39;] = np.around(df[&#39;lmsd&#39;], 2)
  5. df[&#39;lmsd_hash&#39;] = np.around(df[&#39;lmsd_hash&#39;], 2)
  6. print(df)

Output:

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

答案2

得分: 0

你或许可以这样做:

  1. import numpy as np
  2. import pandas as pd
  3. df_lmsd = pd.DataFrame({'LMSD': [1.23456789], 'LMSD(#)': [2.34567890]})
  4. df_lmsd['LMSD'] = np.around(np.array(df_lmsd['LMSD']), 2)
  5. df_lmsd['LMSD(#)'] = np.around(np.array(df_lmsd['LMSD(#)']), 2)
  6. print(df_lmsd)

这将会得到:

  1. LMSD LMSD(#)
  2. 0 1.23 2.35
英文:

You should maybe do this:

  1. import numpy as np
  2. import pandas as pd
  3. df_lmsd = pd.DataFrame({&#39;LMSD&#39;: [1.23456789], &#39;LMSD(#)&#39;: [2.34567890]})
  4. df_lmsd[&#39;LMSD&#39;] = np.around(np.array(df_lmsd[&#39;LMSD&#39;]), 2)
  5. df_lmsd[&#39;LMSD(#)&#39;] = np.around(np.array(df_lmsd[&#39;LMSD(#)&#39;]), 2)
  6. print(df_lmsd)

which gives

  1. LMSD LMSD(#)
  2. 0 1.23 2.35
  3. </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:

确定