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!
You can create a
DataFrame
with mixed values.pandas tries to guess the type for each column by chunks, if chunks have different guessed type then a warning is emitted but the type is preserved.
It's probably not the best way to write a production-ready code but it can be useful in tests, and when debugging your code.
See the documentation: https://pandas.pydata.org/docs/reference/api/pandas.errors.DtypeWarning.html