File: test_pandas_accessor.py

package info (click to toggle)
python-geopandas 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,752 kB
  • sloc: python: 26,021; makefile: 147; sh: 25
file content (73 lines) | stat: -rw-r--r-- 1,688 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Ensure geopandas methods can be accessed via pandas 'geo' accessor."""

import re

import pandas as pd

from shapely.geometry import Point

# Use this import to register the "geo" accessor.
import geopandas.accessors  # noqa: F401 # pylint: disable=unused-import
from geopandas.array import GeometryDtype

import pandas.testing
import pytest


@pytest.fixture
def s():
    return pd.Series(
        [Point(x, y) for x, y in zip(range(3), range(3))], dtype=GeometryDtype()
    )


@pytest.fixture
def s2():
    return pd.Series(
        [Point(x, y + 1) for x, y in zip(range(3), range(3))], dtype=GeometryDtype()
    )


def test_series_geo_buffer(s):
    """Ensure returned geometry values have the expected dtype."""
    got = s.geo.buffer(0.2)
    assert isinstance(got.dtype, GeometryDtype)

    # Double-check that the .geo accessor works on the result.
    radius = got.geo.minimum_bounding_radius()
    for row in radius.index:
        assert radius[row] >= 0.1999  # Allow for some rounding error.


def test_series_geo_distance(s, s2):
    got = s.geo.distance(s2)
    pandas.testing.assert_series_equal(
        got,
        pd.Series([1.0, 1.0, 1.0]),
    )


def test_series_geo_x(s):
    x = s.geo.x
    pandas.testing.assert_series_equal(
        x,
        pd.Series([0.0, 1.0, 2.0]),
    )


def test_series_geo_x_attributeerror_for_not_geo_dtype():
    s = pd.Series([1, 2, 3])

    with pytest.raises(
        AttributeError,
        match=re.escape("Can only use .geo accessor with GeometryDtype values"),
    ):
        s.geo.x


def test_series_geo_y(s):
    y = s.geo.y
    pandas.testing.assert_series_equal(
        y,
        pd.Series([0.0, 1.0, 2.0]),
    )