From 9096a7c4ca02359c1960427424401cb74007d754 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Wed, 7 Feb 2024 10:12:27 +0000 Subject: [PATCH] chore: fix ruff warnings (#3010) * fix: specify dtype for `full` * fix: drop use of sctype * fix: don't use quadratic loop --- src/awkward/_nplikes/typetracer.py | 2 +- src/awkward/operations/ak_enforce_type.py | 16 ++++++++++++---- src/awkward/operations/ak_to_dataframe.py | 9 +++++++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/awkward/_nplikes/typetracer.py b/src/awkward/_nplikes/typetracer.py index cdf7b307a8..1e2812beb8 100644 --- a/src/awkward/_nplikes/typetracer.py +++ b/src/awkward/_nplikes/typetracer.py @@ -505,7 +505,7 @@ def _scalar_type_of(obj) -> DType: if is_unknown_scalar(obj): return obj.dtype else: - return numpy.obj2sctype(obj) + return numpy.array(obj).dtype def try_touch_data(array: Any): diff --git a/src/awkward/operations/ak_enforce_type.py b/src/awkward/operations/ak_enforce_type.py index 902ed8a369..937c2fde9b 100644 --- a/src/awkward/operations/ak_enforce_type.py +++ b/src/awkward/operations/ak_enforce_type.py @@ -630,7 +630,9 @@ def _recurse_option_any( # Converting to an unknown inside an option! if isinstance(type_.content, ak.types.UnknownType): return ak.contents.IndexedOptionArray( - ak.index.Index64(layout.backend.index_nplike.full(layout.length, -1)), + ak.index.Index64( + layout.backend.index_nplike.full(layout.length, -1, dtype=np.int64) + ), ak.forms.from_type(type_.content).length_zero_array( backend=layout.backend ), @@ -696,7 +698,9 @@ def _recurse_any_option( # Converting to an unknown inside an option! if isinstance(type_.content, ak.types.UnknownType): return ak.contents.IndexedOptionArray( - ak.index.Index64(layout.backend.index_nplike.full(layout.length, -1)), + ak.index.Index64( + layout.backend.index_nplike.full(layout.length, -1, dtype=np.int64) + ), ak.forms.from_type(type_.content).length_zero_array(backend=layout.backend), ) else: @@ -1125,7 +1129,9 @@ def _recurse_record_any( next_contents.append( ak.contents.IndexedOptionArray( ak.index.Index64( - layout.backend.index_nplike.full(layout.length, -1) + layout.backend.index_nplike.full( + layout.length, -1, dtype=np.int64 + ) ), ak.forms.from_type(next_type.content).length_zero_array( backend=layout.backend @@ -1170,7 +1176,9 @@ def _recurse_record_any( next_contents.append( ak.contents.IndexedOptionArray( ak.index.Index64( - layout.backend.index_nplike.full(layout.length, -1) + layout.backend.index_nplike.full( + layout.length, -1, dtype=np.int64 + ) ), ak.forms.from_type(field_type.content).length_zero_array( backend=layout.backend diff --git a/src/awkward/operations/ak_to_dataframe.py b/src/awkward/operations/ak_to_dataframe.py index e0c739d7b2..e6986ed373 100644 --- a/src/awkward/operations/ak_to_dataframe.py +++ b/src/awkward/operations/ak_to_dataframe.py @@ -2,6 +2,9 @@ from __future__ import annotations +from functools import reduce +from operator import iconcat + import awkward as ak from awkward._dispatch import high_level_function from awkward._nplikes.numpy import Numpy @@ -193,7 +196,8 @@ def recurse(layout, row_arrays, col_names): if layout.is_union: return [(ak.operations.to_numpy(layout), row_arrays, col_names)] else: - return sum( + return reduce( + iconcat, ( recurse(layout._getitem_field(n), row_arrays, (*col_names, n)) for n in layout.fields @@ -202,7 +206,8 @@ def recurse(layout, row_arrays, col_names): ) elif isinstance(layout, ak.contents.RecordArray): - return sum( + return reduce( + iconcat, ( recurse(layout._getitem_field(n), row_arrays, (*col_names, n)) for n in layout.fields