title: How to stop Pandas DataFrame from converting int to float for no reason?
body: <p>I am creating a small Pandas DataFrame and adding some data to it which is supposed to be integers. But even though I am trying very hard to explicitly set the dtype to int and only provide int values, it always ends up becoming floats. It is making no sense to me at all and the behaviour doesn't even seem entirely consistent.</p>
  
  <p>Consider the following Python script:</p>
  
  <pre><code>import pandas as pd
  
  df = pd.DataFrame(columns=["col1", "col2"])  # No dtype specified.
  print(df.dtypes)  # dtypes are object, since there is no information yet.
  df.loc["row1", :] = int(0)  # Add integer data.
  print(df.dtypes)  # Both columns have now become int64, as expected.
  df.loc["row2", :] = int(0)  # Add more integer data.
  print(df.dtypes)  # Both columns are now float64???
  print(df)  # Shows as 0.0.
  
  # Let's try again, but be more specific.
  del df  
  df = pd.DataFrame(columns=["col1", "col2"], dtype=int)  # Explicit set dtype.
  print(df.dtypes)  # For some reason both colums are already float64???
  df.loc["row1", :] = int(0)
  print(df.dtypes)  # Both colums still float64.
  
  # Output:
  """
  col1    object
  col2    object
  dtype: object
  col1    int64
  col2    int64
  dtype: object
  col1    float64
  col2    float64
  dtype: object
        col1  col2
  row1   0.0   0.0
  row2   0.0   0.0
  col1    float64
  col2    float64
  dtype: object
  col1    float64
  col2    float64
  dtype: object
  """
  </code></pre>
  
  <p>I can fix it by doing <code>df = df.astype(int)</code> at the end. There are other ways to fix it as well. But this should not be necessary. I am trying to figure out what I am doing wrong that makes the columns become floats in the first place.</p>
  
  <p>What is going on?</p>
  
  <p>Python version 3.7.1
  Pandas version 0.23.4</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I think maybe some people are misunderstanding. There are never any NaN values in this DataFrame. Immediately after its creation it looks like this:</p>
  
  <pre><code>Empty DataFrame
  Columns: [col1, col2]
  Index: []
  </code></pre>
  
  <p>It is an <em>empty</em> Dataframe, df.shape=0, but there is no NaN in it, there's just no rows yet.</p>
  
  <p>I have also discovered something even worse. Even if I do <code>df = df.astype(int)</code> after adding data such that it becomes int, <em>it becomes float again as soon as I add more data</em>!</p>
  
  <pre><code>df = pd.DataFrame(columns=["col1", "col2"], dtype=int)
  df.loc["row1", :] = int(0)
  df.loc["row2", :] = int(0)
  df = df.astype(int)  # Force it back to int.
  print(df.dtypes)  # It is now ints again.
  df.loc["row3", :] = int(0)  # Add another integer row.
  print(df.dtypes)  # It is now float again???
  
  # Output:
  """
  col1    int32
  col2    int32
  dtype: object
  col1    float64
  col2    float64
  dtype: object
  """
  </code></pre>
  
  <p>The <a href="https://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.html" rel="noreferrer">suggested fix in version 0.24</a> does not seem related to my problem. That feature is about Nullable Integer Data Type. There are no NaN or None values in my data.</p>