This is a rather non-standard question. For educational purposes, I'm trying to create a mixed type column in a csv file, so that I get a warning message when importing the dataset in a pandas DataFrame and later on, deal with that column to show how it's done.
The problem is that I'd type 0s in a string column in Excel, save it and close the file, but the clever pandas still imports that column as a string column, so it doesn't detect that there are in fact floats in it.
I also tried to change the format of only these 0s in pandas using astype('float'), exporting and re-importing. Still doesn't work.
Does anyone have an idea how can I create a column that pandas will read a mixed type?
Thanks in advance!
Pandas will always infer the type of a column (
Seriesobject) and this is always going to be a single type. If every value in the column is string then pandas will load it as a column of type string.If there are "mixed" values that can't be reasonably loaded as a strings, integers... then the inferred type will simply be
dtype: object. Which also means that you will get no warning.You can force the type when loading dataframe via
dtypeparameter.Now the pandas will try to convert everything to
intand if there are values that can't be converted toint, you will get an exception such asWhen trying to load dataset that contains string
ain it. But again, this will not produce a warning, the operation will simply fail.Here is how you can create a mixed column.
Type of the
mixcolumn is objectIf you change the
read_csvin the above code intoyou will get the mentioned error.