Open FlorinAndrei opened 2 years ago
This is the actual output:
Outstanding Not Outstanding Total
Bank 70.0 150.0 220.0
Credit Union 66.0 81.0 147.0
Total 136.0 231.0 367.0
And this is ctot before I do the sums:
Outstanding Not Outstanding
Bank 70 150
Credit Union 66 81
Thanks @FlorinAndrei for the report.
Note that the row expansion with a 2d indexer for .loc
is causing the cast (the first total) and in the code sample this results in the floats in the row totals (second total and added column) and the DataFrame copy is also not relevant.
# single column DataFrame
df = pd.DataFrame([70, 150], dtype="i8")
df.loc["Total"] = df.sum(axis=0)
print(df.dtypes)
# 0 int64
# dtype: object
df = pd.DataFrame([70, 150], dtype="i8")
df.loc["Total", :] = df.sum(axis=0)
print(df.dtypes)
# 0 float64
# dtype: object
df = pd.DataFrame([70, 150], dtype="i8")
df.loc[:, "Total"] = df.sum(axis=1)
print(df.dtypes)
# 0 int64
# Total int64
# dtype: object
# # multi-column DataFrame
df = pd.DataFrame([[70, 150], [66, 81]], dtype="i8")
df.loc["Total"] = df.sum(axis=0)
print(df.dtypes)
# 0 int64
# 1 int64
# dtype: object
df = pd.DataFrame([[70, 150], [66, 81]], dtype="i8")
df.loc["Total", :] = df.sum(axis=0)
print(df.dtypes)
# 0 float64
# 1 float64
# dtype: object
df = pd.DataFrame([[70, 150], [66, 81]], dtype="i8")
df.loc[:, "Total"] = df.sum(axis=1)
print(df.dtypes)
# 0 int64
# 1 int64
# Total int64
# dtype: object
The result dtype from df.sum() is correct, so the title of this issue is misleading and the MRE could be reduced to
df = pd.DataFrame([70, 150], dtype="i8")
df.loc["Total", :] = pd.Series([220], dtype="i8")
df.dtypes
# 0 float64
# dtype: object
so it appears to be an indexing issue i.e. setting values with .loc
with row expansion. (might be worth looking for similar issues)
contributions and PRs to fix welcome
this is as expected and nothing to be done here
can you assign this issue to me?
@jreback - why should expanding an integer column with an integer value coerce to float?
@jreback - why should expanding an integer column with an integer value coerce to float?
In case the examples are not clear, does not coerce to float with 1d indexer
df = pd.DataFrame([70, 150], dtype="i8")
df.loc["Total"] = df.sum(axis=0)
print(df.dtypes)
# 0 int64
# dtype: object
does coerce to float with 2d indexer
df = pd.DataFrame([70, 150], dtype="i8")
df.loc["Total", :] = df.sum(axis=0)
print(df.dtypes)
# 0 float64
# dtype: object
and only happens with axis=0
, i.e. adding rows.
looks like a bug to me. These should both produce the same output?
Happens because of the reindex we are doing on the DataFrame before adding the values
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
I generate a dataframe from an
np.array()
of integers. I want to calculate all the totals: by row, by column, and the grand total.The code works, but the output is the float type.
Expected Behavior
Since all inputs are integers and I only add the numbers, I expected the output dataframe to also be only integers.
Installed Versions