Mar-26-2024, 02:18 PM
As deanhystad posted more info may be needed.
paul18fr good effort,but would say that look wrong in most cases.
The
Then we talk about Structured arrays.
To give one more example on how Structured arrays works
and where want to perform efficient, vectorized operations on this data.
Take a look at data manipulation,this would not be possible if not specify
Structured arrays can also easily be taken into Pandas if need more advanced stuff like grouping, Plot...
paul18fr good effort,but would say that look wrong in most cases.
The
TypeArray
dos not work(eg try with a wrong date) and repeat data unnecessary. (Mar-23-2024, 08:55 PM)water Wrote: and specific dtype
likes:
In first post he ask about specify dtype
in a NumPy array.Then we talk about Structured arrays.
To give one more example on how Structured arrays works
import numpy as np # Sample data: Transaction ID, Date, Amount, Transaction Type data = [ (1001, '2023-01-01', 250.00, 'Deposit'), (1002, '2023-01-03', -100.00, 'Withdrawal'), (1003, '2023-01-05', 200.00, 'Deposit'), (1004, '2023-01-07', -50.00, 'Withdrawal'), (1005, '2023-01-09', 300.00, 'Deposit'), ] # Define the dtype for the structured array dtype = [ ('trans_id', 'int32'), ('date', 'datetime64[D]'), ('amount', 'float64'), ('type', 'U10') # Transaction type with up to 10 characters ] transactions = np.array(data, dtype=dtype)Structured arrays are particularly useful in scenarios where working with tabular data that mixes different data types,
and where want to perform efficient, vectorized operations on this data.
Take a look at data manipulation,this would not be possible if not specify
dtype
.# Get all dates >>> transactions['date'] array(['2023-01-01', '2023-01-03', '2023-01-05', '2023-01-07', '2023-01-09'], dtype='datetime64[D]') # Find all withdrawals >>> withdrawals = transactions[transactions['type'] == 'Withdrawal'] >>> withdrawals array([(1002, '2023-01-03', -100., 'Withdrawal'), (1004, '2023-01-07', -50., 'Withdrawal')], dtype=[('trans_id', '<i4'), ('date', '<M8[D]'), ('amount', '<f8'), ('type', '<U10')]) # Calculate the total amount of deposits >>> total_deposits = transactions[transactions['type'] == 'Deposit']['amount'].sum() >>> total_deposits 750.0Operations that are easily vectorized, staying within NumPy can be faster and more memory efficient.
Structured arrays can also easily be taken into Pandas if need more advanced stuff like grouping, Plot...
import pandas as pd df = pd.DataFrame(transactions) print(df)
Output: trans_id date amount type
0 1001 2023-01-01 250.0 Deposit
1 1002 2023-01-03 -100.0 Withdrawal
2 1003 2023-01-05 200.0 Deposit
3 1004 2023-01-07 -50.0 Withdrawal
4 1005 2023-01-09 300.0 Deposit