Python Forum
[Numpy] How to store different data type in one numpy array?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Numpy] How to store different data type in one numpy array?
#7
agree with @deanhystad

For instance, if the goal is to recover data per type, I would imagine the following if you can use 2 arrays (possible?):

import numpy as np

Array1 = np.array([['2024-03-22', 71.0, 'ceh'], 
                   ['2024-03-23', 63.0, 'abc'],
                   ['2024-03-24', -50.6, 'zzzzzzz'],
                   ['2024-03-25', 13.8, 'lkj'],
                   ['2024-03-26', 05.2, 'dsfdssss'],
                   [935.2, 'hgjhg', '2024-03-27']                   
                   ])

TypeArray = np.array([['datetime64[D]', 'float64', 'string'],
                      ['datetime64[D]', 'float64', 'string'],
                      ['datetime64[D]', 'float64', 'string'],
                      ['datetime64[D]', 'float64', 'string'],
                      ['datetime64[D]', 'float64', 'string'],
                      ['float64', 'string', 'datetime64[D]']   # !!!!!!!!!!!!!!                  
                     ])


NumberOfTypes = np.unique(TypeArray)


# results are stored in a dictionary PER type but you can proceed differently
RecoveringDictionary = {}

for ntype in NumberOfTypes:
    Index = np.where(TypeArray == ntype)
    Extract = Array1[Index]
    
    if ntype == 'float64': Extract = Extract.astype(np.float64)
    # if ntype == 'datetime64[D]': Extract = Extract.astype(np.datetime64)   
    
    RecoveringDictionary.update({ ntype: Extract, })

    
# print results
for ntype in NumberOfTypes:
    print(f"{ntype} = {RecoveringDictionary[ntype]}\n")
Output:
datetime64[D] = ['2024-03-22' '2024-03-23' '2024-03-24' '2024-03-25' '2024-03-26' '2024-03-27'] float64 = [ 71. 63. -50.6 13.8 5.2 935.2] string = ['ceh' 'abc' 'zzzzzzz' 'lkj' 'dsfdssss' 'hgjhg']
Reply


Messages In This Thread
RE: [Numpy] How to store different data type in one numpy array? - by paul18fr - Mar-26-2024, 08:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: '>' not supported between instances of 'numpy.str_' and 'int' Anouar 0 315 Apr-26-2024, 09:34 AM
Last Post: Anouar
  Bitwise Operations in numpy Sowmya 3 446 Apr-03-2024, 02:51 PM
Last Post: deanhystad
  [Numpy] Load date/time from .txt to 'datetime64' type. water 4 874 Mar-01-2024, 11:16 PM
Last Post: Gribouillis
  numpy.ufunc - Arguments missunderstand MarioBruza 0 917 Jan-11-2023, 05:03 AM
Last Post: MarioBruza
  reshaping 2D numpy array paul18fr 3 1,111 Jan-03-2023, 06:45 PM
Last Post: paul18fr
  Pandas dataframes and numpy arrays bytecrunch 1 1,404 Oct-11-2022, 08:08 PM
Last Post: Larz60+
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,767 Jul-19-2022, 06:31 AM
Last Post: paul18fr
Question about Numpy indexing. water 1 1,525 Jan-18-2022, 09:52 PM
Last Post: paul18fr
  numpy masking/filtering nilamo 3 3,654 Dec-04-2021, 10:28 PM
Last Post: nilamo
  Data Science - "key of type tuple not found and not a MultiIndex" priyanshuaggarwal 0 5,414 Nov-07-2021, 11:22 PM
Last Post: priyanshuaggarwal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020