Skip to content

setting column value with .loc based on a condition #28924

Open
@jGaboardi

Description

@jGaboardi

Code Sample, a copy-pastable example if possible

>>> import pandas as pd
>>> from shapely.geometry import LineString

>>> line_1 = LineString(((0,0), (1,1)))
>>> line_2 = LineString(((3,3), (5,5)))

>>> df = pd.DataFrame(data={"line_id":"999", "geometry":[line_1]})
>>> print("inital dataframe:\n%s\n" % df)

# Isolate column value
>>> condition = df["line_id"] == "999"
>>> print("location of condition:\n%s\n" % df.loc[condition, "geometry"])

# [Fail] Set geometry value by the location of condition
>>> try:
>>>     df.loc[condition,  "geometry"] = line_2
>>> except ValueError as e:
>>>     print(e, "\n")

# [Pass] Set geometry value by index location
>>> df.loc[0, "geometry"] = line_2
>>> print(df)

Problem description

Migrated from geopandas/geopandas#831.

The flexibility to set values based on a certain condition when index location is unknown is critical for some spatial workflows. This seems to be unexpected behavior in pandas (though I may be wrong). Also, as @martinfleis suggested, it may be related to np.array(LineString(((0,0), (1,1)))) returning an array of coordinates.

Expected Output

see problem description

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS

commit : None
python : 3.7.3.final.0
python-bits : 64
OS : Darwin
OS-release : 19.0.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.1
numpy : 1.17.2
pytz : 2019.3
dateutil : 2.8.0
pip : 19.2.3
setuptools : 41.4.0
Cython : 0.29.13
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.8.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugExtensionArrayExtending pandas with custom dtypes or arrays.IndexingRelated 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