-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Description
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
class MyWeirdObject:
def __iter__(self):
raise TypeError("actually not iterable!")
# this is OK
df = pd.DataFrame({
"foo": [MyWeirdObject()],
"bar": [123],
})
# this works OK
df.loc[1] = [MyWeirdObject(), 456]
# this fails
df.loc[2, "foo"] = MyWeirdObject()Issue Description
Assigning an object with .loc[row, col] = ... fails with TypeError: object of type ... has no len() when the object implements __iter__ but not __len__.
The exception is raised here:
pandas/pandas/core/indexing.py
Line 2005 in 1b5b02c
| elif len(ilocs) == 1 and lplane_indexer == len(value) and not is_scalar(pi): |
The root cause is that is_list_like_indexer calls is_list_like which checks if the object implements __iter__ but not __len__.
In general, iterators may not have a length defined, but for me, this came up when I was using TypedUnits:
from tunits.units import MHz
df.loc[3, "foo"] = 1 * MHz # raises exceptionTypedUnits values with units implement __iter__ which just raises an exception, which is due to a quirk in Python - if __iter__ is not defined, iter(obj) will instead do __getitem__(0), __getitem__(1), ... until IndexError is raised, but here the library authors want to avoid the call to __getitem__.
(While browsing around, I found at least
pandas/pandas/core/indexing.py
Line 2305 in 1b5b02c
| if is_list_like_indexer(value): |
len() is called after checking is_list_like.
Expected Behavior
Assignment should not fail with object of type ... has no len(). In my case, it should be treated as a scalar value, although I'm not sure about general iterators with no length defined.
Installed Versions
INSTALLED VERSIONS
commit : 9c8bc3e
python : 3.13.5
python-bits : 64
OS : Linux
OS-release : 6.17.7-200.fc42.x86_64
Version : #1 SMP PREEMPT_DYNAMIC Sun Nov 2 17:43:34 UTC 2025
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.3.3
numpy : 2.3.5
pytz : 2025.2
dateutil : 2.9.0.post0
pip : None
Cython : 3.2.1
sphinx : None
IPython : 9.7.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None