Skip to content

Commit

Permalink
Merge 02fc006 into 9866fda
Browse files Browse the repository at this point in the history
  • Loading branch information
coldfix committed Aug 12, 2021
2 parents 9866fda + 02fc006 commit 8cb5c94
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
6 changes: 6 additions & 0 deletions doc/known-issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ Known issues
handles in the MAD-X process or prevent them from being inherited by
default. You have to make sure on your own that you close all file handles
before creating a new ``cpymad.madx.Madx`` instance!

- the MAD-X ``USE`` command invalidates table row names. Therefore, using
``Table.dframe()`` is unsafe after ``USE`` should be avoided, unless
manually specifying an index, e.g. ``Table.dframe(index='name')``, see `#93`_.

.. _#93: https://github.com/hibtc/cpymad/issues/93
32 changes: 28 additions & 4 deletions src/cpymad/madx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,13 @@ def col_names(self):
return self._libmadx.get_table_column_names(self._name)

def row_names(self):
"""Get table row names."""
"""
Get table row names.
WARNING: using ``row_names`` after calling ``USE`` (before recomputing
the table) is unsafe and may lead to segmentation faults or incorrect
results.
"""
return self._libmadx.get_table_row_names(self._name)

@property
Expand Down Expand Up @@ -1142,10 +1148,28 @@ def copy(self, columns=None) -> dict:
columns = self
return {column: self[column] for column in columns}

def dframe(self, columns=None):
"""Return table as ``pandas.DataFrame``."""
def dframe(self, columns=None, index=None):
"""
Return table as ``pandas.DataFrame``.
:param list columns: column names or ``None`` for all columns.
:param str index: column name or sequence to be used as index, or
``None`` for using the ``row_names``
:returns: column data as ``pandas.DataFrame``
:raises ValueError: if the table name is invalid
WARNING: using ``index=None`` is unsafe after calling ``USE``.
In this case, please manually specify another column to be used,
e.g. ``index="name"``.
"""
import pandas as pd
return pd.DataFrame(self.copy(columns), index=self.row_names())
if index is None:
index = self.row_names()
elif isinstance(index, str):
index = np.partition(self[index], ':')[:, 0]
else:
index = index
return pd.DataFrame(self.copy(columns), index=index)

def getmat(self, name, idx, *dim):
s = () if isinstance(idx, int) else (-1,)
Expand Down

0 comments on commit 8cb5c94

Please sign in to comment.