TypeError: Image data of dtype object cannot be converted to float I am trying to convert files stored in a CSV file dataset to grayscale. I run the code which worked on my previous dataset but I now get this error:
TypeError: Image data of dtype object cannot be converted to float
The code I produced is below:
import pandas as pd 
import numpy as np 
from sklearn.datasets import load_digits
import matplotlib.pyplot as plt

data_path = &quot;pe_section_headers.csv&quot;

data1 = pd.read_csv(data_path);

data = data1

#data = data.drop(&quot;Malware&quot;, axis=1)
#data = data.drop(&quot;Name&quot;, axis=1)
data = data.values
data = data.reshape(data.shape[0], data.shape[1], 1)
data = np.tile(data, (1, data.shape[1]))

for i in range(data.shape[0]):
    plt.tick_params(left=False, bottom=False, labelleft=False, labelbottom=False)
    plt.imshow(data[i], cmap=&quot;gray&quot;)
    plt.savefig(f&quot;output_image_{data1.iloc[i,0]}.png&quot;)
    plt.close()

#print(data[0].shape)

And the error message I receive is:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-8-9f9e3cc13a98&gt; in &lt;module&gt;
     21 for i in range(data.shape[0]):
     22     plt.tick_params(left=False, bottom=False, labelleft=False, labelbottom=False)
---&gt; 23     plt.imshow(data[i], cmap=&quot;gray&quot;)
     24     plt.savefig(f&quot;output_image_{data1.iloc[i,0]}.png&quot;)
     25     plt.close()

~/opt/anaconda3/envs/jacksprojectnew/lib/python3.6/site-packages/matplotlib/pyplot.py in imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, filternorm, filterrad, resample, url, data, **kwargs)
   2728         filternorm=filternorm, filterrad=filterrad, resample=resample,
   2729         url=url, **({&quot;data&quot;: data} if data is not None else {}),
-&gt; 2730         **kwargs)
   2731     sci(__ret)
   2732     return __ret

~/opt/anaconda3/envs/jacksprojectnew/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1445     def inner(ax, *args, data=None, **kwargs):
   1446         if data is None:
-&gt; 1447             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1448 
   1449         bound = new_sig.bind(ax, *args, **kwargs)

~/opt/anaconda3/envs/jacksprojectnew/lib/python3.6/site-packages/matplotlib/axes/_axes.py in imshow(self, X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, filternorm, filterrad, resample, url, **kwargs)
   5521                               resample=resample, **kwargs)
   5522 
-&gt; 5523         im.set_data(X)
   5524         im.set_alpha(alpha)
   5525         if im.get_clip_path() is None:

~/opt/anaconda3/envs/jacksprojectnew/lib/python3.6/site-packages/matplotlib/image.py in set_data(self, A)
    701                 not np.can_cast(self._A.dtype, float, &quot;same_kind&quot;)):
    702             raise TypeError(&quot;Image data of dtype {} cannot be converted to &quot;
--&gt; 703                             &quot;float&quot;.format(self._A.dtype))
    704 
    705         if self._A.ndim == 3 and self._A.shape[-1] == 1:

TypeError: Image data of dtype object cannot be converted to float

Could anyone suggest what this means? Thanks
The data you are trying to plot (data[i]) contains data of type object (i.e. a string) which of course cannot be plotted as they are not numbers. This is caused by the fact that one of the columns in your dataset is a string which you are trying to plot, likely the Name column. Try removing the Name column from the dataset and see if there are other column which contains strings.