-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: Pandas squashes 1-dimensional Numpy array with shape (1,) down to a 0-dimensional array #59249
Comments
can I take this issue ? |
take |
@sunghjung3 I think it seems to be a regression in recent versions of pandas. It appears that a 1-dimensional numpy array with a single entry is being automatically converted to a 0-dimensional array when stored in a DataFrame.
Hope this helps, |
@Siddharth-Latthe-07 I've already tried this, and it doesn't work. It still prints |
oh, I see then can we create a custom function to ensure that the shape of the numpy array is preserved when you store it in the DataFrame. This function will explicitly check the shape of the array and reshape it if necessary before storing it in the DataFrame. |
something like this:-
|
@Siddharth-Latthe-07 I don't get the logic of your function. I'm assuming that we would call this function like And the result is still the same; it prints Also, even if I change this if-statement to |
Result of a git-bisect:
PR: #52906 cc @mroeschke |
@rhshadrach Has this been fixed? |
No - and it's not clear what the resolution will be. |
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
Issue Description
When a 1-dimensional Numpy array that only has 1 entry is stored to a DataFrame, Pandas seems to automatically make it a 0-dimensional array.
This is extremely inconvenient when the entries of both
df.loc[0, "x"]
anddf.loc[1, "x"]
are accessed later likedf.loc[1, "x"][i]
just for it to cause Numpy to raise an IndexError becausedf.loc[1, "x"]
is now a 0-dimensional array, not a 1-dimensional array.NOTE: This was not an issue in Pandas 2.0.0, but it is in version 2.2.2. I am not sure exactly which version in between this was introduced.
FYI: If a higher dimensional array with 1 entry is stored (e.g.
np.array([[1.]])
), then the shape is correctly preserved. It is only an issue when the array is 1-dimensional.Expected Behavior
print(df.loc[1, "x"].shape)
should print(1,)
.Installed Versions
INSTALLED VERSIONS
commit : d9cdd2e
python : 3.11.9.final.0
python-bits : 64
OS : Linux
OS-release : 4.18.0-477.15.1.el8_8.x86_64
Version : #1 SMP Wed Jun 28 15:04:18 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.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0
setuptools : 70.3.0
pip : 24.0
Cython : None
pytest : 8.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.4
IPython : 8.26.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.6.1
gcsfs : None
matplotlib : 3.9.1
numba : 0.60.0
numexpr : 2.10.0
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.0
sqlalchemy : None
tables : 3.9.2
tabulate : None
xarray : None
xlrd : None
zstandard : 0.22.0
tzdata : 2024.1
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: