-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Json import sorting #4703
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
The JSON emitted is a dictionary object, not an array, so it's inherently |
no it's sorting lexicographically, most likely because the index is sorted before being converted to a numeric dtype. I think this is a bug. |
@cpcloud sure, that may be, but fundamentally, you can't assure that whatever ordering was there initially should be in the final index. I.e.: df = DataFrame([range(10)] * 10, index = range(10,0,-1))
df.from_json(df.to_json()) There's no way that you can ensure that the index falls out correctly. But yes, it's clearly lexicographically sorting. |
hm....okay...but why is it sorting at all? |
The JSON code initially decodes to a dict with string keys which it then passes to the In [9]: d = {'foo': {'1': 1, '2': 2, '3': 3, '10': 10, '15': 15, '100': 100, '101': 101}}
In [10]: pd.DataFrame(d)
Out[10]:
foo
1 1
10 10
100 100
101 101
15 15
2 2
3 3 |
Yep, that'll do it! |
passing a list-of-lists will not sort (though may be more expensive 2 create) |
BTW @ghego if you want to ensure that order is preserved during roudtrip JSONifying try using n [27]: pd.read_json(df.to_json(orient='split'), orient='split')
[0]
Out[27]:
foo
1 1
2 2
3 3
4 10
10 11
11 20
100 100
101 101 |
Might be nice to have that in the docs/cookbook. |
@cpcloud agreed. There's a couple of things I'd like to add to the JSON docs (incl a couple of benchmarks and more about the numpy param and when it can be useful). I'll try and get a PR together for this stuff before the next release. |
So I think this can be closed because it's expected behavior? |
I think @Komnomnomnom going to take a look....so let's leave open for a bit |
Just to clarify I'm going to update the docs to give more detail on the different orients, and make it clear when you can expect non order-preserving behaviour. Apart from that I wasn't going to change anything.... |
@Komnomnomnom changing this to docs only....when u have a chance |
@Komnomnomnom how's docs coming on this? |
@Komnomnomnom do we need anything in docs for this? (I believe you did cover it...)...lmk |
@jreback, sorry yeah still planning to get to the json docs at some point. Hopefully will have some time over the next few days. |
gr8 |
@Komnomnomnom docs? |
I've done a rough pass but still struggling to find adequate time I'm afraid. Last chance this weekend? :) |
docs are ok even after release-candidate....so ok |
Noticed that if export a df to json file and then reload the data, ordering is not kept.
gives:
instead of
it suffices to call
newdf = pd.read_json('test.json').sort()
to have the what I'd expect.Is this intended behaviour or bug?
The text was updated successfully, but these errors were encountered: