-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
setting column value with .loc based on a condition #28924
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
Comments
cc @jorisvandenbossche might be geopandas related |
@jbrockmendel it was reported to geopandas but we found out that the issue is in pandas as seen above. So even though we might be able to do some kind of patch in geopandas, it does not resolve the issue. |
Looks like in pandas.core.internals.blocks.setitem on L841 we effectively do Don't these objects live inside of a geopandas ExtensionArray? I would expect that skirt this problem nicely. |
@jbrockmendel If it is in GeometryArray (ExtensionArray), then the issue is not present. As you say, the check needs to be smarter, because if you try to pass 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]})
multi = line_1.union(line_2)
df.loc[0, "geometry"] = multi Traceback:
|
Hi @martinfleis multi = line_1.union(line_2) |
@AliOmran19991 Probably not in pure pandas. There is a way in geopandas - you have to assign it to GeoSeries and then move it back to gdf. import geopandas as gpd
from shapely.geometry import LineString
line_1 = LineString(((0,0), (1,1)))
line_2 = LineString(((3,3), (5,5)))
df = gpd.GeoDataFrame(data={"line_id":"999", "geometry":[line_1]})
multi = line_1.union(line_2)
geom = df.geometry
geom.loc[0] = multi
df['geometry'] = geom |
The problem we are running into here is the general problem that comes up regularly: the dividing line between "scalar" objects and "lists/arrays of scalar objects" is not a clear line. And Pandas has especially problems with scalars that are iterable themselves (I think @jbrockmendel we also ran into this few months ago with one of your groupby refactoring PRs). That comes up for geopandas because certain geometries are iterable. It also comes up when eg storing lists or dicts in a column (which is currently not "really" supported, you can do it in an object dtype, but if we want to add a more proper list-type, we will also run into this). |
Code Sample, a copy-pastable example if possible
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 tonp.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
The text was updated successfully, but these errors were encountered: