Skip to content

BUG: Cannot use .loc to set a ndarray as the value of an empty dataframe #49972

@LouisLU9911

Description

@LouisLU9911

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
result = pd.DataFrame()
result.loc[0, 0] = np.asarray([0])     # This assignment statement will raise an IndexError

Issue Description

  • This error occurs when I want to upgrade my pandas==1.1.5 to a higher version, including the newest one(pandas==1.5.2).
  • Cannot use result.loc[0, 0] = np.asarray([0]) to set a ndarray as the value of an empty dataframe
  • I try to figure out what's wrong with it by myself, but I am not familiar with pandas's internal. Maybe BUG: Adding Series to empty DataFrame can reset dtype to float64 #42099 has changed the indexing behavior for a ndarray.
  • Finally, I am not sure whether it is a bug or not. 😂

Expected Behavior

  • The assignment statement will not raise an IndexError and set the value correctly and successfully.

Expected Output (When I use pandas==1.1.5):

...
>>> result.loc[0, 0] = np.asarray([0])
>>> print(result)
     0
0  0.0

Installed Versions

INSTALLED VERSIONS

commit : 8dab54d
python : 3.8.11.final.0
python-bits : 64
OS : Darwin
OS-release : 20.3.0
Version : Darwin Kernel Version 20.3.0: Thu Jan 21 00:07:06 PST 2021; root:xnu-7195.81.3~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.5.2
numpy : 1.23.5
pytz : 2022.6
dateutil : 2.8.2
setuptools : 57.0.0
pip : 21.1.3
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : 1.0.2
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : 0.8.10
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions