Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

numpy datatypes

import numpy as np

arr = np.array([1, 2, 3, 4])

# print array datatype
print(arr.dtype)
# output int64

# 8 byte integer datatype
arr = np.array([1, 2, 3, 4], dtype="i8")
print(arr)
# output [1 2 3 4]

# string datatype
arr = np.array([1, 2, 3, 4], dtype="S")
print(arr)
# output [b'1' b'2' b'3' b'4']

arr = np.array([1, 0, 3])

# converting arrays
arr = arr.astype(str)
print(arr)
# output ['1' '0' '3']

arr = arr.astype(float)
print(arr)
# output [1. 0. 3.]

arr = arr.astype(bool)
print(arr)
# output [ True False  True]

print(arr.dtype)
# output bool

arr = np.array(["mike", "sam"])
arr = arr.astype(int)
# output ValueError: invalid literal for int() with base 10: 'mike'

# data types

# i - integer
# b - boolean
# u - unsigned integer
# f - float
# c - complex float
# m - timedelta
# M - datetime
# O - object
# S - string
# U - unicode string
# V - fixed chunk of memory for other type ( void )
Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #numpy #datatypes
ADD COMMENT
Topic
Name
5+7 =