If I select a row with .loc from a GeoDataFrame, I get out a Series instead of a GeoSeries. Is this an intended behavior?
You will only get a GeoSeries if all values in the selected row or column are shapely geometric objects, from the docs here: http://geopandas.org/data_structures.html
So I think it is intended.
The code snippet below shows this behaviour:
import pandas as pd
import geopandas as gpd
df = pd.DataFrame(
{'City': ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas'],
'Country': ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela'],
'Latitude': [-34.58, -15.78, -33.45, 4.60, 10.48],
'Longitude': [-58.66, -47.91, -70.66, -74.08, -66.86]})
gdf = gpd.GeoDataFrame(
df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude))
# row is pd.Series
print(type(gdf.loc[0]))
# column is geopandas.Series if all values are Points, Lines or Polygons (or Multi-*)
print(type(gdf.loc[:,'City']))
print(type(gdf.loc[:,'geometry']))
As @Rabscuttler nicely explained, GeoSeries is "a vector where each entry in the vector is a set of shapes corresponding to one observation", so this is indeed intended behaviour. I will close this issue now, if we want to discuss it more we can always reopen.
... just in case someone stumbles upon this issue while searching for an efficient way to get a single-row geodataframe from an existing geodataframe (as i just did)... this does the trick:
df1 = df.iloc[1]
>>> a pandas Series of the first row
df1 = df.iloc[[1]]
>>> a GeoDataframe with only the first row
Most helpful comment
... just in case someone stumbles upon this issue while searching for an efficient way to get a single-row geodataframe from an existing geodataframe (as i just did)... this does the trick: