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

Different dtype for Series in empty DataFrame depending on construction. #25767

Closed
ngnpope opened this issue Mar 18, 2019 · 4 comments
Closed
Labels
Duplicate Report Duplicate issue or pull request

Comments

@ngnpope
Copy link

ngnpope commented Mar 18, 2019

Code Sample, a copy-pastable example if possible

>>> df0 = pd.DataFrame({}, columns=['A'])
>>> df1 = pd.DataFrame({'A': []})
>>> df0.columns
Index(['A'], dtype='object')
>>> df1.columns
Index(['A'], dtype='object')
>>> df0.empty
True
>>> df1.empty
True
>>> df0['A']
Series([], Name: A, dtype: object)
>>> df1['A']
Series([], Name: A, dtype: float64)

Problem description

The dtype of df0['A'] and df1['A'] differ (object vs float64) which is confusing and can lead to subtle bugs.

Expected Output

The dtype of df0['A'] and df1['A'] should match.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.7.0.final.0
python-bits: 64
OS: Linux
OS-release: 4.18.9-arch1-1-ARCH
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_GB.utf8
LOCALE: en_GB.UTF-8

pandas: 0.23.4
pytest: None
pip: None
setuptools: 40.4.3
Cython: 0.28.5
numpy: 1.15.1
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.5.0
sphinx: 1.8.0
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: 1.2.11
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@WillAyd
Copy link
Member

WillAyd commented Mar 19, 2019

Strange they would be different but also tough to reason about which one is correct or not - how do you expect this to be handled?

@jschendel
Copy link
Member

My guess is that this is a manifestation of #17261 and that defaulting to object dtype would be the correct behavior here.

@WillAyd
Copy link
Member

WillAyd commented Mar 19, 2019

Makes sense. I think this is a duplicate then so closing as such but feel free to reopen if anyone disagrees

@WillAyd WillAyd added the Duplicate Report Duplicate issue or pull request label Mar 19, 2019
@WillAyd WillAyd closed this as completed Mar 19, 2019
@ngnpope
Copy link
Author

ngnpope commented Mar 19, 2019

Ah, thanks - searched for something before opening this, but just not with the right terms...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request
Projects
None yet
Development

No branches or pull requests

3 participants