Issue
I am unable to reproduce the .clip()
-example from the GeoPandas docs without error. I suspect it has something to do with my setup, since the same thing worked a few months ago in a different environment and I have not found reports of this happening to others. But I cannot figure out what the problem is--I am hoping someone here has an idea.
Copying and pasting the example code into my jupyter notebook looks something like this:
import geopandas
from shapely.geometry import Polygon
# get a set of points
capitals = geopandas.read_file(geopandas.datasets.get_path("naturalearth_cities"))
# Create a custom polygon
polygon = Polygon([(0, 0), (0, 90), (180, 90), (180, 0), (0, 0)])
# Attempt to clip points by polygon
capitals_clipped = capitals.clip(polygon)
Running it gives me the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 capitals_clipped = capitals.clip(polygon)
File ~/.conda/envs/test-env/lib/python3.8/site-packages/pandas/util/_decorators.py:311, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
305 if len(args) > num_allow_args:
306 warnings.warn(
307 msg.format(arguments=arguments),
308 FutureWarning,
309 stacklevel=stacklevel,
310 )
--> 311 return func(*args, **kwargs)
File ~/.conda/envs/test-env/lib/python3.8/site-packages/pandas/core/frame.py:10917, in DataFrame.clip(self, lower, upper, axis, inplace, *args, **kwargs)
10905 @deprecate_nonkeyword_arguments(
10906 version=None, allowed_args=["self", "lower", "upper"]
10907 )
(...)
10915 **kwargs,
10916 ) -> DataFrame | None:
> 10917 return super().clip(lower, upper, axis, inplace, *args, **kwargs)
File ~/.conda/envs/test-env/lib/python3.8/site-packages/pandas/core/generic.py:7569, in NDFrame.clip(self, lower, upper, axis, inplace, *args, **kwargs)
7567 result = self
7568 if lower is not None:
-> 7569 result = result._clip_with_one_bound(
7570 lower, method=self.ge, axis=axis, inplace=inplace
7571 )
7572 if upper is not None:
7573 if inplace:
File ~/.conda/envs/test-env/lib/python3.8/site-packages/pandas/core/generic.py:7417, in NDFrame._clip_with_one_bound(self, threshold, method, axis, inplace)
7414 else:
7415 threshold_inf = threshold
-> 7417 subset = method(threshold_inf, axis=axis) | isna(self)
7419 # GH 40420
7420 return self.where(subset, threshold, axis=axis, inplace=inplace)
File ~/.conda/envs/test-env/lib/python3.8/site-packages/pandas/core/ops/__init__.py:470, in flex_comp_method_FRAME.<locals>.f(self, other, axis, level)
466 axis = self._get_axis_number(axis) if axis is not None else 1
468 self, other = align_method_FRAME(self, other, axis, flex=True, level=level)
--> 470 new_data = self._dispatch_frame_op(other, op, axis=axis)
471 return self._construct_result(new_data)
File ~/.conda/envs/test-env/lib/python3.8/site-packages/pandas/core/frame.py:6973, in DataFrame._dispatch_frame_op(self, right, func, axis)
6970 if not is_list_like(right):
6971 # i.e. scalar, faster than checking np.ndim(right) == 0
6972 with np.errstate(all="ignore"):
-> 6973 bm = self._mgr.apply(array_op, right=right)
6974 return self._constructor(bm)
6976 elif isinstance(right, DataFrame):
File ~/.conda/envs/test-env/lib/python3.8/site-packages/pandas/core/internals/managers.py:302, in BaseBlockManager.apply(self, f, align_keys, ignore_failures, **kwargs)
300 try:
301 if callable(f):
--> 302 applied = b.apply(f, **kwargs)
303 else:
304 applied = getattr(b, f)(**kwargs)
File ~/.conda/envs/test-env/lib/python3.8/site-packages/pandas/core/internals/blocks.py:402, in Block.apply(self, func, **kwargs)
396 @final
397 def apply(self, func, **kwargs) -> list[Block]:
398 """
399 apply the function to my values; return a block if we are not
400 one
401 """
--> 402 result = func(self.values, **kwargs)
404 return self._split_op_result(result)
File ~/.conda/envs/test-env/lib/python3.8/site-packages/pandas/core/ops/array_ops.py:283, in comparison_op(left, right, op)
280 return invalid_comparison(lvalues, rvalues, op)
282 elif is_object_dtype(lvalues.dtype) or isinstance(rvalues, str):
--> 283 res_values = comp_method_OBJECT_ARRAY(op, lvalues, rvalues)
285 else:
286 res_values = _na_arithmetic_op(lvalues, rvalues, op, is_cmp=True)
File ~/.conda/envs/test-env/lib/python3.8/site-packages/pandas/core/ops/array_ops.py:73, in comp_method_OBJECT_ARRAY(op, x, y)
71 result = libops.vec_compare(x.ravel(), y.ravel(), op)
72 else:
---> 73 result = libops.scalar_compare(x.ravel(), y, op)
74 return result.reshape(x.shape)
File ~/.conda/envs/test-env/lib/python3.8/site-packages/pandas/_libs/ops.pyx:107, in pandas._libs.ops.scalar_compare()
TypeError: '>=' not supported between instances of 'str' and 'Polygon'
So far I have tried to repeat this in a clean conda environment obtained like so:
conda create -n test-env
conda activate test-env
conda install ipykernel geopandas
ipython kernel install --user --name=test_ipython
conda deactivate test-env
According to conda, running python 3.7.11 and geopandas 0.9.0. Even using this fresh test_ipython
-Kernel, I get the same TypeError
when I attempt clipping the stock world map.
I don't currently think this is a bug, and am assuming mere ignorance on my part.
Solution
This was a problem with geopandas versions.
It turns out that geopandas.GeoDataFrame.clip()
did not work the same way in v0.9.0. Checking the docs for the appropriate version of geopandas reveals that back then, clip was not a GeoDataFrame
-method but a standalone one, making the solution simply
capitals_clipped = geopandas.clip(capitals, polygon)
Answered By - maitagorri
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.