In my environment out of simplicity it was decided to store everything in hdfs as a string. So when I pull data into a pandas dataframe from this location, every type is a string, despite the values being ints, floats, bool etc...
Is there a way to dynamically determine a column property type based on the value evaluation? ie: look to see that every value in a column is 'x' type and then cast as such?
EDIT:
I couldn't get the below solution to work due to my version of python (I think). So I decided my own attempt at a hacky solution. This is probably not perfect and I haven't figured out dates yet. Because of those two things I'm not going to post it as a solution but maybe this can be a starting point for someone else needing it:
#get dtypes when we can - Doesn't do dates.
for i in df:
try:
df[i] = df[i].astype(int)
print(i, 'is an int')
except:
[]
try:
if '.' in str(df[i]):
df[i] = df[i].astype(float)
print(i, 'is a float')
except:
[]
try:
if df[i].replace('False', '').unique()=='True' or df[i].replace('False', '').unique() == 'TRUE':
df[i] = df[i].replace('False', '').astype(bool).astype(int)
print(i, 'is bool')
except:
print(i, 'is an object')
Essentially I'm just attempting to cast and catch the error if it breaks. I'm sure this is probably a very poor way to go about this however.