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
243 lines (201 loc) · 9.46 KB
/
Copy pathutils.py
File metadata and controls
243 lines (201 loc) · 9.46 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# 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 base64
import decimal
import functools
import geopandas as gpd # type: ignore
import numpy as np
import pandas as pd
import pyarrow as pa # type: ignore
import pytest
def skip_legacy_pandas(test):
@functools.wraps(test)
def wrapper(*args, **kwds):
if pd.__version__.startswith("1."):
pytest.skip("Skips pandas 1.x as not compatible with 2.x behavior.")
return test(*args, **kwds)
return wrapper
def assert_pandas_df_equal(df0, df1, ignore_order: bool = False, **kwargs):
if ignore_order:
# Sort by a column to get consistent results.
if df0.index.name != "rowindex":
df0 = df0.sort_values(
list(df0.columns.drop("geography_col", errors="ignore"))
).reset_index(drop=True)
df1 = df1.sort_values(
list(df1.columns.drop("geography_col", errors="ignore"))
).reset_index(drop=True)
else:
df0 = df0.sort_index()
df1 = df1.sort_index()
pd.testing.assert_frame_equal(df0, df1, **kwargs)
def assert_series_equal(
left: pd.Series, right: pd.Series, ignore_order: bool = False, **kwargs
):
if ignore_order:
if left.index.name is None:
left = left.sort_values().reset_index(drop=True)
right = right.sort_values().reset_index(drop=True)
else:
left = left.sort_index()
right = right.sort_index()
pd.testing.assert_series_equal(left, right, **kwargs)
def _standardize_index(idx):
return pd.Index(list(idx), name=idx.name)
def assert_pandas_index_equal_ignore_index_type(idx0, idx1):
idx0 = _standardize_index(idx0)
idx1 = _standardize_index(idx1)
pd.testing.assert_index_equal(idx0, idx1)
def convert_pandas_dtypes(df: pd.DataFrame, bytes_col: bool):
"""Convert pandas dataframe dtypes compatible with bigframes dataframe."""
# TODO(chelsealin): updates the function to accept dtypes as input rather than
# hard-code the column names here.
# Convert basic types columns
df["bool_col"] = df["bool_col"].astype(pd.BooleanDtype())
df["int64_col"] = df["int64_col"].astype(pd.Int64Dtype())
df["int64_too"] = df["int64_too"].astype(pd.Int64Dtype())
df["float64_col"] = df["float64_col"].astype(pd.Float64Dtype())
df["string_col"] = df["string_col"].astype(pd.StringDtype(storage="pyarrow"))
if "rowindex" in df.columns:
df["rowindex"] = df["rowindex"].astype(pd.Int64Dtype())
if "rowindex_2" in df.columns:
df["rowindex_2"] = df["rowindex_2"].astype(pd.Int64Dtype())
# Convert time types columns. The `astype` works for Pandas 2.0 but hits an assert
# error at Pandas 1.5. Hence, we have to convert to arrow table and convert back
# to pandas dataframe.
if not isinstance(df["date_col"].dtype, pd.ArrowDtype):
df["date_col"] = pd.to_datetime(df["date_col"], format="%Y-%m-%d")
arrow_table = pa.Table.from_pandas(
pd.DataFrame(df, columns=["date_col"]),
schema=pa.schema([("date_col", pa.date32())]),
)
df["date_col"] = arrow_table.to_pandas(types_mapper=pd.ArrowDtype)["date_col"]
if not isinstance(df["datetime_col"].dtype, pd.ArrowDtype):
df["datetime_col"] = pd.to_datetime(
df["datetime_col"], format="%Y-%m-%d %H:%M:%S"
)
arrow_table = pa.Table.from_pandas(
pd.DataFrame(df, columns=["datetime_col"]),
schema=pa.schema([("datetime_col", pa.timestamp("us"))]),
)
df["datetime_col"] = arrow_table.to_pandas(types_mapper=pd.ArrowDtype)[
"datetime_col"
]
if not isinstance(df["time_col"].dtype, pd.ArrowDtype):
df["time_col"] = pd.to_datetime(df["time_col"], format="%H:%M:%S.%f")
arrow_table = pa.Table.from_pandas(
pd.DataFrame(df, columns=["time_col"]),
schema=pa.schema([("time_col", pa.time64("us"))]),
)
df["time_col"] = arrow_table.to_pandas(types_mapper=pd.ArrowDtype)["time_col"]
if not isinstance(df["timestamp_col"].dtype, pd.ArrowDtype):
df["timestamp_col"] = pd.to_datetime(
df["timestamp_col"], format="%Y-%m-%d %H:%M:%S.%f%Z"
)
arrow_table = pa.Table.from_pandas(
pd.DataFrame(df, columns=["timestamp_col"]),
schema=pa.schema([("timestamp_col", pa.timestamp("us", tz="UTC"))]),
)
df["timestamp_col"] = arrow_table.to_pandas(types_mapper=pd.ArrowDtype)[
"timestamp_col"
]
# Convert geography types columns.
if "geography_col" in df.columns:
df["geography_col"] = df["geography_col"].astype(
pd.StringDtype(storage="pyarrow")
)
df["geography_col"] = gpd.GeoSeries.from_wkt(
df["geography_col"].replace({np.nan: None})
)
if bytes_col and not isinstance(df["bytes_col"].dtype, pd.ArrowDtype):
df["bytes_col"] = df["bytes_col"].apply(
lambda value: base64.b64decode(value) if not pd.isnull(value) else value
)
arrow_table = pa.Table.from_pandas(
pd.DataFrame(df, columns=["bytes_col"]),
schema=pa.schema([("bytes_col", pa.binary())]),
)
df["bytes_col"] = arrow_table.to_pandas(types_mapper=pd.ArrowDtype)["bytes_col"]
if not isinstance(df["numeric_col"].dtype, pd.ArrowDtype):
# Convert numeric types column.
df["numeric_col"] = df["numeric_col"].apply(
lambda value: decimal.Decimal(str(value)) if value else None # type: ignore
)
arrow_table = pa.Table.from_pandas(
pd.DataFrame(df, columns=["numeric_col"]),
schema=pa.schema([("numeric_col", pa.decimal128(38, 9))]),
)
df["numeric_col"] = arrow_table.to_pandas(types_mapper=pd.ArrowDtype)[
"numeric_col"
]
def assert_pandas_df_equal_pca_components(actual, expected, **kwargs):
"""Compare two pandas dataframes representing PCA components. The columns
required to be present in the dataframes are:
numerical_value: numeric,
categorical_value: List[object(category, value)]
The index types of `actual` and `expected` are ignored in the comparison.
Args:
actual: Actual Pandas DataFrame
expected: Expected Pandas DataFrame
kwargs: kwargs to use in `pandas.testing.assert_series_equal` per column
"""
# Compare the index, columns and values separately, as the polarity of the
# PCA vectors can be arbitrary
pd.testing.assert_index_equal(
actual.index, expected.index.astype(actual.index.dtype)
) # dtype agnostic index comparison
pd.testing.assert_index_equal(actual.columns, expected.columns)
for column in expected.columns:
try:
pd.testing.assert_series_equal(actual[column], expected[column], **kwargs)
except AssertionError:
if column not in {"numerical_value", "categorical_value"}:
raise
# Allow for sign difference per numeric/categorical column
if column == "numerical_value":
actual_ = -actual[column]
expected_ = expected[column]
else:
# In this column each element is an array of objects, where the
# object has attributes "category" and "value". For the sake of
# comparison let's normalize by flipping the polarity of "value".
def normalize_array_of_objects(arr, reverse_polarity=False):
newarr = []
for element in arr:
newelement = dict(element)
if reverse_polarity:
newelement["value"] = -newelement["value"]
newarr.append(newelement)
return sorted(newarr, key=lambda d: d["category"])
actual_ = actual[column].apply(normalize_array_of_objects, args=(True,))
expected_ = expected[column].apply(normalize_array_of_objects)
pd.testing.assert_series_equal(actual_, expected_, **kwargs)
def assert_pandas_df_equal_pca(actual, expected, **kwargs):
"""Compare two pandas dataframes representing PCA predictions. The columns
in the dataframes are expected to be numeric.
Args:
actual: Actual Pandas DataFrame
expected: Expected Pandas DataFrame
kwargs: kwargs to use in `pandas.testing.assert_series_equal` per column
"""
# Compare the index, columns and values separately, as the polarity of the
# PCA vector can be arbitrary
pd.testing.assert_index_equal(actual.index, expected.index)
pd.testing.assert_index_equal(actual.columns, expected.columns)
for column in expected.columns:
try:
pd.testing.assert_series_equal(actual[column], expected[column], **kwargs)
except AssertionError:
# Allow for sign difference per column
pd.testing.assert_series_equal(-actual[column], expected[column], **kwargs)