Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numpy array raises error when used in create_junctions function specifically in geodata attribute #694

Open
oakca opened this issue Feb 24, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@oakca
Copy link

oakca commented Feb 24, 2025

Describe the bug
When a numpy array of (x, y) tuples are used as geodata in create_junctions() function, an error is raised. When converted the same array to a list then it works.

here is the nodes file: https://congasnet-my.sharepoint.com/:u:/g/personal/okan_akca_congas_net/ET8b6UY-W9pIurtbPBMkT94BLqnS27GoyyHx15GbflWqsQ?e=qTBPBt

To Reproduce

import geopandas as gpd
import pandapipes as pp

n_ = gpd.read_file('nodes.geojson', driver='GeoJSON').set_index('id')

net = pp.create_empty_network(fluid='hydrogen')
nodes = pp.create_junctions(
        net,
        nr_junctions=len(n_),
        pn_bar=50,
        tfluid_k=283.15,
        height_m=n_['height'].values,
        name=n_['name'].values,
        index=n_.index,
        geodata=n_['geometry'].map(lambda p: (p.x, p.y)).values)

Error message

Traceback (most recent call last):
  File "C:\.venv\Lib\site-packages\IPython\core\interactiveshell.py", line 3577, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-18-cb2062836528>", line 3, in <module>
    nodes = pp.create_junctions(
            ^^^^^^^^^^^^^^^^^^^^
  File "C:\.venv\Lib\site-packages\pandapipes\create.py", line 1135, in create_junctions
    net.junction_geodata.loc[index, ["x", "y"]] = geodata
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "C:\.venv\Lib\site-packages\pandas\core\indexing.py", line 885, in __setitem__
    iloc._setitem_with_indexer(indexer, value, self.name)
  File "C:\.venv\Lib\site-packages\pandas\core\indexing.py", line 1893, in _setitem_with_indexer
    self._setitem_with_indexer_split_path(indexer, value, name)
  File "C:\.venv\Lib\site-packages\pandas\core\indexing.py", line 1978, in _setitem_with_indexer_split_path
    raise ValueError(
ValueError: Must have equal len keys and value when setting with an iterable

Expected behavior
it works when numpy array changed to list via n_['geometry'].map(lambda p: (p.x, p.y)).values.tolist(), it should also work with numpy array since the other attributes raise no errors.

Python environment (please complete the following information):

  • OS: Windows 11
  • pandapipes 0.11.0
  • geopandas
@oakca oakca added the bug Something isn't working label Feb 24, 2025
@dad616610
Copy link

I believe this is not a bug with pandapipes, but rather a shape mismatch in the input data.. Your data is not in the form of

(x, y) tuples

- its shape is (48, ), whereas pandapipes expects (48, 2)

>>> n_["geometry"].shape
(48,)

Your solution with .tolist() works because it explicitly converts each Point into a tuple of two values, resulting in a (48, 2) shape:

>>> np.array(n_["geometry"].map(lambda p: (p.x, p.y)).values.tolist()).shape
(48, 2)

However, a more direct approach is to use .get_coordinates(), which is built into geopandas:

>>> n_["geometry"].get_coordinates().shape
(48, 2)

See the GeoSeries.get_coordinates() documentation for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants