Skip to main content

NumPy Data Types and Declaration


In this tutorial you will learn:

  • Various Data types in NumPy
  • How to declare NumPy data types?
  • How to use NumPy data types?

Data Types in NumPy

NumPy provides a greater flexibility to the users by providing them a great number of in built data types, which could be declared and further used by the programmers as per the requirement of the overall program sequence, hence avoiding any memory loss, increasing space efficiency and overall improving the program’s speed. A character code uniquely identifies the NumPy  built in data type.

Character Codes for Built in Data Types

  • 'b' – Boolean
    • It can either be true or false (Stored in a byte)
  • 'i' − (signed) integer
    • Distributed in further 7 types as
      • int_
        • It is a default integer type, it is either int32 or int64. It is just like C long
      • Intc
        • It is int32 or int64, it is similar to C int
      • Intp
        • It is int32 or int64, it is similar to C ssize_t and used for indexing only
      • int8
        • Numbers from -127 to 128 can be represented using int8
      • int16
        • Numbers from -32768 to 32767 can be represented using int16
      • int32
        • Numbers from -2147483648 to 2147483647can be represented using int32
      • int64
        • Numbers from -9223372036854775808 to 9223372036854775807 can be represented using int64
  • 'u' − unsigned integer
    • Distributed further into 4 types
      • uint8
        • Numbers from 0 to 255 can be represented using uint8
      • uint16
        • Numbers from 0 to 65535 can be represented using uint16
      • uint32
        • Numbers from 0 to 4294967295 can be represented using uint32
      • uint64
        • Numbers from 0 to 18446744073709551615 can be represented using uint32
  • 'f' − floating-point
    • Distributed further into 4 types
      • float_
        • It is same as float64
      • float16
        • It has 1 sign bit, 5 bits for exponent and 10 bits for mantissa( also known as half precision float)
      • float32
        • It has 1 sign bit, 8 bits for exponent and 23 bits for mantissa( also known as Single precision float)
      • float64
        • It has 1 sign bit, 11 bits for exponent and 52 bits for mantissa( also known as Double precision float)
  • 'c' − complex-floating point
    • Distributed further into 3 types
      • complex_
        • It is same as complex128
      • complex64
        • Use 32 bit float for real and imaginary components each
      • complex128
        • Use 32 bit float for real and imaginary components each
  • 'm' − timedelta
  • 'M' − datetime
  • 'O' − (Python) objects
  • 'S', 'a' − (byte-)string
  • 'U' − Unicode
  • 'V' − raw data (void)

Declaring and Using NumPy Data Types

We can declare an array of any of the above mentioned data types and further use in our code. For a specific data type we declare it by assigning character code parameter ‘dtype’ as demonstrated in follow up examples.  Below are the given examples for declaration of all types

In this example we are creating an array of data type Boolean.

  1. print ('**NumPy Data Types**')
  2. arr = np.array((False,True),dtype='b')
  3. print('Content of Boolean Array are :', arr)
  4. print('Type of Array is:' , arr.dtype)

In this example we are creating an array of data type Int having 8 bytes

  1. import numpy as np
  2. arr = np.array([20,30,40,50,60,70], dtype='i8')
  3. print('Content of Int Array are :', arr)
  4. print('Type of Array is:' , arr.dtype)

In this example we are creating an array of data type unsigned int having 16 bytes

  1. import numpy as np
  2. arr = np.array([13,14,15,12], dtype='uint16')
  3. print('Content of Unsigned Int Datatype Array are :', arr)
  4. print('Type of Array is:' , arr.dtype)

In this example we are creating an array of data type float having 64 bytes

  1. import numpy as np
  2. arr = np.array([13.223,17.921,66.96,69.11,80], dtype='float64')
  3. print('Content of float Datatype Array are :', arr)
  4. print('Type of Array is:' , arr.dtype)

In this example we are creating an array of data type Complex having 64 bits

  1. import numpy as np
  2. arr = np.array([2j, 1+2j, 3-5j,1],dtype='complex64')
  3. print('Content of Complex Datatype Array are :',arr)
  4. print('Type of Array is:' , arr.dtype)

In this example we will create a datetime object

  1. from datetime import date
  2. today = date.today();
  3. print('Time now is',today)

In this example we will create an a python object

  1. import numpy as np
  2. arr = np.array([9,8,7,6,5,4], dtype='O')
  3. print('Content of Object Datatype Array are :',arr)
  4. print('Type of Array is:' , arr.dtype)

In this example we will create a String datatype array.

  1. import numpy as np
  2. arr = np.array([10,20,30,40,50,60], dtype='S')
  3. print('Content of String Datatype Array are :',arr)
  4. print('Type of Array is:' , arr.dtype)

In this example we will create a unicode array

  1. import numpy as np
  2. arr = np.array([10, 20, 9,1],dtype='U')
  3. print('Content of Unicode Datatype Array are :',arr)
  4. print('Type of Array is:' , arr.dtype)

In this example we will create a Raw Data array

  1. import numpy as np
  2. arr = np.array(bytes('Sourcecodester', "utf8"), dtype='V')
  3. print('Content of Raw Datatype Array are :',arr)
  4. print('Type of Array is:' , arr.dtype)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.