Skip to content

BUG: Can't use iloc assign a list of None to a 1-d subset of a string dataframe. #45469

Closed
@mvashishtha

Description

@mvashishtha

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
s = pd.DataFrame([["A"], ["B"], ["C"]], dtype="string")
s.iloc[[0, 1], :] = [None, None]

Issue Description

On the latest pandas source code (version 1.5.0.dev0+122.gb4e578db85) I get a NotImplementedError:

Stack trace
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
Input In [9], in <module>
----> 1 s.iloc[[0, 1], :] = [None, None]

File ~/pandas/pandas/core/indexing.py:731, in _LocationIndexer.__setitem__(self, key, value)
    728 self._has_valid_setitem_indexer(key)
    730 iloc = self if self.name == "iloc" else self.obj.iloc
--> 731 iloc._setitem_with_indexer(indexer, value, self.name)

File ~/pandas/pandas/core/indexing.py:1704, in _iLocIndexer._setitem_with_indexer(self, indexer, value, name)
   1702     self._setitem_with_indexer_split_path(indexer, value, name)
   1703 else:
-> 1704     self._setitem_single_block(indexer, value, name)

File ~/pandas/pandas/core/indexing.py:1952, in _iLocIndexer._setitem_single_block(self, indexer, value, name)
   1949 self.obj._check_is_chained_assignment_possible()
   1951 # actually do the set
-> 1952 self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value)
   1953 self.obj._maybe_update_cacher(clear=True, inplace=True)

File ~/pandas/pandas/core/internals/managers.py:337, in BaseBlockManager.setitem(self, indexer, value)
    331 def setitem(self: T, indexer, value) -> T:
    332     """
    333     Set values with indexer.
    334
    335     For SingleBlockManager, this backs s[indexer] = value
    336     """
--> 337     return self.apply("setitem", indexer=indexer, value=value)

File ~/pandas/pandas/core/internals/managers.py:304, in BaseBlockManager.apply(self, f, align_keys, ignore_failures, **kwargs)
    302         applied = b.apply(f, **kwargs)
    303     else:
--> 304         applied = getattr(b, f)(**kwargs)
    305 except (TypeError, NotImplementedError):
    306     if not ignore_failures:

File ~/pandas/pandas/core/internals/blocks.py:1394, in EABackedBlock.setitem(self, indexer, value)
   1391     nb = self.coerce_to_target_dtype(value)
   1392     return nb.setitem(indexer, value)
-> 1394 indexer = self._unwrap_setitem_indexer(indexer)
   1395 value = self._maybe_squeeze_arg(value)
   1397 values = self.values

File ~/pandas/pandas/core/internals/blocks.py:1652, in ExtensionBlock._unwrap_setitem_indexer(self, indexer)
   1650             indexer = indexer[0]
   1651         else:
-> 1652             raise NotImplementedError(
   1653                 "This should not be reached. Please report a bug at "
   1654                 "github.com/pandas-dev/pandas/"
   1655             )
   1656 return indexer

NotImplementedError: This should not be reached. Please report a bug at github.com/pandas-dev/pandas/

On the latest pandas release candidate (version 1.4.0rc0) I get a ValueError:

Stack trace
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [1], in <module>
      1 import pandas as pd
      3 s = pd.DataFrame([["A"], ["B"], ["C"]], dtype="string")
----> 4 s.iloc[[0, 1], :] = [None, None]

File /usr/local/lib/python3.9/site-packages/pandas/core/indexing.py:715, in _LocationIndexer.__setitem__(self, key, value)
    712 self._has_valid_setitem_indexer(key)
    714 iloc = self if self.name == "iloc" else self.obj.iloc
--> 715 iloc._setitem_with_indexer(indexer, value, self.name)

File /usr/local/lib/python3.9/site-packages/pandas/core/indexing.py:1689, in _iLocIndexer._setitem_with_indexer(self, indexer, value, name)
   1687     self._setitem_with_indexer_split_path(indexer, value, name)
   1688 else:
-> 1689     self._setitem_single_block(indexer, value, name)

File /usr/local/lib/python3.9/site-packages/pandas/core/indexing.py:1937, in _iLocIndexer._setitem_single_block(self, indexer, value, name)
   1934 self.obj._check_is_chained_assignment_possible()
   1936 # actually do the set
-> 1937 self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value)
   1938 self.obj._maybe_update_cacher(clear=True, inplace=True)

File /usr/local/lib/python3.9/site-packages/pandas/core/internals/managers.py:337, in BaseBlockManager.setitem(self, indexer, value)
    331 def setitem(self: T, indexer, value) -> T:
    332     """
    333     Set values with indexer.
    334
    335     For SingleBlockManager, this backs s[indexer] = value
    336     """
--> 337     return self.apply("setitem", indexer=indexer, value=value)

