Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert Numpy dtypes to Native Python Types

  • 23 Dec, 2024
  • Com 0
Converting Numpy dtypes to Native Python Types

Numpy is a fantastic library for numerical operations, but we can’t use its data types everywhere. For example, if we are working on a Database, we often use psycopg2 for PostgreSQL, and this library always expects Python’s native data types instead of dtypes. That is why we need type conversion.

The most efficient and straightforward way to convert Numpy dtypes to Native Python data types is by using the “.item()” method.

Converting Numpy dtypes to Native Python Types

Numpy .item() Method

The .item() is a method that converts a Numpy scalar (a single value, not an array) into its equivalent native Python type.

The numpy scalar types include but are not limited to numpy.int32, which is equivalent to the int type in native Python. The numpy.float64’s equivalent is float in Python.

Always remember that the .item() method works on single or scalar values that include zero-dimensional arrays or individual elements of an array, and it returns a native data type depending on the input value.

It performs an explicit conversion.

If you want to, let’s say, convert an entire numpy array to Python’s type list, then you must use the “tolist()” method. Here, you are converting the whole array, not an individual element.

Basic Numerical Types

Basic Numerical Types

import numpy as np

# Integer types
np_int8 = np.int8(10)
py_int = np_int8.item()
print(type(np_int8))  # <class 'numpy.int8'>
print(type(py_int))  # <class 'int'>

np_uint16 = np.uint16(1000)
py_int16 = np_uint16.item()
print(type(np_uint16))  # <class 'numpy.uint16'>
print(type(py_int16))  # <class 'int'>

np_float32 = np.float32(3.14)
py_float = np_float32.item()
print(type(np_float32))  # <class 'numpy.float32'>
print(type(py_float))  # <class 'float'>

np_float64 = np.float64(2.71828)
py_float64 = np_float64.item()
print(type(np_float64))  # <class 'numpy.float64'>
print(type(py_float64))  # <class 'float'>

np_complex = np.complex128(1+2j)
py_complex = np_complex.item()
print(type(np_complex))  # <class 'numpy.complex128'>
print(type(py_complex))  # <class 'complex'>

In the above code, we defined different types of numeric values, including numpy.int8, numpy.float32, numpy.float64, and numpy.complex128 and converted into equivalent native types int, float, and complex.

Boolean Type

import numpy as np

# Boolean type
np_bool = np.bool_(True)  # Note the underscore: np.bool_
py_bool = np_bool.item()

print(type(np_bool))  # <class 'numpy.bool_'>
print(type(py_bool))  # <class 'bool'>

You can define a boolean value in numpy using np.bool_. You can see that the .item() method converts numpy.bool_ type into bool type.

Datetime and Timedelta Types

import numpy as np

# Datetime and Timedelta Types
np_datetime = np.datetime64('2024-12-23T10:30:00')
py_datetime = np_datetime.item()
print(type(np_datetime)) # <class 'numpy.datetime64'>
print(type(py_datetime)) # <class 'datetime.datetime'>

np_timedelta = np.timedelta64(5, 'D') # 5 days
py_timedelta = np_timedelta.item()
print(type(np_timedelta)) # <class 'numpy.timedelta64'>
print(type(py_timedelta)) # <class 'datetime.timedelta'>

In this code, we defined np.datetime64 type and then converted it into datetime.datetime, which is the native type. And then converted np.timedelta64 type into datetime.timedelta.

Bytes Types

import numpy as np

np_string = np.bytes_("hello") # Note the underscore
py_string = np_string.item()

print(type(np_string)) # <class 'numpy.bytes_'>
print(type(py_string)) # <class 'bytes'>

These examples should give us a good understanding of how .item() works across different NumPy data types.

Post Views: 123
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Exporting Pandas DataFrame into a PDF File in Python
Printing a NumPy Array Without Truncation in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend