Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dataframe column filled with .sum() of int-series has dtype not int64, but object #20754

Closed
Rik-de-Kort opened this issue Apr 20, 2018 · 1 comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@Rik-de-Kort
Copy link
Contributor

Rik-de-Kort commented Apr 20, 2018

MnWE

import pandas as pd

the_int_column = pd.Series([1, 2, 3])
foobar = pd.DataFrame(columns=("ints",))
foobar.loc["First"] = [the_int_column.sum()]

print(foobar.dtypes)

the_float_column = pd.Series([1.0, 2.0, 3.0])
floatbar = pd.DataFrame(columns=("floats",))
floatbar.loc["First"] = [the_float_column.sum()]

print(floatbar.dtypes)

This yields

ints    object
dtype: object
floats    float64
dtype: object

Problem description

Assigning the sum of a Series of (vanilla Python) ints to a DataFrame leads to not specific enough type inference on the frame: it keeps the object type. Vanilla floats work fine, as the second example demonstrates.

This is an issue for some testing I'm doing, where I want to ensure a column of these sums contains only numeric data, and specifically integers.

Might be related to #20635.

Expected Output

Expected output:

ints       int64
dtype: object
floats    float64
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.4.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 142 Stepping 10, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None pandas: 0.22.0 pytest: 3.3.2 pip: 9.0.1 setuptools: 38.4.0 Cython: 0.27.3 numpy: 1.14.0 scipy: 1.0.0 pyarrow: None xarray: None IPython: 6.2.1 sphinx: 1.6.6 patsy: 0.5.0 dateutil: 2.6.1 pytz: 2017.3 blosc: None bottleneck: 1.2.1 tables: 3.4.2 numexpr: 2.6.4 feather: None matplotlib: 2.1.2 openpyxl: 2.4.10 xlrd: 1.1.0 xlwt: 1.3.0 xlsxwriter: 1.0.2 lxml: 4.1.1 bs4: 4.6.0 html5lib: 1.0.1 sqlalchemy: 1.2.1 pymysql: None psycopg2: None jinja2: 2.10 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None
@jreback jreback added Reshaping Concat, Merge/Join, Stack/Unstack, Explode Dtype Conversions Unexpected or buggy dtype conversions Duplicate Report Duplicate issue or pull request labels Apr 20, 2018
@jreback
Copy link
Contributor

jreback commented Apr 20, 2018

duplicate issue of #20635

@jreback jreback closed this as completed Apr 20, 2018
@jreback jreback added this to the No action milestone Apr 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

2 participants