Skip to content

Commit 21e6e9b

Browse files
Add append parameter
1 parent 1c4de5b commit 21e6e9b

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

pandas/core/frame.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3554,6 +3554,7 @@ def to_iceberg(
35543554
*,
35553555
catalog_properties: dict[str, Any] | None = None,
35563556
location: str | None = None,
3557+
append: bool = False,
35573558
snapshot_properties: dict[str, str] | None = None,
35583559
) -> None:
35593560
"""
@@ -3575,6 +3576,8 @@ def to_iceberg(
35753576
The properties that are used next to the catalog configuration.
35763577
location : str, optional
35773578
Location for the table.
3579+
append : bool, default False
3580+
If ``True``, append data to the table, instead of replacing the content.
35783581
snapshot_properties : dict of {str: str}, optional
35793582
Custom properties to be added to the snapshot summary
35803583
@@ -3596,6 +3599,7 @@ def to_iceberg(
35963599
catalog_name,
35973600
catalog_properties=catalog_properties,
35983601
location=location,
3602+
append=append,
35993603
snapshot_properties=snapshot_properties,
36003604
)
36013605

pandas/io/iceberg.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def to_iceberg(
102102
*,
103103
catalog_properties: dict[str, Any] | None = None,
104104
location: str | None = None,
105+
append: bool = False,
105106
snapshot_properties: dict[str, str] | None = None,
106107
) -> None:
107108
"""
@@ -119,6 +120,8 @@ def to_iceberg(
119120
The properties that are used next to the catalog configuration.
120121
location : str, optional
121122
Location for the table.
123+
append : bool, default False
124+
If ``True``, append data to the table, instead of replacing the content.
122125
snapshot_properties : dict of {str: str}, optional
123126
Custom properties to be added to the snapshot summary
124127
@@ -142,4 +145,7 @@ def to_iceberg(
142145
)
143146
if snapshot_properties is None:
144147
snapshot_properties = {}
145-
table.append(arrow_table, snapshot_properties=snapshot_properties)
148+
if append:
149+
table.append(arrow_table, snapshot_properties=snapshot_properties)
150+
else:
151+
table.overwrite(arrow_table, snapshot_properties=snapshot_properties)

pandas/tests/io/test_iceberg.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def test_write_by_catalog_name(self, catalog):
178178
)
179179
tm.assert_frame_equal(result, df)
180180

181-
def test_write_existing_table(self, catalog):
181+
def test_write_existing_table_with_append_true(self, catalog):
182182
original = read_iceberg(
183183
"ns.my_table",
184184
catalog_properties={"uri": catalog.uri},
@@ -194,9 +194,29 @@ def test_write_existing_table(self, catalog):
194194
"ns.my_table",
195195
catalog_properties={"uri": catalog.uri},
196196
location=catalog.warehouse,
197+
append=True,
197198
)
198199
result = read_iceberg(
199200
"ns.my_table",
200201
catalog_properties={"uri": catalog.uri},
201202
)
202203
tm.assert_frame_equal(result, expected)
204+
205+
def test_write_existing_table_with_append_false(self, catalog):
206+
df = pd.DataFrame(
207+
{
208+
"A": [1, 2, 3],
209+
"B": ["foo", "foo", "foo"],
210+
}
211+
)
212+
df.to_iceberg(
213+
"ns.my_table",
214+
catalog_properties={"uri": catalog.uri},
215+
location=catalog.warehouse,
216+
append=False,
217+
)
218+
result = read_iceberg(
219+
"ns.my_table",
220+
catalog_properties={"uri": catalog.uri},
221+
)
222+
tm.assert_frame_equal(result, df)

0 commit comments

Comments
 (0)