Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recfunctions.append_fields fails on arrays containing objects (Trac #1751) #2346

Closed
numpy-gitbot opened this issue Oct 19, 2012 · 4 comments
Closed

Comments

@numpy-gitbot
Copy link

Original ticket http://projects.scipy.org/numpy/ticket/1751 on 2011-02-28 by trac user aickley, assigned to unknown.

The problem is that append_fields calls array.view() which does not accept arrays containing objects (See [http://projects.scipy.org/numpy/ticket/674])

Test case (to include in numpy/lib/tests/recfunctions.py):

class TestAppendFieldsObj(TestCase):
    """
    Test append_fields with arrays containing objects
    """
    def setUp(self):
        from datetime import date
        self.data = dict(obj=date(2000, 1, 1))

    def test_append_to_objects(self):
        "Test append_fields when the base array contains objects"
        obj = self.data['obj']
        x = np.array([(obj, 1.), (obj, 2.)], dtype=[('A', object), ('B', float)])
        y = np.array([10, 20], dtype=int)
        test = append_fields(x, 'C', data=y, usemask=False)
        control = np.array([(obj, 1.0, 10), (obj, 2.0, 20)],
                           dtype=[('A', object), ('B', float), ('C', int)])
        assert_equal(test, control)
    #
    def test_append_with_objects(self):
        "Test append_fields when the appended data contains objects"
        obj = self.data['obj']
        x = np.array([(10, 1.), (20, 2.)], dtype=[('A', int), ('B', float)])
        y = np.array([obj, obj], dtype=object)
        test = append_fields(y, 'C', data=y, dtypes=object, usemask=False)
        control = np.array([(10, 1.0, obj), (20, 2.0, obj)],
                           dtype=[('A', int), ('B', float), ('C', object)])
        assert_equal(test, control)






======================================================================
ERROR: Test append_fields when the base array contains objects
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/kolpakov/lib/python2.6/site-packages/numpy/lib/tests/test_recfunctions.py", line 406, in test_append_to_objects
    test = append_fields(x, 'C', data=y, usemask=False)
  File "/home/kolpakov/lib/python2.6/site-packages/numpy/lib/recfunctions.py", line 629, in append_fields
    base = merge_arrays(base, usemask=usemask, fill_value=fill_value)
  File "/home/kolpakov/lib/python2.6/site-packages/numpy/lib/recfunctions.py", line 399, in merge_arrays
    return seqarrays.view(dtype=seqdtype, type=seqtype)
TypeError: Cannot change data-type for object array.

======================================================================
ERROR: Test append_fields when the appended data contains objects
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/kolpakov/lib/python2.6/site-packages/numpy/lib/tests/test_recfunctions.py", line 416, in test_append_with_objects
    test = append_fields(y, 'C', data=y, dtypes=object, usemask=False)
  File "/home/kolpakov/lib/python2.6/site-packages/numpy/lib/recfunctions.py", line 627, in append_fields
    for (a, n, d) in zip(data, names, dtypes)]
TypeError: Cannot change data-type for object array.

----------------------------------------------------------------------
Ran 33 tests in 0.062s

Regarding the first failure, append_fields calls merge_arrays on the base array:

base = merge_arrays(base, usemask=usemask, fill_value=fill_value)

Actually this line can be completely removed without breaking existing unit tests. I don't like this call because if we are calling merge_array on a single array we are are not actually merging anything, but rather making sure that the array conforms to certain criteria. Maybe this code should be refactored out from merge_arrays...

@numpy-gitbot
Copy link
Author

@rgommers wrote on 2011-03-02

ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 4, 2015
Previously views of structured arrays containing objects were completely
disabled.  This commit adds more lenient check for whether an object-array
view is allowed, and adds similar checks to getfield/setfield

Fixes numpy#2346. Fixes numpy#3256. Fixes numpy#2599. Fixes numpy#3253. Fixes numpy#3286.
Fixes numpy#5762.
ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 4, 2015
Previously views of structured arrays containing objects were completely
disabled.  This commit adds more lenient check for whether an object-array
view is allowed, and adds similar checks to getfield/setfield

Fixes numpy#2346. Fixes numpy#3256. Fixes numpy#2599. Fixes numpy#3253. Fixes numpy#3286.
Fixes numpy#5762.
ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 4, 2015
Previously views of structured arrays containing objects were completely
disabled.  This commit adds more lenient check for whether an object-array
view is allowed, and adds similar checks to getfield/setfield

Fixes numpy#2346. Fixes numpy#3256. Fixes numpy#2599. Fixes numpy#3253. Fixes numpy#3286.
Fixes numpy#5762.
ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 4, 2015
Previously views of structured arrays containing objects were completely
disabled.  This commit adds more lenient check for whether an object-array
view is allowed, and adds similar checks to getfield/setfield

Fixes numpy#2346. Fixes numpy#3256. Fixes numpy#2599. Fixes numpy#3253. Fixes numpy#3286.
Fixes numpy#5762.
ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 4, 2015
Previously views of structured arrays containing objects were completely
disabled.  This commit adds more lenient check for whether an object-array
view is allowed, and adds similar checks to getfield/setfield

Fixes numpy#2346. Fixes numpy#3256. Fixes numpy#2599. Fixes numpy#3253. Fixes numpy#3286.
Fixes numpy#5762.
ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 5, 2015
Previously views of structured arrays containing objects were completely
disabled.  This commit adds more lenient check for whether an object-array
view is allowed, and adds similar checks to getfield/setfield

Fixes numpy#2346. Fixes numpy#3256. Fixes numpy#2599. Fixes numpy#3253. Fixes numpy#3286.
Fixes numpy#5762.
ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 5, 2015
Previously views of structured arrays containing objects were completely
disabled.  This commit adds more lenient check for whether an object-array
view is allowed, and adds similar checks to getfield/setfield

Fixes numpy#2346. Fixes numpy#3256. Fixes numpy#2599. Fixes numpy#3253. Fixes numpy#3286.
Fixes numpy#5762.
ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 5, 2015
Previously views of structured arrays containing objects were completely
disabled.  This commit adds more lenient check for whether an object-array
view is allowed, and adds similar checks to getfield/setfield

Fixes numpy#2346. Fixes numpy#3256. Fixes numpy#2599. Fixes numpy#3253. Fixes numpy#3286.
Fixes numpy#5762.
ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 5, 2015
Previously views of structured arrays containing objects were completely
disabled.  This commit adds more lenient check for whether an object-array
view is allowed, and adds similar checks to getfield/setfield

Fixes numpy#2346. Fixes numpy#3256. Fixes numpy#2599. Fixes numpy#3253. Fixes numpy#3286.
Fixes numpy#5762.
@ahaldane
Copy link
Member

Reopening this after the partial backtrack in #6562.

Now it is partially fixed, but appending object fields still doesn't work. Once it is fixed, remember to add back the test removed in #6562.

I suspect this will be fixed by #6053.

@ahaldane ahaldane reopened this Oct 29, 2015
@hpaulj
Copy link

hpaulj commented Apr 6, 2016

This

base = merge_arrays(base, usemask=usemask, fill_value=fill_value)

is also causing problems if base is a single element masked array.

http://stackoverflow.com/questions/36440557/typeerror-when-appending-fields-to-a-structured-array-of-size-one

If len(base) is 1, merge_arrays calls np.asanyarray([base][0]). In the case of masked structure array this returns a void array element, which gives a subsequent ravel a headache.

Simply dropping this merge_arrays call might the solution, if it's sole purpose is to cleanup some obscure way of passing a base to append_fields.

(I'm not familiar enough with the numpy issues to know whether adding this to this old issue is the best choice, or whether it would be better to raise a new issue.)

I ran the test_recfunctions.py on this modification, and it ran fine. So apparently this base 'cleanup' is not needed for the test cases.

ahaldane added a commit to ahaldane/numpy that referenced this issue Jun 17, 2016
This commit attempts to make structure assignment more consistent, and
then changes multi-field indices to return a view instead of a copy.

Assignment between structures now works "by field position" rather than
"by field name".

Fixes numpy#2353, fixes numpy#6085, fixes numpy#3351, fixes numpy#6085, fixes numpy#6314,
fixes numpy#2346, fixes numpy#7058, fixes numpy#3641, fixes numpy#5994, fixes numpy#7262,
fixes numpy#7493
@eric-wieser
Copy link
Member

This is also fixed in #8514

@charris charris closed this as completed in 7412b85 Sep 9, 2017
theodoregoetz pushed a commit to theodoregoetz/numpy that referenced this issue Oct 23, 2017
This commit attempts to make structure assignment more consistent, and
then changes multi-field indices to return a view instead of a copy.

Assignment between structures now works "by field position" rather than
"by field name".

Fixes numpy#2353, fixes numpy#6085, fixes numpy#3351, fixes numpy#6085, fixes numpy#6314,
fixes numpy#2346, fixes numpy#7058, fixes numpy#3641, fixes numpy#5994, fixes numpy#7262,
fixes numpy#7493
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants