Skip to content

BUG: inconsistency between str.center and np.char.center #25659

Closed

Description

@ngoldbaum

Note that the same issue happens with ljust, rjust, and zfill or any other operation implemented using _vec_string.

The python docs for str.center say:

The original string is returned if width is less than or equal to len(s).

And the docs for np.char.center say:

Calls :meth:`str.center` element-wise.

but observe:

In [1]: np.char.center(["this is a string that's longer than the width", "a"], 10)
Out[1]: array(['this is a ', '    a     '], dtype='<U10')

In [2]: "this is a string that's longer than the width".center(10)
Out[2]: "this is a string that's longer than the width"

Now I can see why the _vec_string implementations (and probably future ufunc implementations from @lysnikolaou) are written this way. I'm perfectly happy to keep this difference compared with str.center, but we should intentionally choose that and document it in the affected functions. I'm happy to implement whichever behavior we decide on in stringdtype.

Activity

  1. seberg commented on Jan 25, 2024

    @seberg
    Member

    No strong opinion. The fix would also be trivial and just bloat memory a bit if it isn't a bug fix. Since the problem is probably just the returned string space needed is max(center, old_size) rather than just center.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions