{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using GeoPandas via the pandas \"geo\" accessor\n", "\n", "GeoPandas can be used from pandas with the \"geo\" accessor, which is registered on `pandas.Series` when `geopandas.accessors` is first imported." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Import geopandas.accessors to register the \"geo\" accessor.\n", "import geopandas.accessors # noqa # pylint: disable=unused-import\n", "import pandas as pd\n", "from shapely import Point" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the Series.geo accessor\n", "\n", "Construct a Series using the GeometryDtype." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 POINT (1 2)\n", "1 POINT (3 4)\n", "2 POINT (5 6)\n", "dtype: geometry" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = pd.Series(\n", " [Point(1, 2), Point(3, 4), Point(5, 6)],\n", " dtype='geometry',\n", ")\n", "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All GeoSeries methods and properties are available via the \"geo\" accessor." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 1.0\n", "1 3.0\n", "2 5.0\n", "dtype: float64" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.geo.x" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 4.472136\n", "1 10.000000\n", "2 15.620499\n", "dtype: float64" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2 = pd.Series(\n", " [Point(-1, -2), Point(-3, -4), Point(-5, -6)],\n", " dtype='geometry',\n", ")\n", "s.geo.distance(s2)" ] } ], "metadata": { "kernelspec": { "display_name": "dev-3.11", "language": "python", "name": "python3" }, "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.11.2" } }, "nbformat": 4, "nbformat_minor": 2 }