Skip to content

Deprecate use of chararray in io.fits #3862

Closed

Description

@embray

As raised in #3854 and earlier issues, Numpy is gradually trying to deprecate and eventually kill off the old Numarray chararray class. This is fine, but io.fits still uses it for FITS tables. This is especially important in supporting FITS ASCII tables, for its ability to automatically rstrip() individual strings returned from the array.

However, for FITS data we can just use our own class that has this capability (we don't need any of the other chararray methods). It might be nice to go ahead and use some version of my encoded_text_array class for FITS string columns (and possible string columns in other Astropy tables), and to add optional rstrip support to that class (it already has automatic rstripping, but that should probably be optional).

I think for backwards compat the new class should inherit chararray methods, but they would all raise deprecation warnings. Similarly isinstance(..., chararray) checks should raise a deprecation warning (that the class will no longer be a chararray subclass after long).

Activity

  1. added this to the v1.1.0 milestone on Jun 18, 2015
  2. self-assigned this
    on Jun 18, 2015
  3. astrofrog commented on Sep 29, 2015

    @astrofrog
    Member

    Don't think this will happen for 1.1 so removing milestone

  4. removed this from the v1.1.0 milestone on Sep 29, 2015
  5. pllim commented on Feb 14, 2024

    @pllim
    Member

    Hmm did numpy 2.0 already forced our hands in this issue (maybe #15363 ) or not?

  6. neutrinoceros commented on Feb 14, 2024

    @neutrinoceros
    Contributor

    @pllim I don't think so. In fact, numpy 2.0 has received some attention in that area. For instance:

    Besides, np.chararray is still very much used in io.fits, so when the time comes, I'm sure we'll know 😅

  7. mhvk commented on Feb 14, 2024

    @mhvk
    Contributor

    I think the idea is still that we'd slowly stop using np.char and instead directly use functions from the new np.strings. But there is no specific deprecation planned for 2.0 (and the ufuncs are now used inside chararray so we'll benefit from the speed-up).

  8. saimn commented on Jan 26, 2026

    @saimn
    Contributor

    Seems to be happening on Numpy side: #19216
    We need to find a solution since currently io.fits relies on chararray to automatically strip trailing spaces from string columns.

  9. mhvk commented on Jan 26, 2026

    @mhvk
    Contributor

    If we only want to keep the .rstrip(), it may be easiest to define our own ndarray subclass, overriding __getitem__ like done in chararray: https://github.com/numpy/numpy/blob/f590d096927d9165979d3a7fb3b72a07364344f2/numpy/_core/defchararray.py#L595-L599

    Maybe at least for a test? One could add something that calls other overridden methods but with a deprecation warning.

    The alternative would, I think, mean creating a fresh array in which strings are pre-stripped, i.e., a copy (possibly one could check whether it would make a difference first; not implausible that many FITS columns are fully filled, eg., because they just contain "Y" or "N").

  10. saimn commented on Jan 27, 2026

    @saimn
    Contributor

    If we only want to keep the .rstrip(), it may be easiest to define our own ndarray subclass, overriding getitem like done in chararray: https://github.com/numpy/numpy/blob/f590d096927d9165979d3a7fb3b72a07364344f2/numpy/_core/defchararray.py#L595-L599

    Yes I think we need to keep the rstrip behavior, and ideally support the char methods with a deprecation warning.

    The alternative would, I think, mean creating a fresh array in which strings are pre-stripped, i.e., a copy (possibly one could check whether it would make a difference first; not implausible that many FITS columns are fully filled, eg., because they just contain "Y" or "N").

    I was going to say that the first option is better but actually, as we already have a conversion mechanism (and cache) for some column types (unsigned integer, boolean), maybe it's not unreasonable to do the rstrip that way. In the end it's pretty much the same as what chararray does.

  11. mhvk commented on Jan 27, 2026

    @mhvk
    Contributor

    I was going to say that the first option is better but actually, as we already have a conversion mechanism (and cache) for some column types (unsigned integer, boolean), maybe it's not unreasonable to do the rstrip that way. In the end it's pretty much the same as what chararray does.

    True, though with chararray you only do it as needed, so the memory footprint doesn't double. But perhaps with a check of whether the .rstrip() is in fact needed it might be the simplest...

  12. pllim commented on Feb 2, 2026

    @pllim
    Member

    The deprecation warning is bubbling downstream in packages that pull in both numpy-dev and astropy-dev and turn unhandled warnings into errors.

    Do we have a patch that can be implemented relatively quickly? What is the way forward besides max-pinning numpy here? Worst case is we copy over all the deprecated chararray stuff from numpy but that is probably not elegant.

  13. saimn commented on Feb 4, 2026

    @saimn
    Contributor

    One problem is that we don't know yet if np.char.array is also going to be deprecated (in the same Numpy release). If this change lands in Numpy dev then the current PR is useless.
    Then there is the question of what to do long term: ndarray subclass implementing the on the fly rstrip, or a simple ndarray and rstrip is done with the conversion mechanism already present in io.fits. I have some local changes for the second approach, almost working, currently that would be my preference.
    This would be a breaking change, which we could do in 8.0, but would be nice to find a way to warn people that chararray method will disappear.
    So maybe something we could do now (and release in a 7.2.x version) would be to make a chararray subclass, suppressing the warning from Numpy, and raising a warning when chararray methods are called ?

  14. neutrinoceros commented on Feb 4, 2026

    @neutrinoceros
    Contributor

    If this change lands in Numpy dev then the current PR is useless.

    I disagree. The existing PR, if nothing else, would allow us to see any other failure modes currently hidden in devdeps jobs.

    So maybe something we could do now (and release in a 7.2.x version) would be to make a chararray subclass

    Something like #19241 ?

  15. saimn commented on Feb 4, 2026

    @saimn
    Contributor

    I was thinking something like #19267 to avoid duplicating the whole chararray class.

  16. saimn commented on Feb 4, 2026

    @saimn
    Contributor

    One issue is that we don't have much time to deprecate things on our side if we want to remove chararray in 8.0, but I don't know how long Numpy will keep the deprecated class (i.e. can we wait 9.0 ?).

  17. neutrinoceros commented on Feb 4, 2026

    @neutrinoceros
    Contributor

    deprecation cycles in numpy are usually about a year long, and often longer. There should be enough time to adjust.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions