Exploring NumPy Datatypes in Python
NumPy me arrays aur matrices sabse zyada common datatypes hain, lekin iske alawa bhi kuch aur important datatypes aur structures hote hain jo specific situations me kaam aate hain. Yaha kuch aur data types hain jo NumPy support karta hai:
1. Scalar Types
Integers (
int
): NumPy me different size ke integers ka support hota hai, jaiseint8
,int16
,int32
, aurint64
. Ye system memory ke hisaab se kam ya zyada space le sakte hain.Floating-Point Numbers (
float
): Jaisefloat16
,float32
, aurfloat64
, jo decimal values ko represent karte hain.Complex Numbers (
complex
): NumPy me complex numbers ka bhi support hota hai, jaisecomplex64
aurcomplex128
, jo real aur imaginary parts ko handle karte hain.
Example:
import numpy as np
a = np.array([1.5, 2.3], dtype=np.float32) # Floating-point array
b = np.array([1 + 2j, 3 + 4j], dtype=np.complex128) # Complex number array
2. Boolean (bool
)
NumPy arrays me boolean datatype bhi use hota hai, jo True
ya False
values ko represent karta hai. Yeh boolean indexing aur filtering ke liye kaafi useful hota hai.
Example:
bool_array = np.array([True, False, True], dtype=np.bool_)
3. String (str
)
NumPy me strings ko bhi arrays ke form me store kiya ja sakta hai, lekin strings fixed-length hote hain. Isme aap ya to Unicode strings (<U
) ya byte strings (<S
) use kar sakte ho.
Example:
string_array = np.array(['Python', 'NumPy'], dtype='<U10') # Unicode string array
4. Datetime and Timedelta (datetime64
and timedelta64
)
NumPy me dates aur times ko handle karne ke liye special datatypes hote hain:
datetime64
: Ye datatype dates ko store karne ke liye use hota hai.timedelta64
: Ye datatype time difference ko measure karne ke liye use hota hai.
Example:
date_array = np.array(['2023-09-15', '2024-01-01'], dtype='datetime64')
time_diff = np.array([10, 15], dtype='timedelta64[D]') # Time difference in days
5. Object Datatype (object
)
Jab aapko mixed data types ko ek array me store karna hota hai, to aap object datatype ka use kar sakte ho. Ye general-purpose data type hai jo different Python objects ko ek jagah rakh sakta hai.
Example:
obj_array = np.array([1, 'Python', [1, 2, 3]], dtype=object)
6. Record Arrays (Structured Arrays)
Record arrays ya structured arrays me aap different fields aur datatypes ko ek array ke andar define kar sakte ho. Ye tab useful hota hai jab aapko tabular data ko handle karna ho, jaise kisi database me hota hai.
Example:
data = np.array([(1, 'John', 78.5), (2, 'Alice', 85.6)],
dtype=[('id', 'i4'), ('name', 'U10'), ('score', 'f4')])
Yaha aap ek structured array banate ho jisme alag-alag fields different data types ke saath store hoti hain.
Summary:
NumPy me arrays aur matrices ke alawa bhi kaafi saare datatypes available hain.
Scalar types jaise integers, floats, complex numbers, aur booleans ke saath kaam kiya ja sakta hai.
Strings, datetime, aur timedelta ko bhi NumPy arrays ke form me store kiya ja sakta hai.
Special cases me aap object datatype ya structured arrays use kar sakte ho jab aapko mixed data types ya tabular data handle karna ho.
Toh, NumPy me arrays aur matrices ke alawa bhi kaafi powerful datatypes available hain jo different use cases ke liye kaam aate hain.