Skip to content

Commit 684bf65

Browse files
authored
Merge branch 'main' into send_dicts
2 parents fd634d4 + 22cc5e7 commit 684bf65

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/pymadng/madp_classes.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,16 @@ def __next__(self):
199199
except IndexError:
200200
raise StopIteration
201201

202-
def to_df(self, columns: list = None): # For backwards compatibility (jgray 2024)
202+
def to_df(self, columns: list = None, force_pandas: bool = False): # For backwards compatibility (jgray 2024)
203203
"""See `convert_to_dataframe`"""
204-
return self.convert_to_dataframe(columns)
204+
return self.convert_to_dataframe(columns, force_pandas)
205205

206-
def convert_to_dataframe(self, columns: list = None):
206+
def convert_to_dataframe(self, columns: list = None, force_pandas: bool = False):
207207
"""Converts the object to a pandas dataframe.
208208
209-
This function imports pandas and tfs-pandas, if tfs-pandas is not installed, it will only return a pandas dataframe.
210-
211209
Args:
212210
columns (list, optional): List of columns to include in the dataframe. Defaults to None.
211+
force_pandas (bool, optional): If True, always use pandas.DataFrame. Defaults to False.
213212
214213
Returns:
215214
pandas.DataFrame or tfs.TfsDataFrame: The dataframe containing the object's data.
@@ -227,7 +226,10 @@ def convert_to_dataframe(self, columns: list = None):
227226
# If tfs is available, use the headers attribute
228227
DataFrame, hdr_attr = tfs.TfsDataFrame, "headers"
229228
except ImportError:
230-
# Otherwise, use the pandas dataframe and attrs attribute
229+
force_pandas = True
230+
231+
if force_pandas:
232+
# If pandas is the only option, use pandas DataFrame, with the attrs attribute
231233
DataFrame, hdr_attr = pd.DataFrame, "attrs"
232234

233235
py_name, obj_name = self._mad.py_name, self._name

tests/test_object_wrapping.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import unittest
55

66
import numpy as np
7-
import pandas
7+
import pandas as pd
88
import tfs
99

1010
from pymadng import MAD
@@ -369,7 +369,7 @@ def test_history(self):
369369

370370

371371
class TestDataFrame(unittest.TestCase):
372-
def generalDataFrame(self, headers, DataFrame):
372+
def generalDataFrame(self, headers, DataFrame, force_pandas=False):
373373
mad = MAD()
374374
mad.send("""
375375
test = mtable{
@@ -394,7 +394,7 @@ def generalDataFrame(self, headers, DataFrame):
394394
test:addcol("generator", \\ri, m -> m:getcol("number")[ri] + 1i * m:getcol("number")[ri])
395395
test:write("test")
396396
""")
397-
df = mad.test.to_df()
397+
df = mad.test.to_df(force_pandas=force_pandas)
398398
self.assertTrue(isinstance(df, DataFrame))
399399
header = getattr(df, headers)
400400
self.assertEqual(header["name"], "test")
@@ -434,9 +434,12 @@ def test_tfsDataFrame(self):
434434

435435
def test_pandasDataFrame(self):
436436
sys.modules["tfs"] = None # Remove tfs-pandas
437-
self.generalDataFrame("attrs", pandas.DataFrame)
437+
self.generalDataFrame("attrs", pd.DataFrame)
438438
del sys.modules["tfs"]
439439

440+
def test_tfsDataFrame_force_pandas(self):
441+
self.generalDataFrame("attrs", pd.DataFrame, force_pandas=True)
442+
440443
def test_failure(self):
441444
with MAD() as mad:
442445
mad.send("""

0 commit comments

Comments
 (0)