forked from googleapis/python-bigquery-dataframes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
71 lines (55 loc) · 2.35 KB
/
Copy pathutils.py
File metadata and controls
71 lines (55 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import typing
from typing import Iterable, Optional, Union
import bigframes.constants as constants
from bigframes.core import blocks
import bigframes.pandas as bpd
# Internal type alias
ArrayType = Union[bpd.DataFrame, bpd.Series]
def convert_to_dataframe(*input: ArrayType) -> Iterable[bpd.DataFrame]:
return (_convert_to_dataframe(frame) for frame in input)
def _convert_to_dataframe(frame: ArrayType) -> bpd.DataFrame:
if isinstance(frame, bpd.DataFrame):
return frame
if isinstance(frame, bpd.Series):
return frame.to_frame()
raise ValueError(
f"Unsupported type {type(frame)} to convert to DataFrame. {constants.FEEDBACK_LINK}"
)
def convert_to_series(*input: ArrayType) -> Iterable[bpd.Series]:
return (_convert_to_series(frame) for frame in input)
def _convert_to_series(frame: ArrayType) -> bpd.Series:
if isinstance(frame, bpd.DataFrame):
if len(frame.columns) != 1:
raise ValueError(
"To convert into Series, DataFrames can only contain one column. "
f"Try input with only one column. {constants.FEEDBACK_LINK}"
)
label = typing.cast(blocks.Label, frame.columns.tolist()[0])
return typing.cast(bpd.Series, frame[label])
if isinstance(frame, bpd.Series):
return frame
raise ValueError(
f"Unsupported type {type(frame)} to convert to Series. {constants.FEEDBACK_LINK}"
)
def parse_model_endpoint(model_endpoint: str) -> tuple[str, Optional[str]]:
"""Parse model endpoint string to model_name and version."""
model_name = model_endpoint
version = None
at_idx = model_endpoint.find("@")
if at_idx != -1:
version = model_endpoint[at_idx + 1 :]
model_name = model_endpoint[:at_idx]
return model_name, version