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

.loc and .iloc returns different dtypes on empty dataframe #9964

Closed
artemyk opened this issue Apr 22, 2015 · 2 comments · Fixed by #9983
Closed

.loc and .iloc returns different dtypes on empty dataframe #9964

artemyk opened this issue Apr 22, 2015 · 2 comments · Fixed by #9983
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@artemyk
Copy link
Contributor

artemyk commented Apr 22, 2015

.iloc returns object dtype for a non-object empty series:

In [1]: import pandas as pd
In [2]: df = pd.DataFrame({'a':[1,2,3],'b':['b','b2','b3']})

In [3]: df.ix[[],:].loc[:,'a']
Out[3]: Series([], name: a, dtype: int64)

In [4]: df.ix[[],:].iloc[:,0]
Out[4]: Series([], name: a, dtype: object)

(came across this as I think it is related to the issue reported http://stackoverflow.com/questions/29749356/python-pandas-export-structure-only-no-rows-of-a-dataframe-to-sql/ , i.e. in sql code which is trying to infer series datatypes)

 >>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.9.final.0
python-bits: 64
OS: Darwin
OS-release: 14.1.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.15.2
nose: 1.3.6
Cython: 0.21.1
numpy: 1.9.2
scipy: 0.14.0
statsmodels: 0.6.1
IPython: 3.1.0
sphinx: 1.2.3
patsy: 0.2.1
dateutil: 2.4.2
pytz: 2015.2
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.4.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: 3.4.2
bs4: 4.3.2
html5lib: None
httplib2: 0.9
apiclient: None
rpy2: None
sqlalchemy: 0.9.8
pymysql: 0.6.2.None
psycopg2: 2.5.4 (dt dec pq3 ext)
@sinhrks sinhrks added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Apr 22, 2015
@sinhrks
Copy link
Member

sinhrks commented Apr 22, 2015

Thanks for the report. Though it looks like a bug, is there any reason once you convert it to empty DataFrame? All the followings work properly.

import pandas as pd
df = pd.DataFrame({'a':[1,2,3],'b':['b','b2','b3']})
df.loc[[], 'a']
# Series([], Name: a, dtype: int64)
df.ix[[], 'a']
# Series([], Name: a, dtype: int64)
df.iloc[[], 0]
# Series([], Name: a, dtype: int64)
df.ix[[], 0]
# Series([], Name: a, dtype: int64)

@artemyk
Copy link
Contributor Author

artemyk commented Apr 22, 2015

@sinhrks It's a bit involved, but basically there as a function (to write to a table to a sql database) that takes in a dataframe and infers types from the columns. Sometimes people would like to pass in an empty dataframe in order to create an empty table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants