Skip to content

fix: replace deprecated np.char.chararray with np.full - #1211

Open
xyf5432 wants to merge 1 commit into
ghostmkg:mainfrom
xyf5432:fix/chararray-deprecation
Open

fix: replace deprecated np.char.chararray with np.full#1211
xyf5432 wants to merge 1 commit into
ghostmkg:mainfrom
xyf5432:fix/chararray-deprecation

Conversation

@xyf5432

@xyf5432 xyf5432 commented Aug 15, 2026

Copy link
Copy Markdown

Fixes #1210

Problem

np.char.chararray((n, n)) is deprecated in numpy 2.5.0 and emits a DeprecationWarning on every call — the constructor runs on each invocation of do_something, producing repeated warning noise.

Fix

Use a regular numpy array filled with the initial value instead:

- grid = np.char.chararray((n, n))
- for i in range(n):
-     for j in range(n):
-         grid[i][j] = "*"
+ grid = np.full((n, n), "*", dtype="<U1")

np.full creates an identical 2D string array in one call and works on all numpy versions.

Verification

  • Verified the replacement in isolation: same 2D shape, same filled content, and single-character element assignment (#, -) works identically.
  • One intentional, cosmetic difference: the old chararray stored bytes (b'*'), the new array stores unicode ('*') — the display loop now prints * instead of b'*'. Grid values are never compared programmatically, so no logic depends on this.
  • No deprecated API access remains.

np.char.chararray is deprecated in numpy 2.5.0 and emits a
DeprecationWarning on every call. Replace the construction plus fill
loop with np.full, which creates the same 2D single-character grid in
one call and works on all numpy versions.

The grid now stores unicode characters instead of bytes, so the
display loop prints '*' instead of b'*' — no logic depends on the
grid values.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

np.char.chararray constructor deprecated in numpy >= 2.5.0

1 participant