Skip to content

Commit 38b06e2

Browse files
tswastarwas11
authored andcommitted
deps: re-introduce support for numpy 1.24.x (#931)
1 parent 1e29edc commit 38b06e2

File tree

5 files changed

+53
-41
lines changed

5 files changed

+53
-41
lines changed

bigframes/dataframe.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,15 @@ def __init__(
112112
*,
113113
session: typing.Optional[bigframes.session.Session] = None,
114114
):
115+
global bigframes
116+
115117
if copy is not None and not copy:
116118
raise ValueError(
117119
f"DataFrame constructor only supports copy=True. {constants.FEEDBACK_LINK}"
118120
)
119-
# just ignore object dtype if provided
120-
if dtype in {numpy.dtypes.ObjectDType, "object"}:
121+
# Ignore object dtype if provided, as it provides no additional
122+
# information about what BigQuery type to use.
123+
if dtype is not None and bigframes.dtypes.is_object_like(dtype):
121124
dtype = None
122125

123126
# Check to see if constructing from BigQuery-backed objects before

bigframes/dtypes.py

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -205,93 +205,100 @@ class SimpleDtypeInfo:
205205

206206

207207
## 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)
210210

211211

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)
214214

215215

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)
218218

219219

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)
222222

223223

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"
226229

227230

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
231238
)
232239

233240

234-
def is_array_string_like(type: ExpressionType) -> bool:
241+
def is_array_string_like(type_: ExpressionType) -> bool:
235242
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)
239246
)
240247

241248

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
245252
)
246253

247254

248-
def is_json_like(type: ExpressionType) -> bool:
255+
def is_json_like(type_: ExpressionType) -> bool:
249256
# TODO: Add JSON type support
250-
return type == STRING_DTYPE
257+
return type_ == STRING_DTYPE
251258

252259

253-
def is_json_encoding_type(type: ExpressionType) -> bool:
260+
def is_json_encoding_type(type_: ExpressionType) -> bool:
254261
# Types can be converted into JSON.
255262
# https://cloud.google.com/bigquery/docs/reference/standard-sql/json_functions#json_encodings
256-
return type != GEO_DTYPE
263+
return type_ != GEO_DTYPE
257264

258265

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
261268

262269

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_)
265272

266273

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_)
269276

270277

271278
_ORDERABLE_SIMPLE_TYPES = set(
272279
mapping.dtype for mapping in SIMPLE_TYPES if mapping.orderable
273280
)
274281

275282

276-
def is_orderable(type: ExpressionType) -> bool:
283+
def is_orderable(type_: ExpressionType) -> bool:
277284
# On BQ side, ARRAY, STRUCT, GEOGRAPHY, JSON are not orderable
278-
return type in _ORDERABLE_SIMPLE_TYPES
285+
return type_ in _ORDERABLE_SIMPLE_TYPES
279286

280287

281288
_CLUSTERABLE_SIMPLE_TYPES = set(
282289
mapping.dtype for mapping in SIMPLE_TYPES if mapping.clusterable
283290
)
284291

285292

286-
def is_clusterable(type: ExpressionType) -> bool:
293+
def is_clusterable(type_: ExpressionType) -> bool:
287294
# https://cloud.google.com/bigquery/docs/clustered-tables#cluster_column_types
288295
# 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
290297

291298

292-
def is_bool_coercable(type: ExpressionType) -> bool:
299+
def is_bool_coercable(type_: ExpressionType) -> bool:
293300
# 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_)
295302

296303

297304
BIGFRAMES_STRING_TO_BIGFRAMES: Dict[DtypeString, Dtype] = {

bigframes/operations/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from typing import List, Sequence
1919

2020
import bigframes_vendored.pandas.pandas._typing as vendored_pandas_typing
21-
import numpy
2221
import pandas as pd
2322

2423
import bigframes.constants as constants
@@ -49,8 +48,9 @@ def __init__(
4948
):
5049
import bigframes.pandas
5150

52-
# just ignore object dtype if provided
53-
if dtype in {numpy.dtypes.ObjectDType, "object"}:
51+
# Ignore object dtype if provided, as it provides no additional
52+
# information about what BigQuery type to use.
53+
if dtype is not None and bigframes.dtypes.is_object_like(dtype):
5454
dtype = None
5555

5656
read_pandas_func = (

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"google-cloud-storage >=2.0.0",
5050
"ibis-framework[bigquery] >=8.0.0,<9.0.0dev",
5151
"jellyfish >=0.8.9",
52+
"numpy >=1.24.0",
5253
# TODO: Relax upper bound once we have fixed `system_prerelease` tests.
5354
"pandas >=1.5.0",
5455
"pyarrow >=8.0.0",

testing/constraints-3.9.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ google-cloud-resource-manager==1.10.3
1414
google-cloud-storage==2.0.0
1515
ibis-framework==8.0.0
1616
jellyfish==0.8.9
17+
numpy==1.24.0
1718
pandas==1.5.0
1819
pyarrow==8.0.0
1920
pydata-google-auth==1.8.2

0 commit comments

Comments
 (0)