jorisvandenbossche commented on a change in pull request #10591:
URL: https://github.com/apache/arrow/pull/10591#discussion_r660380916
##########
File path: python/pyarrow/scalar.pxi
##########
@@ -652,21 +649,42 @@ cdef class StructScalar(Scalar, collections.abc.Mapping):
try:
return Scalar.wrap(GetResultValue(sp.field(ref)))
- except ArrowInvalid:
+ except ArrowInvalid as exc:
if isinstance(key, int):
- raise IndexError(key)
+ raise IndexError(key) from exc
else:
- raise KeyError(key)
+ raise KeyError(key) from exc
def as_py(self):
"""
Return this value as a Python dict.
"""
if self.is_valid:
- return {k: v.as_py() for k, v in self.items()}
+ try:
+ return {k: self[k].as_py() for k in self.keys()}
Review comment:
`self.items()` (currently) returns tuples with pyarrow scalars, not
python scalars (so it could also be `{k: v.as_py() for k, v in self.items()}`)
##########
File path: python/pyarrow/scalar.pxi
##########
@@ -652,21 +649,42 @@ cdef class StructScalar(Scalar, collections.abc.Mapping):
try:
return Scalar.wrap(GetResultValue(sp.field(ref)))
- except ArrowInvalid:
+ except ArrowInvalid as exc:
if isinstance(key, int):
- raise IndexError(key)
+ raise IndexError(key) from exc
else:
- raise KeyError(key)
+ raise KeyError(key) from exc
def as_py(self):
"""
Return this value as a Python dict.
"""
if self.is_valid:
- return {k: v.as_py() for k, v in self.items()}
+ try:
+ return {k: self[k].as_py() for k in self.keys()}
+ except KeyError:
+ raise ValueError(
+ "Converting to Python dictionary is not supported when "
+ "duplicate field names are present")
else:
return None
+ def _as_py_tuple(self):
+ # a version that returns a tuple instead of dict to support repr/str
+ # with the presence of duplicate field names
+ if self.is_valid:
+ return [(key, self[i].as_py()) for i, key in enumerate(self)]
Review comment:
Same here, I need to `as_py()` version of the scalar.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]