Datatypes in Numpy

Python Numpy supports the following data types:

  • b – boolean
  • u – unsigned integer
  • f – float
  • c – complex float
  • m – timedelta
  • M – datetime
  • O – object
  • S – string
  • U – unicode string

Let us see some examples:

  • Get the datatype of a NumPy array with integers
  • Get the datatype of a NumPy array with strings
  • Set the datatype size within a NumPy array
  • Convert one datatype to another

Get the datatype of a Numpy array with integers

To get the datatype of a Numpy array, use the dtype attribute. Let us see an example:

Output

Get the datatype of a Numpy array with strings

To get the datatype of a Numpy array, use the dtype attribute. Let us see an example:

Output

Set the datatype size within a Numpy array

Set a defined data type to a new array using the dtype attribute. With that set the size as well. Let us see an example:

Output

In the above example, we have set the data type as string and size 5.

Convert one datatype to another

Use the astype() to create a copy of an array and then set the new data type. This is how you can convert one type to another.

In the example, we have an array of strings. We will convert it to an integer, with i as a parameter as shown below:

Output

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.

For Videos, Join Our YouTube Channel: Join Now


Read More:

NumPy - Array Slicing
NumPy Array Shape
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading