@@ -1003,17 +1003,9 @@ def view_fields(self, fields: str | list[str]) -> Self: # type: ignore[name-def
1003
1003
if not set (fields ).issubset (self .field_names ):
1004
1004
raise ValueError (f"Some fields are not found, given: { fields } , available: { self .field_names } " )
1005
1005
1006
- chunks = []
1007
- for chunk in self .struct_array .iterchunks ():
1008
- chunk = cast (pa .StructArray , chunk )
1009
- struct_dict = {}
1010
- for field in fields :
1011
- struct_dict [field ] = chunk .field (field )
1012
- struct_array = pa .StructArray .from_arrays (struct_dict .values (), struct_dict .keys ())
1013
- chunks .append (struct_array )
1014
- pa_array = pa .chunked_array (chunks )
1015
-
1016
- return type (self )(pa_array , validate = False )
1006
+ new = self .copy ()
1007
+ new .pa_table = new .pa_table .select (fields )
1008
+ return new
1017
1009
1018
1010
def set_flat_field (self , field : str , value : ArrayLike , * , keep_dtype : bool = False ) -> None :
1019
1011
"""Set the field from flat-array of values
@@ -1171,16 +1163,9 @@ def pop_fields(self, fields: Iterable[str]):
1171
1163
if not fields .issubset (self .field_names ):
1172
1164
raise ValueError (f"Some fields are not found, given: { fields } , available: { self .field_names } " )
1173
1165
1174
- if len (self .field_names ) - len (fields ) == 0 :
1166
+ fields_to_keep = [field for field in self .field_names if field not in fields ]
1167
+
1168
+ if len (fields_to_keep ) == 0 :
1175
1169
raise ValueError ("Cannot delete all fields" )
1176
1170
1177
- chunks = []
1178
- for chunk in self .struct_array .iterchunks ():
1179
- chunk = cast (pa .StructArray , chunk )
1180
- struct_dict = {}
1181
- for pa_field in chunk .type :
1182
- if pa_field .name not in fields :
1183
- struct_dict [pa_field .name ] = chunk .field (pa_field .name )
1184
- struct_array = pa .StructArray .from_arrays (struct_dict .values (), struct_dict .keys ())
1185
- chunks .append (struct_array )
1186
- self .struct_array = pa .chunked_array (chunks )
1171
+ self .pa_table = self .pa_table .select (fields_to_keep )
0 commit comments