Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Inconsistent implicit conversion of 1-arrays inside data frames #57923

Open
2 of 3 tasks
erezinman opened this issue Mar 20, 2024 · 1 comment
Open
2 of 3 tasks
Assignees
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@erezinman
Copy link

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
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

INSTALLED VERSIONS

commit : fd3f571
python : 3.9.18.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-75-generic
Version : #82~20.04.1-Ubuntu SMP Wed Jun 7 19:37:37 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 2.2.0
numpy : 1.26.3
pytz : 2023.4
dateutil : 2.8.2
setuptools : 69.1.1
pip : 24.0
Cython : 3.0.8
pytest : 7.4.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.12.2
gcsfs : 2023.12.2post1
matplotlib : None
numba : None
numexpr : 2.9.0
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : 2.0.25
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : None
pyqt5 : None

@erezinman erezinman added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 20, 2024
@gb0808
Copy link

gb0808 commented Apr 6, 2024

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
2 participants