Commit 1d2c0097 authored by David Sikter's avatar David Sikter
Browse files

Merge branch '501-add-pandas-object-datatype-support-to-nomad' into 'develop'

Resolve "Add Pandas object (datatype) support to NOMAD"

Closes #501

See merge request !753
parents b91bc30d c0cb0e5f
Pipeline #138292 passed with stages
in 79 minutes and 10 seconds
......@@ -27,6 +27,7 @@ import inspect
import re
import json
import numpy as np
import pandas as pd
import pint
import pint.unit
import pint.quantity
......@@ -1467,6 +1468,14 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
value = value.to(quantity_def.unit).magnitude
if isinstance(value, pd.DataFrame):
try:
value = value.to_numpy()
except AttributeError:
raise AttributeError(
'Could not convert value %s of type pandas.Dataframe to a numpy array' %
(value))
if type(value) != np.ndarray:
if len(quantity_def.shape) > 0:
try:
......@@ -1501,6 +1510,8 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
if isinstance(quantity_def.type, np.dtype):
value = self.__to_np(quantity_def, value)
elif isinstance(quantity_def.type, pd.DataFrame):
value = self.__to_np(quantity_def, value)
else:
dimensions = len(quantity_def.shape)
if dimensions == 0:
......
......@@ -3,6 +3,7 @@ pip
wheel
pyyaml==6.0
numpy==1.21.2
pandas==1.3.2
fastentrypoints==0.12
cython>=0.19
future==0.18.2
......@@ -28,7 +29,6 @@ memoization==0.4.0
# [parsing]
netCDF4==1.5.4
pandas==1.3.2
h5py==3.6.0
hjson==3.0.2
scipy==1.7.1
......
......@@ -21,6 +21,7 @@
import pytest
import numpy as np
import pandas as pd
import pint.quantity
from nomad.metainfo.metainfo import (
......@@ -491,6 +492,12 @@ class TestM1:
system.atom_positions = [[1, 2, 3]]
assert isinstance(system.atom_positions, pint.quantity._Quantity)
def test_pd_dataframe(self):
system = System()
system.atom_positions = pd.DataFrame([[1, 2], [3, 4]])
assert isinstance(system.atom_positions, pint.quantity._Quantity)
assert np.all(system.atom_positions.m == [[1, 2], [3, 4]])
def test_np_scalar(self):
class TestSection(MSection):
test_quantity = Quantity(type=np.dtype('int16'))
......@@ -500,6 +507,14 @@ class TestM1:
assert test_section.test_quantity == 12
assert type(test_section.test_quantity) == np.int16
def test_pd_dataframe_quantity(self):
class TestSection(MSection):
test_quantity = Quantity(type=np.dtype('int16'))
test_section = TestSection()
test_section.test_quantity = pd.DataFrame([[1, 2]])
assert np.all(test_section.test_quantity == [1, 2])
def test_unit_conversion(self):
system = System()
system.atom_positions = [[1, 2, 3]] * ureg.angstrom
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment