Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8467653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:42:26+00:00 2026-06-10T15:42:26+00:00

relatively new to python so excuse me if this has an obvious answer that

  • 0

relatively new to python so excuse me if this has an obvious answer that I havent found.

I am reading some temporaly contiguous binary files into numpy record arrays with the end goal of storing them in a pytable. The problem I anticipate is that the files may not all have the same fields, or the same field order. I have been looking for a numpy function that will sort the columns (NOT the rows) of a recarray using either the field labels or an index. Even better would be a function that does this for you – and accounts for missing columns – when you append a recarray to another. Below is a sample of what I had in mind:

#-------script------------
Myarray1 = np.array([(1,2,3),(1,2,3),(1,2,3)], {'names': ('a','b','c'), 'formats': ('f4', 'f4', 'f4')})
Myarray2 = np.array([(2,1,4,3),(2,1,4,3),(2,1,4,3)], {'names': ('b','a','d','c'), 'formats': ('f4', 'f4', 'f4', 'f4')})
Myarray3 = SomeColumnSortFunction(Myarray2, sortorder=[2,1,4,3])
Myarray4 = SomeBetterVerticalStackFunction(Myarray1,Myarray2)
#
print(Myarray1)
print()
print(Myarray2)
print()
print(Myarray3)
print()
print(Myarray4)

#---------- Wished for Output -------------
[(1.0, 2.0, 3.0) (1.0, 2.0, 3.0) (1.0, 2.0, 3.0)],
 dtype=[('a', 'i4'), ('b', 'i4'), ('c', 'i4')]

[(2.0, 1.0, 4.0, 3.0) (2.0, 1.0, 4.0, 3.0) (2.0, 1.0, 4.0, 3.0)],
dtype=[('b', 'i4'), ('a', 'i4'), ('d', 'i4'), ('c', 'i4')]


[(1.0, 2.0, 3.0, 4.0) (1.0, 2.0, 3.0, 4.0) (1.0, 2.0, 3.0, 4.0)] 
dtype=[('a', 'i4'), ('b', 'i4'), ('c', 'i4'), ('d', 'i4')]

[(1.0, 2.0, 3.0, NaN) (1.0, 2.0, 3.0, NaN) (1.0, 2.0, 3.0, NaN),
 (1.0, 2.0, 3.0, 4.0) (1.0, 2.0, 3.0, 4.0) (1.0, 2.0, 3.0, 4.0)] 
dtype=[('a', 'i4'), ('b', 'i4'), ('c', 'i4'), ('d', 'i4')]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:42:28+00:00Added an answer on June 10, 2026 at 3:42 pm
    • If you want to reorder the fields of your structured array, just use fancy indexing:

      MyArray3 = MyArray2[['a','b','c','d']]
      

      If you want to use integers to sort your fields, you can use something like:

      order = [1,0,3,2]
      names = MyArray2.dtype.names
      MyArray3 = MyArray2[[names[i] for i in order]]
      

      (in your sortorder=[2,1,4,3], you probably forgot that the first index of an iterable is 0…)

    • For stacking structured arrays, have a look to the numpy.lib.recfunctions submodule, the stack_arrays function in particular. Note that you have to use import numpy.lib.recfunctions explicitly

    Here’s the docstring

    stack_arrays(arrays, defaults=None, usemask=True, asrecarray=False, autoconvert=False)
    
    Superposes arrays fields by fields
    
    Parameters
    ----------
    seqarrays : array or sequence
        Sequence of input arrays.
    defaults : dictionary, optional
        Dictionary mapping field names to the corresponding default values.
    usemask : {True, False}, optional
        Whether to return a MaskedArray (or MaskedRecords is `asrecarray==True`)
        or a ndarray.
    asrecarray : {False, True}, optional
        Whether to return a recarray (or MaskedRecords if `usemask==True`) or
        just a flexible-type ndarray.
    autoconvert : {False, True}, optional
        Whether automatically cast the type of the field to the maximum.
    
    Examples
    --------
    >>> from numpy.lib import recfunctions as rfn
    >>> x = np.array([1, 2,])
    >>> rfn.stack_arrays(x) is x
    True
    >>> z = np.array([('A', 1), ('B', 2)], dtype=[('A', '|S3'), ('B', float)])
    >>> zz = np.array([('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)],
    ...   dtype=[('A', '|S3'), ('B', float), ('C', float)])
    >>> test = rfn.stack_arrays((z,zz))
    >>> test
    masked_array(data = [('A', 1.0, --) ('B', 2.0, --) ('a', 10.0, 100.0) ('b', 20.0, 200.0)
     ('c', 30.0, 300.0)],
                 mask = [(False, False, True) (False, False, True) (False, False, False)
     (False, False, False) (False, False, False)],
           fill_value = ('N/A', 1e+20, 1e+20),
                dtype = [('A', '|S3'), ('B', '<f8'), ('C', '<f8')])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sorry if this is an obvious question, but I am relatively new to python
I'm still relatively new to Python, so if this is an obvious question, I
Relatively new to python. I recently posted a question in regards to validating that
I'm a relatively new convert to Python. I've written some code to grab/graph data
I'm relatively new to the web development scene, so please excuse any frustratingly obvious
I'm relatively new to the Python world, but this seems very straight forward. Google
I am relatively new to python (already did some 1h scripts like a little
I'm relatively new to python and scrapy and am in need of some assistance
I am relatively new in python, was working on C a lot. Since I
I am relatively new to python and app engine, and I just finished my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.