h2oai / datatable

A Python package for manipulating 2-dimensional tabular data structures
https://datatable.readthedocs.io
Mozilla Public License 2.0
1.82k stars 157 forks source link

create Frame object from ctypes Structure #2216

Open biowpn opened 4 years ago

biowpn commented 4 years ago

say I have a user defined Structure, and some instances:

from ctypes import *

class Person(Structure):
  _fields_ = [
    ("name", c_char * 16),
    ("age", c_int32),
    ("height", c_double),
    ("location", c_int32 * 2)
  ]

alice = Person(b"Alice", 25, 1.72, (3, 5))
bob = Person(b"Bob", 27, 1.77, (4, 3))

I would like to construct a Frame object like

f = dt.Frame([alice, bob])
print(f)

which would give

   name   age   height  location_0  location_1
0  Alice  25    1.72    3           5
1  Bob    27    1.77    4           3

right now, datatable will give ValueError: Unknown format 'T{(16)<c:name:<i:age:<d:height:(2)<i:location:}' with itemsize 40. It would be nice to be able create Frame objects this way.

As a plus, I don't think numpy or pandas fully supports such construction (numpy is close, but it can't handle C-string; it will break it up to 16 chars).

firmai commented 4 years ago

I would like to do the same, but from a structured numpy array. This would be very helpful and can't be too hard to implement.