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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:23:05+00:00 2026-05-26T09:23:05+00:00

If I wanted to change the data type of a numpy array permanently, is

  • 0

If I wanted to change the data type of a numpy array permanently, is reassignment the best way?

Here’s an example to illustrate the syntax:

import numpy as np
x = np.array([1],dtype='float')
x = x.view(dtype=('x','float"))

However, I’d prefer to change change the data type “in place”.

Is there any way to change the dtype of an array in-place? Or is it better to so something similar to this?:

x = x.view(dtype=('x',"float")).copy()
  • 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-26T09:23:06+00:00Added an answer on May 26, 2026 at 9:23 am

    There is no “permanent” dtype, really.

    Numpy arrays are basically just a way of viewing a memory buffer.

    Views, in the numpy sense, are just a different way of slicing and dicing the same memory buffer without making a copy.

    Keep in mind, though, that this is giving you low-level control over the way that the memory buffer is interpreted.

    For example:

    import numpy as np
    x = np.arange(10, dtype=np.int)
    
    print 'An integer array:', x
    print 'But if we view it as a float:', x.view(np.float)
    print "...It's probably not what we expected..."
    

    This yields:

    An integer array: [0 1 2 3 4 5 6 7 8 9]
    But if we view it as a float: [  0.00000000e+000   4.94065646e-324   
       9.88131292e-324   1.48219694e-323   1.97626258e-323   
       2.47032823e-323   2.96439388e-323   3.45845952e-323
       3.95252517e-323   4.44659081e-323]
    ...It's probably not what we expected...
    

    So, we’re interpreting the underlying bits of the original memory buffer as floats, in this case.

    If we wanted to make a new copy with the ints recasted as floats, we’d use x.astype(np.float).

    The reason views (of dtypes… Slicing returns a view as well, though that’s a separate topic.) are so useful, is that you can do some really nice tricks without having to duplicate things in memory.

    For example, if you wanted to convert floats to ints in place (without duplicating memory), you can use a couple of tricks with views to do this. (Based on @unutbu’s answer on this question.)

    import numpy as np
    x = np.arange(10, dtype=np.int)
    print 'The original int array:', x
    
    # We would have just used y = x.astype(np.float), but it makes a copy.
    # This doesn't. If we're worried about memory consumption, it's handy!
    y = x.view(np.float)
    y[:] = x
    
    print 'The new float array:', y
    print 'But, the old int array has been modified in-place'
    print x
    print "They're both views into the same memory buffer!"
    

    Similarly, you can do various low-level bit-twiddling:

    import numpy as np
    x = np.arange(10, dtype=np.uint16)
    y = x.view(np.uint8)
    
    print 'The original array:', x
    print '...Viewed as uint8:', y
    print '...Which we can do some tricks with', y[::2]
    
    # Now let's interpret the uint16's as two streams of uint8's...
    a, b = y[::2], y[1::2]
    b[:] = np.arange(10, dtype=np.uint8)
    print a
    print b
    print x
    print y
    # Notice that the original is modified... We're not duplicating memory anywhere!
    

    To answer your question, “better” is all relative. Do you want a copy, or do you want to view the same memory buffer in a different way?

    As a side note, astype always makes a copy, regardless of the “input” and “output” dtypes. It’s often what people actually want when they refer to view. (e.g. if you want to convert between ints and floats, use astype, not view, unless you need to micro-manage memory usage.)

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

Sidebar

Related Questions

(Note: the data here is a made up example since I can't post the
I wanted to change the resolution of a image,this image I am getting from
I have a ListView inside of an Update Panel and wanted to change the
I have a JSP which has jQuery and wanted to change the action message
i 'm trying the hibernate tutorials from their main site and wanted to change
I'm working on a quick setup program in Visual Studio and wanted to change
I wanted to see what happened if you change the reference of an object
I am getting started with SubSonic. I wanted to change my db to sqlite
Here’s a really really super-basic question. Say I wanted to build – today –
Lets say I wanted to scrape a webpage, and extract some data. I'd most

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.