Skip to content

Commit e85cf0d

Browse files
authored
Merge pull request #26 from DahnJ/fix/dj-k-ring-smoothing
Fix: k-ring smoothing does not fail if geometry is present + switch to ruff
2 parents aa4ebbe + 4af778a commit e85cf0d

File tree

4 files changed

+90
-93
lines changed

4 files changed

+90
-93
lines changed

.pre-commit-config.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
repos:
22
- repo: https://github.com/psf/black
3-
rev: 23.1.0
3+
rev: 23.10.0
44
hooks:
55
- id: black
66
language_version: python3
77

8-
- repo: https://github.com/PyCQA/flake8
9-
rev: 3.9.2
8+
- repo: https://github.com/astral-sh/ruff-pre-commit
9+
rev: v0.1.5
1010
hooks:
11-
- id: flake8
12-
language_version: python3
11+
- id: ruff
1312

1413
- repo: https://github.com/codespell-project/codespell
15-
rev: v1.16.0
14+
rev: v2.2.6
1615
hooks:
1716
- id: codespell
17+
args: ['--config', '.codespellrc']
1818
language_version: python3

h3pandas/h3pandas.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,9 @@ def k_ring_smoothing(
660660
881e30973bfffff 0.714286
661661
881e30973dfffff 0.714286
662662
"""
663+
# Drop geometry if present
664+
df = self._df.drop(columns=["geometry"], errors="ignore")
665+
663666
if sum([weights is None, k is None]) != 1:
664667
raise ValueError("Exactly one of `k` and `weights` must be set.")
665668

@@ -671,7 +674,7 @@ def k_ring_smoothing(
671674
# Unweighted case
672675
if weights is None:
673676
result = pd.DataFrame(
674-
self._df.h3.k_ring(k, explode=True)
677+
df.h3.k_ring(k, explode=True)
675678
.groupby("h3_k_ring")
676679
.sum()
677680
.divide((1 + 3 * k * (k + 1)))
@@ -695,10 +698,7 @@ def weighted_hex_ring(df, k, normalized_weight):
695698

696699
result = (
697700
pd.concat(
698-
[
699-
weighted_hex_ring(self._df, i, weights[i])
700-
for i in range(len(weights))
701-
]
701+
[weighted_hex_ring(df, i, weights[i]) for i in range(len(weights))]
702702
)
703703
.groupby("h3_hex_ring")
704704
.sum()

notebook/01-unified-data-layers.ipynb

+72-81
Large diffs are not rendered by default.

tests/test_h3pandas.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from h3pandas import h3pandas # noqa: F401
22
from h3 import h3
33
import pytest
4-
from shapely.geometry import Polygon, box
4+
from shapely.geometry import Polygon, box, Point
55
import pandas as pd
66
import geopandas as gpd
77
from geopandas.testing import assert_geodataframe_equal
@@ -519,6 +519,12 @@ def test_h3_k_ring_smoothing_1_ring_weighted(self, h3_dataframe_with_values):
519519
result = set(data.h3.k_ring_smoothing(weights=[2, 1])["val"])
520520
assert expected == result
521521

522+
def test_does_not_fail_if_geometry_present(self, h3_geodataframe_with_values):
523+
h3_geodataframe_with_values["geometry"] = [Point(0, 0)] * len(
524+
h3_geodataframe_with_values
525+
)
526+
h3_geodataframe_with_values.h3.k_ring_smoothing(1)
527+
522528

523529
class TestPolyfillResample:
524530
def test_polyfill_resample(self, h3_geodataframe_with_values):

0 commit comments

Comments
 (0)