@@ -205,93 +205,100 @@ class SimpleDtypeInfo:
205
205
206
206
207
207
## dtype predicates - use these to maintain consistency
208
- def is_datetime_like (type : ExpressionType ) -> bool :
209
- return type in (DATETIME_DTYPE , TIMESTAMP_DTYPE )
208
+ def is_datetime_like (type_ : ExpressionType ) -> bool :
209
+ return type_ in (DATETIME_DTYPE , TIMESTAMP_DTYPE )
210
210
211
211
212
- def is_date_like (type : ExpressionType ) -> bool :
213
- return type in (DATETIME_DTYPE , TIMESTAMP_DTYPE , DATE_DTYPE )
212
+ def is_date_like (type_ : ExpressionType ) -> bool :
213
+ return type_ in (DATETIME_DTYPE , TIMESTAMP_DTYPE , DATE_DTYPE )
214
214
215
215
216
- def is_time_like (type : ExpressionType ) -> bool :
217
- return type in (DATETIME_DTYPE , TIMESTAMP_DTYPE , TIME_DTYPE )
216
+ def is_time_like (type_ : ExpressionType ) -> bool :
217
+ return type_ in (DATETIME_DTYPE , TIMESTAMP_DTYPE , TIME_DTYPE )
218
218
219
219
220
- def is_binary_like (type : ExpressionType ) -> bool :
221
- return type in (BOOL_DTYPE , BYTES_DTYPE , INT_DTYPE )
220
+ def is_binary_like (type_ : ExpressionType ) -> bool :
221
+ return type_ in (BOOL_DTYPE , BYTES_DTYPE , INT_DTYPE )
222
222
223
223
224
- def is_string_like (type : ExpressionType ) -> bool :
225
- return type in (STRING_DTYPE , BYTES_DTYPE )
224
+ def is_object_like (type_ : Union [ExpressionType , str ]) -> bool :
225
+ # See: https://stackoverflow.com/a/40312924/101923 and
226
+ # https://numpy.org/doc/stable/reference/generated/numpy.dtype.kind.html
227
+ # for the way to identify object type.
228
+ return type_ in ("object" , "O" ) or getattr (type_ , "kind" , None ) == "O"
226
229
227
230
228
- def is_array_like (type : ExpressionType ) -> bool :
229
- return isinstance (type , pd .ArrowDtype ) and isinstance (
230
- type .pyarrow_dtype , pa .ListType
231
+ def is_string_like (type_ : ExpressionType ) -> bool :
232
+ return type_ in (STRING_DTYPE , BYTES_DTYPE )
233
+
234
+
235
+ def is_array_like (type_ : ExpressionType ) -> bool :
236
+ return isinstance (type_ , pd .ArrowDtype ) and isinstance (
237
+ type_ .pyarrow_dtype , pa .ListType
231
238
)
232
239
233
240
234
- def is_array_string_like (type : ExpressionType ) -> bool :
241
+ def is_array_string_like (type_ : ExpressionType ) -> bool :
235
242
return (
236
- isinstance (type , pd .ArrowDtype )
237
- and isinstance (type .pyarrow_dtype , pa .ListType )
238
- and pa .types .is_string (type .pyarrow_dtype .value_type )
243
+ isinstance (type_ , pd .ArrowDtype )
244
+ and isinstance (type_ .pyarrow_dtype , pa .ListType )
245
+ and pa .types .is_string (type_ .pyarrow_dtype .value_type )
239
246
)
240
247
241
248
242
- def is_struct_like (type : ExpressionType ) -> bool :
243
- return isinstance (type , pd .ArrowDtype ) and isinstance (
244
- type .pyarrow_dtype , pa .StructType
249
+ def is_struct_like (type_ : ExpressionType ) -> bool :
250
+ return isinstance (type_ , pd .ArrowDtype ) and isinstance (
251
+ type_ .pyarrow_dtype , pa .StructType
245
252
)
246
253
247
254
248
- def is_json_like (type : ExpressionType ) -> bool :
255
+ def is_json_like (type_ : ExpressionType ) -> bool :
249
256
# TODO: Add JSON type support
250
- return type == STRING_DTYPE
257
+ return type_ == STRING_DTYPE
251
258
252
259
253
- def is_json_encoding_type (type : ExpressionType ) -> bool :
260
+ def is_json_encoding_type (type_ : ExpressionType ) -> bool :
254
261
# Types can be converted into JSON.
255
262
# https://cloud.google.com/bigquery/docs/reference/standard-sql/json_functions#json_encodings
256
- return type != GEO_DTYPE
263
+ return type_ != GEO_DTYPE
257
264
258
265
259
- def is_numeric (type : ExpressionType ) -> bool :
260
- return type in NUMERIC_BIGFRAMES_TYPES_PERMISSIVE
266
+ def is_numeric (type_ : ExpressionType ) -> bool :
267
+ return type_ in NUMERIC_BIGFRAMES_TYPES_PERMISSIVE
261
268
262
269
263
- def is_iterable (type : ExpressionType ) -> bool :
264
- return type in (STRING_DTYPE , BYTES_DTYPE ) or is_array_like (type )
270
+ def is_iterable (type_ : ExpressionType ) -> bool :
271
+ return type_ in (STRING_DTYPE , BYTES_DTYPE ) or is_array_like (type_ )
265
272
266
273
267
- def is_comparable (type : ExpressionType ) -> bool :
268
- return (type is not None ) and is_orderable (type )
274
+ def is_comparable (type_ : ExpressionType ) -> bool :
275
+ return (type_ is not None ) and is_orderable (type_ )
269
276
270
277
271
278
_ORDERABLE_SIMPLE_TYPES = set (
272
279
mapping .dtype for mapping in SIMPLE_TYPES if mapping .orderable
273
280
)
274
281
275
282
276
- def is_orderable (type : ExpressionType ) -> bool :
283
+ def is_orderable (type_ : ExpressionType ) -> bool :
277
284
# On BQ side, ARRAY, STRUCT, GEOGRAPHY, JSON are not orderable
278
- return type in _ORDERABLE_SIMPLE_TYPES
285
+ return type_ in _ORDERABLE_SIMPLE_TYPES
279
286
280
287
281
288
_CLUSTERABLE_SIMPLE_TYPES = set (
282
289
mapping .dtype for mapping in SIMPLE_TYPES if mapping .clusterable
283
290
)
284
291
285
292
286
- def is_clusterable (type : ExpressionType ) -> bool :
293
+ def is_clusterable (type_ : ExpressionType ) -> bool :
287
294
# https://cloud.google.com/bigquery/docs/clustered-tables#cluster_column_types
288
295
# This is based on default database type mapping, could in theory represent in non-default bq type to cluster.
289
- return type in _CLUSTERABLE_SIMPLE_TYPES
296
+ return type_ in _CLUSTERABLE_SIMPLE_TYPES
290
297
291
298
292
- def is_bool_coercable (type : ExpressionType ) -> bool :
299
+ def is_bool_coercable (type_ : ExpressionType ) -> bool :
293
300
# TODO: Implement more bool coercions
294
- return (type is None ) or is_numeric (type ) or is_string_like (type )
301
+ return (type_ is None ) or is_numeric (type_ ) or is_string_like (type_ )
295
302
296
303
297
304
BIGFRAMES_STRING_TO_BIGFRAMES : Dict [DtypeString , Dtype ] = {
0 commit comments