IntCastingNaNError Traceback (most recent call last)
Cell In[1], line 12
9 df = pd.DataFrame(data)
11 # 计算排名
—> 12 df[‘排名’] = df[‘营收(亿元)’].rank(ascending=False).astype(int)
14 # 提取排名前五的公司
15 top_companies = df.nlargest(5, ‘营收(亿元)’)
File /usr/local/lib/python3.10/site-packages/pandas/core/generic.py:6643, in NDFrame.astype(self, dtype, copy, errors)
6637 results = [
6638 ser.astype(dtype, copy=copy, errors=errors) for _, ser in self.items()
6639 ]
6641 else:
6642 # else, only a single dtype is given
-> 6643 new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
6644 res = self._constructor_from_mgr(new_data, axes=new_data.axes)
6645 return res.finalize(self, method=“astype”)
File /usr/local/lib/python3.10/site-packages/pandas/core/internals/managers.py:430, in BaseBlockManager.astype(self, dtype, copy, errors)
427 elif using_copy_on_write():
428 copy = False
–> 430 return self.apply(
431 “astype”,
432 dtype=dtype,
433 copy=copy,
434 errors=errors,
435 using_cow=using_copy_on_write(),
436 )
File /usr/local/lib/python3.10/site-packages/pandas/core/internals/managers.py:363, in BaseBlockManager.apply(self, f, align_keys, **kwargs)
361 applied = b.apply(f, **kwargs)
362 else:
–> 363 applied = getattr(b, f)(**kwargs)
364 result_blocks = extend_blocks(applied, result_blocks)
366 out = type(self).from_blocks(result_blocks, self.axes)
File /usr/local/lib/python3.10/site-packages/pandas/core/internals/blocks.py:758, in Block.astype(self, dtype, copy, errors, using_cow, squeeze)
755 raise ValueError(“Can not squeeze with more than one column.”)
756 values = values[0, :] # type: ignore[call-overload]
–> 758 new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
760 new_values = maybe_coerce_values(new_values)
762 refs = None
File /usr/local/lib/python3.10/site-packages/pandas/core/dtypes/astype.py:237, in astype_array_safe(values, dtype, copy, errors)
234 dtype = dtype.numpy_dtype
236 try:
–> 237 new_values = astype_array(values, dtype, copy=copy)
238 except (ValueError, TypeError):
239 # e.g. _astype_nansafe can fail on object-dtype of strings
240 # trying to convert to float
241 if errors == “ignore”:
File /usr/local/lib/python3.10/site-packages/pandas/core/dtypes/astype.py:182, in astype_array(values, dtype, copy)
179 values = values.astype(dtype, copy=copy)
181 else:
–> 182 values = _astype_nansafe(values, dtype, copy=copy)
184 # in pandas we don’t store numpy str dtypes, so convert to object
185 if isinstance(dtype, np.dtype) and issubclass(values.dtype.type, str):
File /usr/local/lib/python3.10/site-packages/pandas/core/dtypes/astype.py:101, in _astype_nansafe(arr, dtype, copy, skipna)
96 return lib.ensure_string_array(
97 arr, skipna=skipna, convert_na_value=False
98 ).reshape(shape)
100 elif np.issubdtype(arr.dtype, np.floating) and dtype.kind in “iu”:
–> 101 return _astype_float_to_int_nansafe(arr, dtype, copy)
103 elif arr.dtype == object:
104 # if we have a datetime/timedelta array of objects
105 # then coerce to datetime64[ns] and use DatetimeArray.astype
107 if lib.is_np_dtype(dtype, “M”):
File /usr/local/lib/python3.10/site-packages/pandas/core/dtypes/astype.py:145, in _astype_float_to_int_nansafe(values, dtype, copy)
141 “”"
142 astype with a check preventing converting NaN to an meaningless integer value.
143 “”"
144 if not np.isfinite(values).all():
–> 145 raise IntCastingNaNError(
146 “Cannot convert non-finite values (NA or inf) to integer”
147 )
148 if dtype.kind == “u”:
149 # GH#45151
150 if not (values >= 0).all():
IntCastingNaNError: Cannot convert non-finite values (NA or inf) to integer