|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import datetime |
| 16 | +from typing import Dict |
| 17 | + |
| 18 | +import geopandas # type: ignore |
| 19 | +import pandas |
| 20 | +import pandas.testing |
| 21 | +import pyarrow # type: ignore |
| 22 | +import pytest |
| 23 | + |
| 24 | +import bigframes.session._io.pandas |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize( |
| 28 | + ("arrow_table", "dtypes", "expected"), |
| 29 | + ( |
| 30 | + pytest.param( |
| 31 | + pyarrow.Table.from_pydict({}), |
| 32 | + {}, |
| 33 | + pandas.DataFrame(), |
| 34 | + id="empty-df", |
| 35 | + ), |
| 36 | + pytest.param( |
| 37 | + pyarrow.Table.from_pydict( |
| 38 | + { |
| 39 | + "bool": [True, None, False], |
| 40 | + "bytes": [b"123", None, b"abc"], |
| 41 | + "date": pyarrow.array( |
| 42 | + [datetime.date(2023, 8, 29), None, datetime.date(2024, 4, 9)], |
| 43 | + type=pyarrow.date32(), |
| 44 | + ), |
| 45 | + "datetime": pyarrow.array( |
| 46 | + [ |
| 47 | + datetime.datetime(2023, 8, 29), |
| 48 | + None, |
| 49 | + datetime.datetime(2024, 4, 9, 23, 59, 59), |
| 50 | + ], |
| 51 | + type=pyarrow.timestamp("us"), |
| 52 | + ), |
| 53 | + "string": ["123", None, "abc"], |
| 54 | + "time": pyarrow.array( |
| 55 | + [ |
| 56 | + datetime.time(0, 0, 0, 1), |
| 57 | + None, |
| 58 | + datetime.time(23, 59, 59, 999999), |
| 59 | + ], |
| 60 | + type=pyarrow.time64("us"), |
| 61 | + ), |
| 62 | + "timestamp": pyarrow.array( |
| 63 | + [ |
| 64 | + datetime.datetime(2023, 8, 29), |
| 65 | + None, |
| 66 | + datetime.datetime(2024, 4, 9, 23, 59, 59), |
| 67 | + ], |
| 68 | + type=pyarrow.timestamp("us", datetime.timezone.utc), |
| 69 | + ), |
| 70 | + } |
| 71 | + ), |
| 72 | + { |
| 73 | + "bool": "boolean", |
| 74 | + "bytes": "object", |
| 75 | + "date": pandas.ArrowDtype(pyarrow.date32()), |
| 76 | + "datetime": pandas.ArrowDtype(pyarrow.timestamp("us")), |
| 77 | + "string": "string[pyarrow]", |
| 78 | + "time": pandas.ArrowDtype(pyarrow.time64("us")), |
| 79 | + "timestamp": pandas.ArrowDtype( |
| 80 | + pyarrow.timestamp("us", datetime.timezone.utc) |
| 81 | + ), |
| 82 | + }, |
| 83 | + pandas.DataFrame( |
| 84 | + { |
| 85 | + "bool": pandas.Series([True, None, False], dtype="boolean"), |
| 86 | + "bytes": [b"123", None, b"abc"], |
| 87 | + "date": pandas.Series( |
| 88 | + [datetime.date(2023, 8, 29), None, datetime.date(2024, 4, 9)], |
| 89 | + dtype=pandas.ArrowDtype(pyarrow.date32()), |
| 90 | + ), |
| 91 | + "datetime": pandas.Series( |
| 92 | + [ |
| 93 | + datetime.datetime(2023, 8, 29), |
| 94 | + None, |
| 95 | + datetime.datetime(2024, 4, 9, 23, 59, 59), |
| 96 | + ], |
| 97 | + dtype=pandas.ArrowDtype(pyarrow.timestamp("us")), |
| 98 | + ), |
| 99 | + "string": pandas.Series( |
| 100 | + ["123", None, "abc"], dtype="string[pyarrow]" |
| 101 | + ), |
| 102 | + "time": pandas.Series( |
| 103 | + [ |
| 104 | + datetime.time(0, 0, 0, 1), |
| 105 | + None, |
| 106 | + datetime.time(23, 59, 59, 999999), |
| 107 | + ], |
| 108 | + dtype=pandas.ArrowDtype(pyarrow.time64("us")), |
| 109 | + ), |
| 110 | + "timestamp": pandas.Series( |
| 111 | + [ |
| 112 | + datetime.datetime(2023, 8, 29), |
| 113 | + None, |
| 114 | + datetime.datetime(2024, 4, 9, 23, 59, 59), |
| 115 | + ], |
| 116 | + dtype=pandas.ArrowDtype( |
| 117 | + pyarrow.timestamp("us", datetime.timezone.utc) |
| 118 | + ), |
| 119 | + ), |
| 120 | + } |
| 121 | + ), |
| 122 | + id="scalar-dtypes", |
| 123 | + ), |
| 124 | + pytest.param( |
| 125 | + pyarrow.Table.from_pydict( |
| 126 | + { |
| 127 | + "geocol": [ |
| 128 | + "POINT(32 210)", |
| 129 | + None, |
| 130 | + "LINESTRING(1 1, 2 1, 3.1 2.88, 3 -3)", |
| 131 | + ] |
| 132 | + } |
| 133 | + ), |
| 134 | + {"geocol": geopandas.array.GeometryDtype()}, |
| 135 | + pandas.DataFrame( |
| 136 | + { |
| 137 | + "geocol": geopandas.GeoSeries.from_wkt( |
| 138 | + ["POINT(32 210)", None, "LINESTRING(1 1, 2 1, 3.1 2.88, 3 -3)"], |
| 139 | + crs="EPSG:4326", |
| 140 | + ), |
| 141 | + } |
| 142 | + ), |
| 143 | + id="geography-dtype", |
| 144 | + ), |
| 145 | + ), |
| 146 | +) |
| 147 | +def test_arrow_to_pandas( |
| 148 | + arrow_table: pyarrow.Table | pyarrow.RecordBatch, |
| 149 | + dtypes: Dict, |
| 150 | + expected: pandas.DataFrame, |
| 151 | +): |
| 152 | + actual = bigframes.session._io.pandas.arrow_to_pandas(arrow_table, dtypes) |
| 153 | + pandas.testing.assert_frame_equal(actual, expected) |
| 154 | + |
| 155 | + |
| 156 | +@pytest.mark.parametrize( |
| 157 | + ("arrow_table", "dtypes"), |
| 158 | + ( |
| 159 | + pytest.param( |
| 160 | + pyarrow.Table.from_pydict({"col1": [1], "col2": [2]}), |
| 161 | + {"col1": "Int64"}, |
| 162 | + id="too-few-dtypes", |
| 163 | + ), |
| 164 | + pytest.param( |
| 165 | + pyarrow.RecordBatch.from_pydict({"col1": [1]}), |
| 166 | + {"col1": "Int64", "col2": "string[pyarrow]"}, |
| 167 | + id="too-many-dtypes", |
| 168 | + ), |
| 169 | + ), |
| 170 | +) |
| 171 | +def test_arrow_to_pandas_wrong_size_dtypes( |
| 172 | + arrow_table: pyarrow.Table | pyarrow.RecordBatch, dtypes: Dict |
| 173 | +): |
| 174 | + with pytest.raises(ValueError, match=f"Number of types {len(dtypes)}"): |
| 175 | + bigframes.session._io.pandas.arrow_to_pandas(arrow_table, dtypes) |
0 commit comments