Working on jupyter notebook and getting this error for type convert

ValueError Traceback (most recent call last)
Cell In[36], line 4
1 #conversion to yolo labelling center of object
2 #type conversion to integer values
3 cols=[‘width’,‘height’,‘xmin’,‘xmax’,‘ymin’,‘ymax’]
----> 4 df[cols] = df[cols].astype(int)
5 df.info()

File c:\users\lenovo\desktop\internship\yolo\yolo_venv\lib\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 c:\users\lenovo\desktop\internship\yolo\yolo_venv\lib\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 c:\users\lenovo\desktop\internship\yolo\yolo_venv\lib\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 c:\users\lenovo\desktop\internship\yolo\yolo_venv\lib\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 c:\users\lenovo\desktop\internship\yolo\yolo_venv\lib\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 c:\users\lenovo\desktop\internship\yolo\yolo_venv\lib\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 c:\users\lenovo\desktop\internship\yolo\yolo_venv\lib\site-packages\pandas\core\dtypes\astype.py:133, in _astype_nansafe(arr, dtype, copy, skipna)
129 raise ValueError(msg)
131 if copy or arr.dtype == object or dtype == object:
132 # Explicit copy, or required since NumPy can’t view from / to object.
→ 133 return arr.astype(dtype, copy=True)
135 return arr.astype(dtype, copy=copy)

ValueError: invalid literal for int() with base 10: ‘34.0485’

using python 3.9

cols=['width','height','xmin','xmax','ymin','ymax']
df[cols] = df[cols].astype(int)
df.info()

I changed it to float but is there anyway to change it to integer by removing decimal points

I found this Q&A on Stack Overflow which seems to be exactly what you’re looking for:

To find it, I tried putting pandas get integers into a search engine; it was the first result for me.

1 Like