Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] 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
import numpy as np
df = pd.DataFrame(
[None, None], dtype=object
)
df.at[0, 0] = np.arange(2)
df.at[1, 0] = np.arange(1)
print(df)
# 0
# 0 [0, 1]
# 1 0
Issue Description
There's an inconsistent handling of 1D arrays in pandas: arrays of length 1 are converted to scalars while arrays of other shapes aren't. To make things worse, pandas can be tricked into holding 1-arrays using:
df.applymap(lambda x: x.reshape(-1))
print(df)
# 0
# 0 [0, 1]
# 1 [0]
The cause of this error in the code is in the file core/internals/base.py (which doesn't exist in the main branch):
def setitem_inplace(self, indexer, value, warn: bool = True) -> None:
# ....
if isinstance(value, np.ndarray) and value.ndim == 1 and len(value) == 1:
# NumPy 1.25 deprecation: https://github.com/numpy/numpy/pull/10615
value = value[0, ...]
This comment does appear in a few places in the master branch so it should happen there as well.
A possible fix is to check the case when the dtype of the block is object. It is logical that in such cases, implicit conversions should be avoided.
Expected Behavior
Setting the values should be consistent.
Installed Versions
Comment From: gb0808
take