Skip to content

Fix MLJSONEncoder for NumPy 2 and dates - #845

Open
kayaal34 wants to merge 1 commit into
mljar:masterfrom
kayaal34:fix-json-encoder
Open

kayaal34 wants to merge 1 commit into
mljar:masterfrom
kayaal34:fix-json-encoder

Conversation

@kayaal34

@kayaal34 kayaal34 commented Nov 6, 2026

Copy link
Copy Markdown

MLJSONEncoder had two bugs in supervised/utils/jsonencoder.py:

  • The float branch checked np.float_, which NumPy 2.0 removed. The package requires numpy>=2.0.0, so any value that got past the int branch (a numpy float, an ndarray, a date) raised AttributeError instead of being encoded.
  • The date branch used obj, which is not defined in default(self, o), so a date/datetime would have raised NameError.

This removes np.float_ (np.float64 already covers it) and uses o in the date branch. It also adds tests/tests_utils/test_jsonencoder.py (numpy ints and floats, an ndarray, date and datetime, and an unknown type that must still raise TypeError).

How I tested it

In a python:3.12-slim container with NumPy 2, I ran the new test file against the encoder module on its own (the rest of the package's heavy dependencies were not installed):

  • with this change: 5 passed;
  • with the encoder from master: 4 failed, all with AttributeError: `np.float_` was removed in the NumPy 2.0 release.

I did not run the full test suite.

I used Claude (an AI assistant) to help with this change; I reviewed it and ran the checks above.

np.float_ was removed in NumPy 2.0, so the float check raised
AttributeError for any value past the int branch. The date branch
used an undefined name (obj). Add tests for the encoder.
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.

1 participant