Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorisvandenbossche/61ba4b240a2060ec7dafdead84e13371 to your computer and use it in GitHub Desktop.
Save jorisvandenbossche/61ba4b240a2060ec7dafdead84e13371 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import geopandas"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'0.5.0+13.g8e7a3df'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"geopandas.__version__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# With master"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"df = geopandas.read_file(geopandas.datasets.get_path('nybb'))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>BoroCode</th>\n",
" <th>BoroName</th>\n",
" <th>Shape_Leng</th>\n",
" <th>Shape_Area</th>\n",
" <th>geometry</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5</td>\n",
" <td>Staten Island</td>\n",
" <td>330470.010332</td>\n",
" <td>1.623820e+09</td>\n",
" <td>(POLYGON ((970217.0223999023 145643.3322143555...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4</td>\n",
" <td>Queens</td>\n",
" <td>896344.047763</td>\n",
" <td>3.045213e+09</td>\n",
" <td>(POLYGON ((1029606.076599121 156073.8142089844...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>Brooklyn</td>\n",
" <td>741080.523166</td>\n",
" <td>1.937479e+09</td>\n",
" <td>(POLYGON ((1021176.479003906 151374.7969970703...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>Manhattan</td>\n",
" <td>359299.096471</td>\n",
" <td>6.364715e+08</td>\n",
" <td>(POLYGON ((981219.0557861328 188655.3157958984...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>Bronx</td>\n",
" <td>464392.991824</td>\n",
" <td>1.186925e+09</td>\n",
" <td>(POLYGON ((1012821.805786133 229228.2645874023...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" BoroCode BoroName Shape_Leng Shape_Area \\\n",
"0 5 Staten Island 330470.010332 1.623820e+09 \n",
"1 4 Queens 896344.047763 3.045213e+09 \n",
"2 3 Brooklyn 741080.523166 1.937479e+09 \n",
"3 1 Manhattan 359299.096471 6.364715e+08 \n",
"4 2 Bronx 464392.991824 1.186925e+09 \n",
"\n",
" geometry \n",
"0 (POLYGON ((970217.0223999023 145643.3322143555... \n",
"1 (POLYGON ((1029606.076599121 156073.8142089844... \n",
"2 (POLYGON ((1021176.479003906 151374.7969970703... \n",
"3 (POLYGON ((981219.0557861328 188655.3157958984... \n",
"4 (POLYGON ((1012821.805786133 229228.2645874023... "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"df['centroids'] = df.geometry.centroid"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>BoroCode</th>\n",
" <th>BoroName</th>\n",
" <th>Shape_Leng</th>\n",
" <th>Shape_Area</th>\n",
" <th>geometry</th>\n",
" <th>centroids</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5</td>\n",
" <td>Staten Island</td>\n",
" <td>330470.010332</td>\n",
" <td>1.623820e+09</td>\n",
" <td>(POLYGON ((970217.0223999023 145643.3322143555...</td>\n",
" <td>POINT (941639.4503875449 150931.991141128)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4</td>\n",
" <td>Queens</td>\n",
" <td>896344.047763</td>\n",
" <td>3.045213e+09</td>\n",
" <td>(POLYGON ((1029606.076599121 156073.8142089844...</td>\n",
" <td>POINT (1034578.078406461 197116.6042299137)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>Brooklyn</td>\n",
" <td>741080.523166</td>\n",
" <td>1.937479e+09</td>\n",
" <td>(POLYGON ((1021176.479003906 151374.7969970703...</td>\n",
" <td>POINT (998769.1146889532 174169.7607268662)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>Manhattan</td>\n",
" <td>359299.096471</td>\n",
" <td>6.364715e+08</td>\n",
" <td>(POLYGON ((981219.0557861328 188655.3157958984...</td>\n",
" <td>POINT (993336.9649384869 222451.4367245601)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>Bronx</td>\n",
" <td>464392.991824</td>\n",
" <td>1.186925e+09</td>\n",
" <td>(POLYGON ((1012821.805786133 229228.2645874023...</td>\n",
" <td>POINT (1021174.789767234 249937.9800696835)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" BoroCode BoroName Shape_Leng Shape_Area \\\n",
"0 5 Staten Island 330470.010332 1.623820e+09 \n",
"1 4 Queens 896344.047763 3.045213e+09 \n",
"2 3 Brooklyn 741080.523166 1.937479e+09 \n",
"3 1 Manhattan 359299.096471 6.364715e+08 \n",
"4 2 Bronx 464392.991824 1.186925e+09 \n",
"\n",
" geometry \\\n",
"0 (POLYGON ((970217.0223999023 145643.3322143555... \n",
"1 (POLYGON ((1029606.076599121 156073.8142089844... \n",
"2 (POLYGON ((1021176.479003906 151374.7969970703... \n",
"3 (POLYGON ((981219.0557861328 188655.3157958984... \n",
"4 (POLYGON ((1012821.805786133 229228.2645874023... \n",
"\n",
" centroids \n",
"0 POINT (941639.4503875449 150931.991141128) \n",
"1 POINT (1034578.078406461 197116.6042299137) \n",
"2 POINT (998769.1146889532 174169.7607268662) \n",
"3 POINT (993336.9649384869 222451.4367245601) \n",
"4 POINT (1021174.789767234 249937.9800696835) "
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Passed value is an existing column:**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"centroids\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>BoroCode</th>\n",
" <th>BoroName</th>\n",
" <th>Shape_Leng</th>\n",
" <th>Shape_Area</th>\n",
" <th>geometry</th>\n",
" <th>centroids</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5</td>\n",
" <td>Staten Island</td>\n",
" <td>330470.010332</td>\n",
" <td>1.623820e+09</td>\n",
" <td>(POLYGON ((970217.0223999023 145643.3322143555...</td>\n",
" <td>POINT (941639.4503875449 150931.991141128)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4</td>\n",
" <td>Queens</td>\n",
" <td>896344.047763</td>\n",
" <td>3.045213e+09</td>\n",
" <td>(POLYGON ((1029606.076599121 156073.8142089844...</td>\n",
" <td>POINT (1034578.078406461 197116.6042299137)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>Brooklyn</td>\n",
" <td>741080.523166</td>\n",
" <td>1.937479e+09</td>\n",
" <td>(POLYGON ((1021176.479003906 151374.7969970703...</td>\n",
" <td>POINT (998769.1146889532 174169.7607268662)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>Manhattan</td>\n",
" <td>359299.096471</td>\n",
" <td>6.364715e+08</td>\n",
" <td>(POLYGON ((981219.0557861328 188655.3157958984...</td>\n",
" <td>POINT (993336.9649384869 222451.4367245601)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>Bronx</td>\n",
" <td>464392.991824</td>\n",
" <td>1.186925e+09</td>\n",
" <td>(POLYGON ((1012821.805786133 229228.2645874023...</td>\n",
" <td>POINT (1021174.789767234 249937.9800696835)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" BoroCode BoroName Shape_Leng Shape_Area \\\n",
"0 5 Staten Island 330470.010332 1.623820e+09 \n",
"1 4 Queens 896344.047763 3.045213e+09 \n",
"2 3 Brooklyn 741080.523166 1.937479e+09 \n",
"3 1 Manhattan 359299.096471 6.364715e+08 \n",
"4 2 Bronx 464392.991824 1.186925e+09 \n",
"\n",
" geometry \\\n",
"0 (POLYGON ((970217.0223999023 145643.3322143555... \n",
"1 (POLYGON ((1029606.076599121 156073.8142089844... \n",
"2 (POLYGON ((1021176.479003906 151374.7969970703... \n",
"3 (POLYGON ((981219.0557861328 188655.3157958984... \n",
"4 (POLYGON ((1012821.805786133 229228.2645874023... \n",
"\n",
" centroids \n",
"0 POINT (941639.4503875449 150931.991141128) \n",
"1 POINT (1034578.078406461 197116.6042299137) \n",
"2 POINT (998769.1146889532 174169.7607268662) \n",
"3 POINT (993336.9649384869 222451.4367245601) \n",
"4 POINT (1021174.789767234 249937.9800696835) "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res = df.set_geometry('centroids')\n",
"print(res._geometry_column_name)\n",
"res"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Specifying `drop=True`:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"geometry\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>BoroCode</th>\n",
" <th>BoroName</th>\n",
" <th>Shape_Leng</th>\n",
" <th>Shape_Area</th>\n",
" <th>geometry</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5</td>\n",
" <td>Staten Island</td>\n",
" <td>330470.010332</td>\n",
" <td>1.623820e+09</td>\n",
" <td>POINT (941639.4503875449 150931.991141128)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4</td>\n",
" <td>Queens</td>\n",
" <td>896344.047763</td>\n",
" <td>3.045213e+09</td>\n",
" <td>POINT (1034578.078406461 197116.6042299137)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>Brooklyn</td>\n",
" <td>741080.523166</td>\n",
" <td>1.937479e+09</td>\n",
" <td>POINT (998769.1146889532 174169.7607268662)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>Manhattan</td>\n",
" <td>359299.096471</td>\n",
" <td>6.364715e+08</td>\n",
" <td>POINT (993336.9649384869 222451.4367245601)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>Bronx</td>\n",
" <td>464392.991824</td>\n",
" <td>1.186925e+09</td>\n",
" <td>POINT (1021174.789767234 249937.9800696835)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" BoroCode BoroName Shape_Leng Shape_Area \\\n",
"0 5 Staten Island 330470.010332 1.623820e+09 \n",
"1 4 Queens 896344.047763 3.045213e+09 \n",
"2 3 Brooklyn 741080.523166 1.937479e+09 \n",
"3 1 Manhattan 359299.096471 6.364715e+08 \n",
"4 2 Bronx 464392.991824 1.186925e+09 \n",
"\n",
" geometry \n",
"0 POINT (941639.4503875449 150931.991141128) \n",
"1 POINT (1034578.078406461 197116.6042299137) \n",
"2 POINT (998769.1146889532 174169.7607268662) \n",
"3 POINT (993336.9649384869 222451.4367245601) \n",
"4 POINT (1021174.789767234 249937.9800696835) "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res = df.set_geometry('centroids', drop=True)\n",
"print(res._geometry_column_name)\n",
"res"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"-> original geometry column is dropped, but also the centroids is renamed to geometry (or you could also say: the centroids column is dropped and the geometry column's values replaced with the centroids column)\n",
"\n",
"-> should we keep the 'centroids' name?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Passed values is a GeoSeries**:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"df = geopandas.read_file(geopandas.datasets.get_path('nybb'))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"centroids = df.geometry.centroid"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 POINT (941639.4503875449 150931.991141128)\n",
"1 POINT (1034578.078406461 197116.6042299137)\n",
"2 POINT (998769.1146889532 174169.7607268662)\n",
"3 POINT (993336.9649384869 222451.4367245601)\n",
"4 POINT (1021174.789767234 249937.9800696835)\n",
"dtype: object"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"centroids"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"GeoSeries without name (or similar if it was a list or array of values) -> replace existing geometries:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"geometry\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>BoroCode</th>\n",
" <th>BoroName</th>\n",
" <th>Shape_Leng</th>\n",
" <th>Shape_Area</th>\n",
" <th>geometry</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5</td>\n",
" <td>Staten Island</td>\n",
" <td>330470.010332</td>\n",
" <td>1.623820e+09</td>\n",
" <td>POINT (941639.4503875449 150931.991141128)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4</td>\n",
" <td>Queens</td>\n",
" <td>896344.047763</td>\n",
" <td>3.045213e+09</td>\n",
" <td>POINT (1034578.078406461 197116.6042299137)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>Brooklyn</td>\n",
" <td>741080.523166</td>\n",
" <td>1.937479e+09</td>\n",
" <td>POINT (998769.1146889532 174169.7607268662)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>Manhattan</td>\n",
" <td>359299.096471</td>\n",
" <td>6.364715e+08</td>\n",
" <td>POINT (993336.9649384869 222451.4367245601)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>Bronx</td>\n",
" <td>464392.991824</td>\n",
" <td>1.186925e+09</td>\n",
" <td>POINT (1021174.789767234 249937.9800696835)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" BoroCode BoroName Shape_Leng Shape_Area \\\n",
"0 5 Staten Island 330470.010332 1.623820e+09 \n",
"1 4 Queens 896344.047763 3.045213e+09 \n",
"2 3 Brooklyn 741080.523166 1.937479e+09 \n",
"3 1 Manhattan 359299.096471 6.364715e+08 \n",
"4 2 Bronx 464392.991824 1.186925e+09 \n",
"\n",
" geometry \n",
"0 POINT (941639.4503875449 150931.991141128) \n",
"1 POINT (1034578.078406461 197116.6042299137) \n",
"2 POINT (998769.1146889532 174169.7607268662) \n",
"3 POINT (993336.9649384869 222451.4367245601) \n",
"4 POINT (1021174.789767234 249937.9800696835) "
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res = df.set_geometry(centroids)\n",
"print(res._geometry_column_name)\n",
"res"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"geometry\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>BoroCode</th>\n",
" <th>BoroName</th>\n",
" <th>Shape_Leng</th>\n",
" <th>Shape_Area</th>\n",
" <th>geometry</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5</td>\n",
" <td>Staten Island</td>\n",
" <td>330470.010332</td>\n",
" <td>1.623820e+09</td>\n",
" <td>POINT (941639.4503875449 150931.991141128)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4</td>\n",
" <td>Queens</td>\n",
" <td>896344.047763</td>\n",
" <td>3.045213e+09</td>\n",
" <td>POINT (1034578.078406461 197116.6042299137)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>Brooklyn</td>\n",
" <td>741080.523166</td>\n",
" <td>1.937479e+09</td>\n",
" <td>POINT (998769.1146889532 174169.7607268662)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>Manhattan</td>\n",
" <td>359299.096471</td>\n",
" <td>6.364715e+08</td>\n",
" <td>POINT (993336.9649384869 222451.4367245601)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>Bronx</td>\n",
" <td>464392.991824</td>\n",
" <td>1.186925e+09</td>\n",
" <td>POINT (1021174.789767234 249937.9800696835)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" BoroCode BoroName Shape_Leng Shape_Area \\\n",
"0 5 Staten Island 330470.010332 1.623820e+09 \n",
"1 4 Queens 896344.047763 3.045213e+09 \n",
"2 3 Brooklyn 741080.523166 1.937479e+09 \n",
"3 1 Manhattan 359299.096471 6.364715e+08 \n",
"4 2 Bronx 464392.991824 1.186925e+09 \n",
"\n",
" geometry \n",
"0 POINT (941639.4503875449 150931.991141128) \n",
"1 POINT (1034578.078406461 197116.6042299137) \n",
"2 POINT (998769.1146889532 174169.7607268662) \n",
"3 POINT (993336.9649384869 222451.4367245601) \n",
"4 POINT (1021174.789767234 249937.9800696835) "
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res = df.set_geometry(centroids.values)\n",
"print(res._geometry_column_name)\n",
"res"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For this case, specifying `drop=True` has no effect."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"GeoSeries with a name:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"centroids = df.geometry.centroid.rename('centroids')"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 POINT (941639.4503875449 150931.991141128)\n",
"1 POINT (1034578.078406461 197116.6042299137)\n",
"2 POINT (998769.1146889532 174169.7607268662)\n",
"3 POINT (993336.9649384869 222451.4367245601)\n",
"4 POINT (1021174.789767234 249937.9800696835)\n",
"Name: centroids, dtype: object"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"centroids"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"geometry\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>BoroCode</th>\n",
" <th>BoroName</th>\n",
" <th>Shape_Leng</th>\n",
" <th>Shape_Area</th>\n",
" <th>geometry</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5</td>\n",
" <td>Staten Island</td>\n",
" <td>330470.010332</td>\n",
" <td>1.623820e+09</td>\n",
" <td>POINT (941639.4503875449 150931.991141128)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4</td>\n",
" <td>Queens</td>\n",
" <td>896344.047763</td>\n",
" <td>3.045213e+09</td>\n",
" <td>POINT (1034578.078406461 197116.6042299137)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>Brooklyn</td>\n",
" <td>741080.523166</td>\n",
" <td>1.937479e+09</td>\n",
" <td>POINT (998769.1146889532 174169.7607268662)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>Manhattan</td>\n",
" <td>359299.096471</td>\n",
" <td>6.364715e+08</td>\n",
" <td>POINT (993336.9649384869 222451.4367245601)</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>2</td>\n",
" <td>Bronx</td>\n",
" <td>464392.991824</td>\n",
" <td>1.186925e+09</td>\n",
" <td>POINT (1021174.789767234 249937.9800696835)</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" BoroCode BoroName Shape_Leng Shape_Area \\\n",
"0 5 Staten Island 330470.010332 1.623820e+09 \n",
"1 4 Queens 896344.047763 3.045213e+09 \n",
"2 3 Brooklyn 741080.523166 1.937479e+09 \n",
"3 1 Manhattan 359299.096471 6.364715e+08 \n",
"4 2 Bronx 464392.991824 1.186925e+09 \n",
"\n",
" geometry \n",
"0 POINT (941639.4503875449 150931.991141128) \n",
"1 POINT (1034578.078406461 197116.6042299137) \n",
"2 POINT (998769.1146889532 174169.7607268662) \n",
"3 POINT (993336.9649384869 222451.4367245601) \n",
"4 POINT (1021174.789767234 249937.9800696835) "
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res = df.set_geometry(centroids)\n",
"print(res._geometry_column_name)\n",
"res"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"-> this ignores the name of the Series -> is this a bug? \n",
"(it's what I am fixing in the PR)\n",
"\n",
"In the PR this now gives a GeoDataFrame with a 'centroids' column, but also dropping the original geometry column."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python (dev)",
"language": "python",
"name": "dev"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment