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 6335359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:50:27+00:00 2026-05-24T18:50:27+00:00

I am using version 1.5.1 of numpy and Python 2.6.6. I am reading a

  • 0

I am using version 1.5.1 of numpy and Python 2.6.6.

I am reading a binary file into a numpy array:

>>> dt = np.dtype('<u4,<i2,<i2,<i2,<i2,<i2,<i2,<i2,<i2,u1,u1,u1,u1')
>>> file_data = np.fromfile(os.path.join(folder,f), dtype=dt)

This works just fine. Examining the result:

>>> type(file_data)
<type 'numpy.ndarray'>

>>> file_data
array([(3571121L, -54, 103, 1, 50, 48, 469, 588, -10, 0, 102, 0, 0),
   (3571122L, -78, 20, 25, 45, 44, 495, 397, -211, 0, 102, 0, 0),
   (3571123L, -69, -48, 23, 60, 19, 317, -26, -151, 0, 102, 0, 0), ...,
   (3691138L, -53, 52, -2, -11, 76, 988, 288, -101, 1, 102, 0, 0),
   (3691139L, -11, 21, -27, 25, 47, 986, 253, 176, 1, 102, 0, 0),
   (3691140L, -30, -19, -63, 59, 12, 729, 23, 302, 1, 102, 0, 0)],
  dtype=[('f0', '<u4'), ('f1', '<i2'), ('f2', '<i2'), ... , ('f12', '|u1')])

>>> file_data[0]
(3571121L, -54, 103, 1, 50, 48, 469, 588, -10, 0, 102, 0, 0)

>>> file_data[0][0]
3571121    

>>> len(file_data)
120020

When I try to slice the first column:

>>> file_data[:,0]

I get:

IndexError: invalid index.

I have looked at simple examples and was able to do the slicing:

>>> a = np.array([(1,2,3),(4,5,6)])
>>> a[:,0]
array([1, 4])

The only difference I can see between my case and the simple example is that I am using the dtype. What I am doing wrong?

  • 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-05-24T18:50:28+00:00Added an answer on May 24, 2026 at 6:50 pm

    When you set the dtype like that, you are creating a Record Array. Numpy treats that like a 1D array of elements of your dtype. There’s a fundamental difference between

    file_data[0][0]
    

    and

    file_data[0,0] 
    

    In the first, you are asking for the first element of a 1D array and then retrieving the first element of that returned element. In the second, you are asking for the element in the first row of the first column of a 2D array. That’s why you are getting the IndexError.

    If you want to access an individual element using 2D notation, you can create a view and work with that. Unfortunately, AFAIK if you want to treat your object like a 2D array, all elements have to have the same dtype.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: after reading this http://projects.scipy.org/numpy/ticket/1322 it seems that the NumPy version I am using
I'm using git version 1.5.6.3, and it seems git doesn't notice a folder's mode
I am using Numpy to store data into matrices. Coming from R background, there
I am trying to read in data from a text file using numpy.loadtxt with
How can I check which version of NumPy I'm using?
I have set up a NInject (using version 1.5) binding like this: Bind<ISessionFactory>().ToMethod<ISessionFactory>(ctx =>
Let me preface by saying that I'm new to ant, and I'm using version
Ok, so somewhere, something is wrong. When I was using version 1.3 the accordion
Using a version control system for your source code (like subversion ) makes sense
We are currently using Mysql version 5.0 in our application and are thinking of

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.