Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions battdat/io/hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,34 @@ def read_df_from_table(table: Table) -> pd.DataFrame:
array = np.empty((table.nrows,), dtype=table.dtype)
for i, row in enumerate(table.iterrows()):
array[i] = row.fetch_all_fields()
as_dict = dict((c, array[c]) for c in array.dtype.names)

# Expand ndarrays into a list
for k, v in as_dict.items():
if v.ndim != 1:
as_dict[k] = list(v)
as_dict = {}
# Expand the array
for c in array.dtype.names:
col = array[c]

# Expand ndarrays into a list
if col.ndim != 1:
as_dict[c] = list(col)
# Decode fixed-length byte strings to Python str
elif col.dtype.kind == "S":
as_dict[c] = np.char.decode(col, "ascii")
else:
as_dict[c] = col

return pd.DataFrame(as_dict)

# as_dict = dict((c, array[c]) for c in array.dtype.names)

# # Expand ndarrays into a list
# for k, v in as_dict.items():
# if v.ndim != 1:
# as_dict[k] = list(v)
# return pd.DataFrame(as_dict)


@contextmanager
def as_hdf5_object(path_or_file: Union[PathLike, File], **kwargs) -> File:
def as_hdf5_object(path_or_file: Union[PathLike, File], **kwargs):
"""Open a path as a PyTables file object if not done already.

Keyword arguments are used when creating a store from a new file
Expand Down
94 changes: 47 additions & 47 deletions notebooks/extract-from-batterydata.ipynb

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/io/test_hdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_append(tmpdir, prefix):
df_copy = read_df_from_table(table)
assert len(df_copy) == len(example_df) * 2
assert np.allclose(df_copy['a'], [1, 2, 1, 2])
assert np.equal(df_copy['c'], [b'charge', b'discharge'] * 2).all()
assert np.equal(df_copy['c'], ['charge', 'discharge'] * 2).all()

# Test data check
with raises(ValueError, match='Existing and new'):
Expand Down
Loading