Skip to content

Pandas 3.0: TypeError: unhashable type 'StringArray' in parameter_node_at_instant.py #429

@MaxGhenis

Description

@MaxGhenis

Bug Description

When using pandas 3.0, StringArray objects can be passed to ParameterNodeAtInstant.__getitem__, but StringArray is not hashable and can't be used as a dictionary key.

Error

TypeError: unhashable type: 'StringArray'

at policyengine_core/parameters/parameter_node_at_instant.py:61:

return self._children[key]

Root Cause

In pandas 3.0, string columns use StringDtype by default, producing StringArray objects instead of numpy object arrays. The check on line 57 only handles numpy.ndarray:

if isinstance(key, numpy.ndarray):
    return parameters.VectorialParameterNodeAtInstant.build_from_node(self)[key]
return self._children[key]  # Fails if key is StringArray

Proposed Fix

Also check for pandas ExtensionArrays or convert StringArray to numpy array before using as dict key:

import pandas as pd

if isinstance(key, (numpy.ndarray, pd.api.extensions.ExtensionArray)):
    return parameters.VectorialParameterNodeAtInstant.build_from_node(self)[key]
return self._children[key]

Related

This is separate from #427 which fixed enum encoding with StringDtype Series.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions