Skip to content

ENH: Improve error message when using DataFrame.from_items instead of DataFrame.from_records #17312

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

Closed
wesm opened this issue Aug 22, 2017 · 4 comments · Fixed by #17881
Closed
Labels
Error Reporting Incorrect or improved errors from pandas good first issue
Milestone

Comments

@wesm
Copy link
Member

wesm commented Aug 22, 2017

User reported from an early version of pandas, but this is still present in master:

import pandas

counters = [
    ('a', 98172247543414),
    ('b', 7805009),
    ('c', 1172983),
    ]

df = pandas.DataFrame.from_items(counters)


# Traceback (most recent call last):                                                                                                                
#   File "/home/user/ptest.py", line 12, in <module>                                                                                               
#     df = pandas.DataFrame.from_items(counters)                                                                                                    
#   File "/usr/local/lib/python2.7/dist-packages/pandas-0.13.1-py2.7-linux-x86_64.egg/pandas/core/frame.py", line 932, in from_items                
#   File "/usr/local/lib/python2.7/dist-packages/pandas-0.13.1-py2.7-linux-x86_64.egg/pandas/core/frame.py", line 947, in _from_arrays              
#   File "/usr/local/lib/python2.7/dist-packages/pandas-0.13.1-py2.7-linux-x86_64.egg/pandas/core/frame.py", line 4463, in _arrays_to_mgr           
#   File "/usr/local/lib/python2.7/dist-packages/pandas-0.13.1-py2.7-linux-x86_64.egg/pandas/core/frame.py", line 4502, in extract_index            
# ValueError: If using all scalar values, you must must pass an index  
@chris-b1
Copy link
Contributor

xref #4916 - not sure there are many usecases left where from_items is needed, possibly could be deprecated in favor of pd.DataFrame(dict(items)) (that's what I personally use)

@chris-b1 chris-b1 added API Design Error Reporting Incorrect or improved errors from pandas labels Aug 22, 2017
@jreback
Copy link
Contributor

jreback commented Aug 23, 2017

@chris-b1 can you open an issue for the deprecation.

@reidy-p
Copy link
Contributor

reidy-p commented Oct 14, 2017

I've been looking at this issue and the problem seems to be that the value in each (key, value) pair should be an array or a Series as noted in the from_items docstring. Would it make sense to raise a ValueError with this message if the user passes a scalar instead of an array or Series as a value?

@jreback
Copy link
Contributor

jreback commented Oct 14, 2017

The problem is the user passed a dict, with keys that are all scalars; maybe meant to use a Series (or of course meant to pass values that are array-like)

In [6]: Series(dict(counters))
Out[6]: 
a    98172247543414
b           7805009
c           1172983
dtype: int64

I suppose the error message could be improved

reidy-p added a commit to reidy-p/pandas that referenced this issue Oct 15, 2017
@jreback jreback modified the milestones: Next Major Release, 0.22.0 Oct 28, 2017
reidy-p added a commit to reidy-p/pandas that referenced this issue Oct 31, 2017
reidy-p added a commit to reidy-p/pandas that referenced this issue Nov 25, 2017
reidy-p added a commit to reidy-p/pandas that referenced this issue Nov 25, 2017
reidy-p added a commit to reidy-p/pandas that referenced this issue Nov 26, 2017
reidy-p added a commit to reidy-p/pandas that referenced this issue Nov 26, 2017
@reidy-p reidy-p mentioned this issue Nov 26, 2017
34 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants