Skip to content

Cannot append Pandas dataframe with string-type columns #1297

Description

@holgerpieta

In recent asammdf / numpy, trying to append a Pandas dataframe with string-type columns fails with the following exception:

  File "**************.py", line 283, in main
    mdf.append(data)
    ~~~~~~~~~~^^^^^^
  File "**************\.venv\Lib\site-packages\asammdf\mdf.py", line 978, in append
    return self._mdf.append(
           ~~~~~~~~~~~~~~~~^
        signals,
        ^^^^^^^^
    ...<4 lines>...
        units=units,
        ^^^^^^^^^^^^
    )
    ^
  File "**************\.venv\Lib\site-packages\asammdf\blocks\mdf_v4.py", line 2879, in append
    self._append_dataframe(
    ~~~~~~~~~~~~~~~~~~~~~~^
        signals,
        ^^^^^^^^
    ...<3 lines>...
        units=units,
        ^^^^^^^^^^^^
    )
    ^
  File "**************\.venv\Lib\site-packages\asammdf\blocks\mdf_v4.py", line 5065, in _append_dataframe
    array = encode(sig.array.astype(str), "utf-8")
  File "**************\.venv\Lib\site-packages\numpy\_core\strings.py", line 626, in encode
    _vec_string(a, np.object_, 'encode', _clean_args(encoding, errors)),
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: string operation on non-string array

This worked in previous versions, e.g. 8.2.9, though I don't know if the critical change was somewhere in asammdf or in numpy.

After some research I fixed the problem by changing line 5065 (in version 8.8.16) of blocks/mdf_v4.py from
array = encode(sig.array.astype(str), "utf-8")
to
array = encode(sig.to_numpy(dtype=str), "utf-8")

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions