Repository: astropy/astropy

Issue:
# Fix getdata()'s lower and upper keywords.

Fix #4105

<!-- These comments are hidden when you submit the pull request,
so you do not need to remove them! -->

<!-- Please be sure to check out our contributing guidelines,
https://github.com/astropy/astropy/blob/main/CONTRIBUTING.md .
Please be sure to check out our code of conduct,
https://github.com/astropy/astropy/blob/main/CODE_OF_CONDUCT.md . -->

<!-- If you are new or need to be re-acquainted with Astropy
contributing workflow, please see
https://docs.astropy.org/en/latest/development/quickstart.html .
There is even a practical example at
https://docs.astropy.org/en/latest/development/git_edit_workflow_examples.html . -->

<!-- Please just have a quick search on GitHub to see if a similar
pull request has already been posted.
We have old closed pull requests that might provide useful code or ideas
that directly tie in with your pull request. -->

<!-- We have several automatic features that run when a pull request is open.
They can appear daunting but do not worry because maintainers will help
you navigate them, if necessary. -->

### Description
<!-- Provide a general description of what your pull request does.
Complete the following sentence and add relevant details as you see fit. -->

<!-- In addition please ensure that the pull request title is descriptive
and allows maintainers to infer the applicable subpackage(s). -->

<!-- READ THIS FOR MANUAL BACKPORT FROM A MAINTAINER:
Apply "skip-basebranch-check" label **before** you open the PR! -->

This pull request is to address ...

<!-- If the pull request closes any open issues you can add this.
If you replace <Issue Number> with a number, GitHub will automatically link it.
If this pull request is unrelated to any issues, please remove
the following line. -->

Fixes #<Issue Number>

<!-- Optional opt-out -->

- [ ] By checking this box, the PR author has requested that maintainers do **NOT** use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.


Implementation patch:
diff --git a/astropy/io/fits/convenience.py b/astropy/io/fits/convenience.py
index 3253ba9caa2f..83b64816071d 100644
--- a/astropy/io/fits/convenience.py
+++ b/astropy/io/fits/convenience.py
@@ -251,7 +251,9 @@ def getdata(filename, *args, header=None, lower=None, upper=None, view=None, **k
         if data.dtype.descr[0][0] == "":
             # this data does not have fields
             return
-        data.dtype.names = [trans(n) for n in data.dtype.names]
+
+        for n in data.dtype.names:
+            data.columns.change_name(n, trans(n))
 
     # allow different views into the underlying ndarray.  Keep the original
     # view just in case there is a problem


Test patch:
diff --git a/astropy/io/fits/tests/test_convenience.py b/astropy/io/fits/tests/test_convenience.py
index 14c021dadea2..a699f0b30534 100644
--- a/astropy/io/fits/tests/test_convenience.py
+++ b/astropy/io/fits/tests/test_convenience.py
@@ -513,3 +513,22 @@ def test_getdata_ext_not_given_nodata_noext(self):
             IndexError, match="No data in Primary HDU and no extension HDU found."
         ):
             fits.getdata(buf)
+
+    def test_getdata_lower_upper(self, tmp_path):
+        t = Table([np.zeros(5), np.ones(5), np.arange(5)], names=["a", "b", "c"])
+        t.write(tmp_path / "lower.fits")
+        t = Table([np.zeros(5), np.ones(5), np.arange(5)], names=["A", "B", "C"])
+        t.write(tmp_path / "upper.fits")
+
+        ref = np.zeros(5)
+        lowup = fits.getdata(tmp_path / "lower.fits", 1, upper=True)
+        assert lowup.dtype.names == ("A", "B", "C")
+        assert_array_equal(lowup["A"], ref)
+        assert_array_equal(lowup["a"], ref)
+        assert_array_equal(lowup.A, ref)
+
+        uplow = fits.getdata(tmp_path / "upper.fits", 1, lower=True)
+        assert uplow.dtype.names == ("a", "b", "c")
+        assert_array_equal(uplow["A"], ref)
+        assert_array_equal(uplow["a"], ref)
+        assert_array_equal(uplow.a, ref)