File /usr/local/lib/python3.9/site-packages/pandas/core/internals/managers.py:304, in BaseBlockManager.apply(self, f, align_keys, ignore_failures, **kwargs)
    302         applied = b.apply(f, **kwargs)
    303     else:
--> 304         applied = getattr(b, f)(**kwargs)
    305 except (TypeError, NotImplementedError):
    306     if not ignore_failures:

File /usr/local/lib/python3.9/site-packages/pandas/core/internals/blocks.py:1595, in ExtensionBlock.setitem(self, indexer, value)
   1592     value = value._ixs(0, axis=1)._values
   1594 check_setitem_lengths(indexer, value, self.values)
-> 1595 self.values[indexer] = value
   1596 return self

File /usr/local/lib/python3.9/site-packages/pandas/core/arrays/string_.py:412, in StringArray.__setitem__(self, key, value)
    410         value = np.asarray(value, dtype=object)
    411     if len(value) and not lib.is_string_array(value, skipna=True):
--> 412         raise ValueError("Must provide strings.")
    414 super().__setitem__(key, value)

ValueError: Must provide strings.

On the version of pandas that I was using when I found this bug (version 1.3.5) I get a ValueError:

Stack trace
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [1], in <module>
      1 import pandas as pd
      3 s = pd.DataFrame([["A"], ["B"], ["C"]], dtype="string")
----> 4 s.iloc[[0, 1], :] = [None, None]

File /usr/local/lib/python3.9/site-packages/pandas/core/indexing.py:723, in _LocationIndexer.__setitem__(self, key, value)
    720 self._has_valid_setitem_indexer(key)
    722 iloc = self if self.name == "iloc" else self.obj.iloc
--> 723 iloc._setitem_with_indexer(indexer, value, self.name)

File /usr/local/lib/python3.9/site-packages/pandas/core/indexing.py:1732, in _iLocIndexer._setitem_with_indexer(self, indexer, value, name)
   1730     self._setitem_with_indexer_split_path(indexer, value, name)
   1731 else:
-> 1732     self._setitem_single_block(indexer, value, name)

File /usr/local/lib/python3.9/site-packages/pandas/core/indexing.py:1968, in _iLocIndexer._setitem_single_block(self, indexer, value, name)
   1965 self.obj._check_is_chained_assignment_possible()
   1967 # actually do the set
-> 1968 self.obj._mgr = self.obj._mgr.setitem(indexer=indexer, value=value)
   1969 self.obj._maybe_update_cacher(clear=True)

File /usr/local/lib/python3.9/site-packages/pandas/core/internals/managers.py:355, in BaseBlockManager.setitem(self, indexer, value)
    354 def setitem(self: T, indexer, value) -> T:
--> 355     return self.apply("setitem", indexer=indexer, value=value)

File /usr/local/lib/python3.9/site-packages/pandas/core/internals/managers.py:327, in BaseBlockManager.apply(self, f, align_keys, ignore_failures, **kwargs)
    325         applied = b.apply(f, **kwargs)
    326     else:
--> 327         applied = getattr(b, f)(**kwargs)
    328 except (TypeError, NotImplementedError):
    329     if not ignore_failures:

File /usr/local/lib/python3.9/site-packages/pandas/core/internals/blocks.py:1491, in ExtensionBlock.setitem(self, indexer, value)
   1488     indexer = indexer[0]
   1490 check_setitem_lengths(indexer, value, self.values)
-> 1491 self.values[indexer] = value
   1492 return self

File /usr/local/lib/python3.9/site-packages/pandas/core/arrays/string_.py:413, in StringArray.__setitem__(self, key, value)
    411         value = np.asarray(value, dtype=object)
    412     if len(value) and not lib.is_string_array(value, skipna=True):
--> 413         raise ValueError("Must provide strings.")
    415 super().__setitem__(key, value)

ValueError: Must provide strings.

Expected Behavior

After the code example above, s should be missing entries for the first two rows, so it should look like:

      0
0  <NA>
1  <NA>
2     C

Installed Versions

INSTALLED VERSIONS

commit : 66e3805
python : 3.9.9.final.0
python-bits : 64
OS : Darwin
OS-release : 20.6.0
Version : Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:31 PDT 2021; root:xnu-7195.141.2~5/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.5
numpy : 1.22.1
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 59.0.1
Cython : None
pytest : 6.2.5
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.0.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 2022.01.0
fastparquet : None
gcsfs : None
matplotlib : 3.5.1
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.9
pandas_gbq : 0.16.0
pyarrow : 6.0.1
pyxlsb : None
s3fs : 2022.01.0
scipy : 1.7.3
sqlalchemy : 1.4.29
tables : 3.7.0
tabulate : None
xarray : 0.20.2
xlrd : 2.0.1
xlwt : None
numba : None

Note that as I mentioned above, I get different errors even with newer versions of pandas.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesNA - MaskedArraysRelated to pd.NA and nullable extension arraysStringsString extension data type and string data

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions