You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@arrow.apache.org by "NickCrews (via GitHub)" <gi...@apache.org> on 2024/02/12 18:45:46 UTC
[I] pa.array() changes field order to be sorted [arrow]
NickCrews opened a new issue, #40053:
URL: https://github.com/apache/arrow/issues/40053
### Describe the bug, including details regarding any error messages, version, and platform.
If I give a column of structs, then pyarrow seems to sort the fields by name. I would expect the field order to be preserved.
```python
import pandas as pd
import pyarrow as pa
print(pa.__version__)
# 15.0.0
s = pd.Series([{"b": 2, "a": 1}, {"b": 4, "a": 3}])
a = pa.array(s)
print(a.type)
# struct<a: int64, b: int64>
```
This was discovered in https://github.com/ibis-project/ibis/issues/8166
### Component(s)
Python
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@arrow.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pa.array()` changes field order to be sorted [arrow]
Posted by "gabrielsimoes (via GitHub)" <gi...@apache.org>.
gabrielsimoes commented on issue #40053:
URL: https://github.com/apache/arrow/issues/40053#issuecomment-3743802166
:+1:
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pa.array()` changes field order to be sorted [arrow]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on issue #40053:
URL: https://github.com/apache/arrow/issues/40053#issuecomment-3743466349
Issue resolved by pull request 48813
https://github.com/apache/arrow/pull/48813
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pa.array()` changes field order to be sorted [arrow]
Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on issue #40053:
URL: https://github.com/apache/arrow/issues/40053#issuecomment-1941560097
Quick guess, but I assume this might stem from a time when Python dicts didn't have an order, and so the keys were sorted to guarantee stable results.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pa.array()` changes field order to be sorted [arrow]
Posted by "NickCrews (via GitHub)" <gi...@apache.org>.
NickCrews commented on issue #40053:
URL: https://github.com/apache/arrow/issues/40053#issuecomment-1942142974
Thanks for the help @jorisvandenbossche !
Are you referring to [this function](https://github.com/apache/arrow/blob/bbe59b35de33a0534fc76c9617aa4746031ce16c/python/pyarrow/src/arrow/python/inference.cc#L628-L660)? Could the shuffling be due to the use of `struct_inferrers_.insert()`, where `struct_inferrers_` is a `std::map<std::string, TypeInferrer>`, so it doesn't preserve order?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pa.array()` changes field order to be sorted [arrow]
Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on issue #40053:
URL: https://github.com/apache/arrow/issues/40053#issuecomment-1947993140
> Are you referring to [this function](https://github.com/apache/arrow/blob/bbe59b35de33a0534fc76c9617aa4746031ce16c/python/pyarrow/src/arrow/python/inference.cc#L628-L660)?
Yes, and then the `struct_inferrers_` gets converted to the struct type here:
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pa.array()` changes field order to be sorted [arrow]
Posted by "NickCrews (via GitHub)" <gi...@apache.org>.
NickCrews commented on issue #40053:
URL: https://github.com/apache/arrow/issues/40053#issuecomment-3745608996
Thank you all!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pa.array()` changes field order to be sorted [arrow]
Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on issue #40053:
URL: https://github.com/apache/arrow/issues/40053#issuecomment-1948009700
> Are you referring to [this function](https://github.com/apache/arrow/blob/bbe59b35de33a0534fc76c9617aa4746031ce16c/python/pyarrow/src/arrow/python/inference.cc#L628-L660)?
Yes, and then the `struct_inferrers_` gets converted to the struct type here:
https://github.com/apache/arrow/blob/bbe59b35de33a0534fc76c9617aa4746031ce16c/python/pyarrow/src/arrow/python/inference.cc#L668-L676
> Could the shuffling be due to the use of `struct_inferrers_.insert()`, where `struct_inferrers_` is a `std::map<std::string, TypeInferrer>`, so it doesn't preserve order?
Good catch! Indeed, a map will sort the keys. If we want to preserve the order of the dict keys, we will have to use a different data structure (or keep track of the order separately)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pa.array()` changes field order to be sorted [arrow]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou commented on issue #40053:
URL: https://github.com/apache/arrow/issues/40053#issuecomment-3743477698
@gabrielsimoes Can you comment here so that we assign this issue to you?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pa.array()` changes field order to be sorted [arrow]
Posted by "NickCrews (via GitHub)" <gi...@apache.org>.
NickCrews commented on issue #40053:
URL: https://github.com/apache/arrow/issues/40053#issuecomment-1949108155
If you point me in the direction of which data structure you think we should use, I can take a stab at this.
I feel like somewhere in the arrow codebase there MUST be somewhere else an ordered map is used, but [I didn't find it](https://github.com/search?q=repo%3Aapache%2Farrow%20ordered%20map&type=code).
I found https://stackoverflow.com/questions/1098175/a-stdmap-that-keep-track-of-the-order-of-insertion. I'm assuming we don't want to bring in any external deps. Could copy-paste https://github.com/Tessil/ordered-map/blob/master/include/tsl/ordered_map.h? Or, its much smaller, but more "one-off", could [implement it with a list and a map.](https://stackoverflow.com/a/1098236/5156887)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pa.array()` changes field order to be sorted [arrow]
Posted by "jorisvandenbossche (via GitHub)" <gi...@apache.org>.
jorisvandenbossche commented on issue #40053:
URL: https://github.com/apache/arrow/issues/40053#issuecomment-1941581155
Although it seems we iterate over the dict using `PyDict_Next`. The Python docs (https://docs.python.org/3/c-api/dict.html#c.PyDict_Next) are not very clear about the order of the iteration with this function.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] [Python] `pa.array()` changes field order to be sorted [arrow]
Posted by "pitrou (via GitHub)" <gi...@apache.org>.
pitrou closed issue #40053: [Python] `pa.array(<pd.Series of structs>)` changes field order to be sorted
URL: https://github.com/apache/arrow/issues/40053
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org