-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DataFrame.from_dict()
does not behave as documented.
#12387
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
Comments
No, its acting correctly, you just passing in scalars, the values need to be lists, otherwise you don't have an index, but have values, just like the error message says.
Note that this is equivalent to the more used basic constructor.
|
Thanks for the explanation. I still have to ask why the different behavior for the two values of BTW, perhaps it is time to revisit #4916 and deprecate |
certainly could revist #4916. want to put forth a proposal on that issue (e.g. show the new constructor) so people can comment. We don't allow construction of a DataFrame with values when you don't have an index. it is exactly this:
It could simply create an index with len 1, but this is invariably a user mistake, then didn't say how long to make it. |
@jreback why we should have index to construct a DataFrame? Does this mean I have to reconstruct my data with index? Or could I use Pandas to add index for my data? |
Error message could say "If using all scalar values, use orient='index'." I read the error message "you must pass an index" to imply I should set an
|
This is an closed issue so people probably aren't monitoring it. If you
have a suggestion for improving the error message I'd recommend opening a
new issue.
…On Thu, May 16, 2019 at 5:57 PM Jesse W. Collins ***@***.***> wrote:
Error message could say "If using all scalar values, use orient='index'."
If read the error message "you must pass an index" to imply I should set
an index parameter, then I got another error.
TypeError: from_dict() got an unexpected keyword argument 'index'```
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#12387?email_source=notifications&email_token=AAKAOIVR2M3GFDE7OJ2ST7DPVXRHHA5CNFSM4B3WM57KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVTIS4I#issuecomment-493259121>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAKAOIRGU5COGLQIZT7D4YDPVXRHHANCNFSM4B3WM57A>
.
|
This behaviour is extremely counter-intuitive and should be strongly documented at least, if this is how things are intended to be kept. There is no reason why the DataFrame constructor should not create the index automatically when passing scalars, and asking to manually pass the index requires the user to already know the structure of the dictionary they are intending to parse, which is not the case in most practical applications (and should not be). |
If scalar aren't the keys implied as the index? |
All Possible working solution [Solved] ValueError: If using all scalar values, you must pass an index |
DataFrame.from_dict
does not seem to behave according to the documentation.Produces
According to the reference documentation, "If the keys of the passed dict should be the columns of the resulting DataFrame, pass ‘columns’" That doesn't appear to be what is happening.
Expected Output
from_dict(<dict>, orient='index')
works as expected.I would expect
from_dict(..., orient='columns')
to return a dataframe with the dictionary keys forming the column index, like this:I would expect
from_dict()
to take a dict as a parameter in either case. Instead it appears to take a a dict fororient='index'
and a list of lists (or tuples) fororient='columns'
. Passing a dict with integer values whenorient='columns'
causes a crash.output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: