Python Pandas inferring column datatypes
<p>I am reading JSON files into dataframes. The dataframe might have some String (object) type columns, some Numeric (int64 and/or float64), and some datetime type columns. When the data is read in, the datatype is often incorrect (ie datetime, int and float will often be stored as "object" type). I want to report on this possibility. (ie a column is in the dataframe as "object" (String), but it is actually a "datetime"). </p>

<p>The problem i have is that when i use <strong><em>pd.to_numeric</em></strong> and <strong><em>pd.to_datetime</em></strong> they will both evaluate and try to convert the column, and many times it ends up depending on which of the two I call last... (I was going to use <strong><em>convert_objects()</em></strong> which works but that is depreciated, so wanted a better option).</p>

<p>The code I am using to evaluate the dataframe column is (i realize a lot of the below is redundant, but I have written it this way for readability):</p>

<pre><code>try:
   inferred_type = pd.to_datetime(df[Field_Name]).dtype
   if inferred_type == "datetime64[ns]":
      inferred_type = "DateTime"
except:
   pass
try:
   inferred_type = pd.to_numeric(df[Field_Name]).dtype
   if inferred_type == int:
      inferred_type = "Integer"
   if inferred_type == float:
      inferred_type = "Float"
except:
   pass
</code></pre>