Why doesn't pandas update a variable's dtype when populating a dataframe?


February 2019


32 time


I noticed a difference between two pandas version, and was wondering why that was the case / if it was an expected behaviour:

import pandas as pd
df = pd.DataFrame(columns = ("Name", "Age"))
df.loc[1] = "Jane", 5
df.loc[2] = "Riley", 24

In my test:

  • with Python 3.5 and Pandas 0.18.1, populating the dataframe does update the "object" dtype of the Age variable to "float64"
  • with Python 3.7 and Pandas 0.23.4, populating the dataframe does not update the "object" dtype of the Age variable

Why is that? I couldn't find an explanation in the documentation.

Edit: I just noticed df.infer_objects().dtypes, which might be particularly relevant... Did the automatic "inference" get ditched along the way?

0 answers