[FEA] broadcast assignment through loc[]
- Dominant language
- C++
- Stars
- 9.8k
- Forks
- 1.1k
- Avg merge
- 3d 6m
- Merged PRs (30d)
- 278
Description
**Is your feature request related to a problem? Please describe.**
working with `import cudf as pd`
**Describe the solution you'd like**
```
In [1]: import cudf as pd
In [2]: pd.__version__
Out[2]: '23.02.00'
In [3]: df = pd.DataFrame({'a': range(10)})
In [4]: df.loc[df.a > 5, ['b']] = 'ok'
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/opt/conda/envs/rapids/lib/python3.8/site-packages/cudf/core/dataframe.py in _setitem_tuple_arg(self, key, value)
318 try:
--> 319 columns_df = self._frame._get_columns_by_label(key[1])
320 except KeyError:
/opt/conda/envs/rapids/lib/python3.8/contextlib.py in inner(*args, **kwds)
74 with self._recreate_cm():
---> 75 return func(*args, **kwds)
76 return inner
/opt/conda/envs/rapids/lib/python3.8/site-packages/cudf/core/dataframe.py in _get_columns_by_label(self, labels, downcast)
1902 """
-> 1903 new_data = super()._get_columns_by_label(labels, downcast)
1904 if downcast:
/opt/conda/envs/rapids/lib/python3.8/contextlib.py in inner(*args, **kwds)
74 with self._recreate_cm():
---> 75 return func(*args, **kwds)
76 return inner
/opt/conda/envs/rapids/lib/python3.8/site-packages/cudf/core/frame.py in _get_columns_by_label(self, labels, downcast)
417 """
--> 418 return self._data.select_by_label(labels)
419
/opt/conda/envs/rapids/lib/python3.8/site-packages/cudf/core/column_accessor.py in select_by_label(self, key)
349 elif pd.api.types.is_list_like(key) and not isinstance(key, tuple):
--> 350 return self._select_by_label_list_like(key)
351 else:
/opt/conda/envs/rapids/lib/python3.8/site-packages/cudf/core/column_accessor.py in _select_by_label_list_like(self, key)
464 def _select_by_label_list_like(self, key: Any) -> ColumnAccessor:
--> 465 data = {k: self._grouped_data[k] for k in key}
466 if self.multiindex:
/opt/conda/envs/rapids/lib/python3.8/site-packages/cudf/core/column_accessor.py in (.0)
464 def _select_by_label_list_like(self, key: Any) -> ColumnAccessor:
--> 465 data = {k: self._grouped_data[k] for k in key}
466 if self.multiindex:
KeyError: 'b'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
in
----> 1 df.loc[df.a > 5, ['b']] = 'ok'
/opt/conda/envs/rapids/lib/python3.8/site-packages/cudf/core/dataframe.py in __setitem__(self, key, value)
148 if not isinstance(key, tuple):
149 key = (key, slice(None))
--> 150 return self._setitem_tuple_arg(key, value)
151
152 @_cudf_nvtx_annotate
/opt/conda/envs/rapids/lib/python3.8/contextlib.py in inner(*args, **kwds)
73 def inner(*args, **kwds):
74 with self._recreate_cm():
---> 75 return func(*args, **kwds)
76 return inner
77
/opt/conda/envs/rapids/lib/python3.8/site-packages/cudf/core/dataframe.py in _setitem_tuple_arg(self, key, value)
334 new_col = cudf.Series(value, index=idx)
335 if not self._frame.empty:
--> 336 new_col = new_col._align_to_index(
337 self._frame.index, how="right"
338 )
/opt/conda/envs/rapids/lib/python3.8/site-packages/cudf/core/indexed_frame.py in _align_to_index(self, index, how, sort, allow_non_unique)
2274 if not allow_non_unique:
2275 if not self.index.is_unique or not index.is_unique:
-> 2276 raise ValueError("Cannot align indices with non-unique values")
2277
2278 lhs = cudf.DataFrame._from_data(self._data, index=self.index)
ValueError: Cannot align indices with non-unique values
In [5]: pdf = df.to_pandas()
In [6]: pdf.loc[pdf.a > 5, ['b']] = 'ok'
In [7]: pdf
Out[7]:
a b
0 0 NaN
1 1 NaN
2 2 NaN
3 3 NaN
4 4 NaN
5 5 NaN
6 6 ok
7 7 ok
8 8 ok
9 9 ok
```
Contributor guide
Assessment
This issue has not been assessed yet.