|
| 1 | +# Copyright 2025 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 | +import pandas as pd |
| 15 | +import pandas.testing |
| 16 | +import pyarrow as pa |
| 17 | + |
| 18 | +from bigframes import dtypes |
| 19 | +from bigframes.core import local_data |
| 20 | + |
| 21 | +pd_data = pd.DataFrame( |
| 22 | + { |
| 23 | + "ints": [10, 20, 30, 40], |
| 24 | + "nested_ints": [[1, 2], [3, 4, 5], [], [20, 30]], |
| 25 | + "structs": [{"a": 100}, {}, {"b": 200}, {"b": 300}], |
| 26 | + } |
| 27 | +) |
| 28 | + |
| 29 | +pd_data_normalized = pd.DataFrame( |
| 30 | + { |
| 31 | + "ints": pd.Series([10, 20, 30, 40], dtype=dtypes.INT_DTYPE), |
| 32 | + "nested_ints": pd.Series( |
| 33 | + [[1, 2], [3, 4, 5], [], [20, 30]], dtype=pd.ArrowDtype(pa.list_(pa.int64())) |
| 34 | + ), |
| 35 | + "structs": pd.Series( |
| 36 | + [{"a": 100}, {}, {"b": 200}, {"b": 300}], |
| 37 | + dtype=pd.ArrowDtype(pa.struct({"a": pa.int64(), "b": pa.int64()})), |
| 38 | + ), |
| 39 | + } |
| 40 | +) |
| 41 | + |
| 42 | + |
| 43 | +def test_local_data_well_formed_round_trip(): |
| 44 | + local_entry = local_data.ManagedArrowTable.from_pandas(pd_data) |
| 45 | + result = pd.DataFrame(local_entry.itertuples(), columns=pd_data.columns) |
| 46 | + pandas.testing.assert_frame_equal(pd_data_normalized, result, check_dtype=False) |
0 commit comments