Skip to content

Commit

Permalink
Consolidate set_cell immutability behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Oct 9, 2024
1 parent e501880 commit d40040b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
exclude: "(.*\\.svg)|(.*\\.qmd)|(.*\\.ambr)|(.*\\.csv)|(.*\\.txt)|(.*\\.json)"
repos:
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 7.1.1
hooks:
- id: flake8
types:
Expand Down
19 changes: 12 additions & 7 deletions great_tables/_tbl_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,20 @@ def _set_cell(data: DataFrameLike, row: int, column: str, value: Any):
def _(data, row: int, column: str, value: Any) -> PdDataFrame:
# TODO: This assumes column names are unique
# if this is violated, get_loc will return a mask
col_indx = data.columns.get_loc(column)
data.iloc[row, col_indx] = value
return data
data_new = data.copy(deep=False) # make a shallow copy and only update the specific column.
data_new[column] = data_new[column].copy()
data_new.at[row, column] = value
return data_new


@_set_cell.register(PlDataFrame)
def _(data, row: int, column: str, value: Any) -> PlDataFrame:
data[row, column] = value
return data
# While using scatter is considered an antipattern,
# it is easier to read than a when.then.otherwise expression,
# and it is generally better performing.
col_series_modified = data[column].scatter(row, value)
data_new = data.with_columns(col_series_modified)
return data_new


@_set_cell.register(PyArrowTable)
Expand All @@ -268,8 +273,8 @@ def _(data: PyArrowTable, row: int, column: str, value: Any) -> PyArrowTable:
col = data.column(column)
pylist = col.to_pylist()
pylist[row] = value
data = data.set_column(colindex, column, pa.array(pylist))
return data
data_new = data.set_column(colindex, column, pa.array(pylist))
return data_new


# _get_column_dtype ----
Expand Down

0 comments on commit d40040b

Please sign in to comment.