Skip to content

Commit

Permalink
Make odbclient pandas-2.x compliant
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Mueller <[email protected]>
  • Loading branch information
johannes-mueller committed Aug 31, 2023
1 parent 6f2a799 commit 6d99dd1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
10 changes: 5 additions & 5 deletions tools/odbclient/src/odbclient/odbclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def element_connectivity(self, instance_name, elset_name=''):
"""
index, connectivity = self._query('get_connectivity', (instance_name, elset_name))
return pd.DataFrame({'connectivity': connectivity},
index=pd.Int64Index(index, name='element_id'))
index=pd.Index(index, name='element_id', dtype=np.int64))

def nset_names(self, instance_name=''):
"""Query the available node set names.
Expand Down Expand Up @@ -257,7 +257,7 @@ def node_ids(self, nset_name, instance_name=''):
The node ids as :class:`pandas.Index`
"""
node_ids = self._query('get_node_set', (instance_name, nset_name))
return pd.Index(node_ids, name='node_id')
return pd.Index(node_ids, name='node_id', dtype=np.int64)

def elset_names(self, instance_name=''):
"""Query the available element set names.
Expand Down Expand Up @@ -293,7 +293,7 @@ def element_ids(self, elset_name, instance_name=''):
The element ids as :class:`pandas.Index`
"""
element_ids = self._query('get_element_set', (instance_name, elset_name))
return pd.Int64Index(element_ids, name='element_id')
return pd.Index(element_ids, name='element_id', dtype=np.int64)

def step_names(self):
"""Query the step names from the odb file.
Expand Down Expand Up @@ -367,9 +367,9 @@ def variable(self, variable_name, instance_name, step_name, frame_id, nset_name=

index_labels = _ascii(_decode, index_labels)
if len(index_labels) > 1:
index = pd.DataFrame(index_data, columns=index_labels).set_index(index_labels).index
index = pd.DataFrame(index_data, columns=index_labels, dtype=np.int64).set_index(index_labels).index
else:
index = pd.Int64Index(index_data[:, 0], name=index_labels[0])
index = pd.Index(index_data[:, 0], name=index_labels[0], dtype=np.int64)

column_names = _ascii(_decode, labels)
return pd.DataFrame(values, index=index, columns=column_names)
Expand Down
24 changes: 22 additions & 2 deletions tools/odbclient/tests/test_odbclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ def test_odbclient_node_coordinates(client):
pd.testing.assert_frame_equal(client.node_coordinates('PART-1-1'), expected)


def test_odbclient_node_ids(client):
result = client.node_ids('FIX', 'PART-1-1')
expected = pd.Index([1, 2, 3, 4, 22, 27, 31, 32], dtype='int64', name='node_id')

pd.testing.assert_index_equal(result, expected)


def test_odbclient_element_ids(client):
result = client.element_ids('FIX', 'PART-1-1')
expected = pd.Index([1], dtype='int64', name='element_id')

pd.testing.assert_index_equal(result, expected)


def test_odbclient_node_coordinates_invalid_instance_name(client):
with pytest.raises(KeyError, match="Invalid instance name 'nonexistent'."):
client.node_coordinates('nonexistent')
Expand Down Expand Up @@ -120,13 +134,19 @@ def test_variable_names(client):


def test_variable_stress_element_nodal(client):
expected = pd.read_csv('tests/stress_element_nodal.csv',
index_col=['node_id', 'element_id'])
expected = pd.read_csv('tests/stress_element_nodal.csv', index_col=['node_id', 'element_id'])
result = client.variable('S', 'PART-1-1', 'Load', 1)

pd.testing.assert_frame_equal(result, expected)


def test_variable_evol(client):
result = client.variable('EVOL', 'PART-1-1', 'Load', 1)
expected = pd.DataFrame({'EVOL': [1000.]}, index=pd.Index([1, 2, 3, 4], name='element_id'))

pd.testing.assert_frame_equal(result, expected)


def test_variable_invalid_instance_name(client):
with pytest.raises(KeyError, match="nonexistent"):
client.variable('S', 'nonexistent', 'Load', 1)
Expand Down

0 comments on commit 6d99dd1

Please sign in to comment.