Skip to content

DataFrame.from_records ignores exclude if it is [0] #18679

@pfmoore

Description

@pfmoore

Code Sample, a copy-pastable example if possible

df1 = pd.DataFrame.from_records([[1,2,3],[4,5,6],[7,8,9]], index=0, exclude=[0])
df2 = pd.DataFrame.from_records([[1,2,3],[4,5,6],[7,8,9]], index=0)

Problem description

In both cases, the data frame has columns 0, 1 and 2. If you use an index of 1, or exclude=[1], the columns are (correctly) [0, 2].

The issue is caused by the any() call at https://github.com/pandas-dev/pandas/blob/master/pandas/core/frame.py#L1178 which treats a zero column ID as false. Changing this to if exclude fixes the problem for me (by running the code to drop columns if the exclude set is not empty).

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.2.final.0 python-bits: 64 OS: Windows OS-release: 7 machine: AMD64 processor: Intel64 Family 6 Model 61 Stepping 4, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.21.0
pytest: None
pip: 9.0.1
setuptools: 38.2.4
Cython: None
numpy: 1.13.3
scipy: None
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.0
openpyxl: 2.4.9
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0b10
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugConstructorsSeries/DataFrame/Index/pd.array Constructors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions